找不到方法或数据成员。

时间:2018-09-28 03:06:04

标签: excel vba excel-vba

在调试时,它将显示-找不到方法或数据成员,并在.cell处突出显示。我可以知道为什么会出现此错误吗?

我已经将单元格更改为单元格。但仍然没有用。

我在VBA中还很陌生,我已经搜索了一些网站或任何建议,但仍然让我很难理解并且不清楚。

任何帮助将不胜感激。

谢谢

Private Sub cmdAdd_Click()
'dimention the variable
Dim DataSH As Worksheet
Dim Addme As Range
Dim str As String
Dim totalRows As Long

'set the variable
Set DataSH = Sheet1
'error handler
On Error GoTo errHandler:
'set variable for the destination
Set Addme = DataSH.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
'hold in memory and stop screen flicker
Application.ScreenUpdating = False
If Me.txtName = "" Or Me.cboAmount = "" Or Me.cboCeti = "" Then
MsgBox "There is insufficient data, Please return and add the needed information"
Exit Sub
End If
'determine worksheet row to post data
totalRows = Sheet1**.Cell**(Rows, Count, "A").End(xUp).Row
If totalRows < 3 Then
totalRows = 3
Else
totalRows = totalRows
End If
'send the values to the database
Sheet1.Cell(totalRows + 1, 1) = txtName.Text
If cbWhatsapp.Value = True Then
str = "Whatsapp, "
End If
If cbSMS.Value = True Then
str = str & "SMS, "
End If
If cbEmail.Value = True Then
str = str & "Email, "
End If
If cbFacebook.Value = True Then
str = str & "Facebook, "
End If
If cbPhoneCall.Value = True Then
str = str & "Phone Call, "
'deleting comma and space, the 2 characters at the end of str
str = Left(str, Len(str) - 2)
Sheet1.Cell(totalRows + 1, 2) = str
If optYes.Value = True Then
Sheet1.Cell(totalRow + 1, 3) = "Yes"
ElseIf optNo.Value = True Then
Sheet1.Cell(totalRows + 1, 3) = "No"
End If
Sheet1.Cell(totalRows + 1, 4) = cboAmount.Value
Sheet1.Cell(totalRows + 1, 5) = cboCeti.Value
Sheet1.Cell(totalRows + 1, 6) = txtPhone.Text
Sheet1.Cell(totalRows + 1, 7) = txtEmail.Text
'sort the data by "cboCeti"
DataSH.Select
With DataSH
.Range("A2:G10000").Sort Key1:=Range("E2"), Order1:=xlAscending, Header:=xlGuess
End With

1 个答案:

答案 0 :(得分:0)

  1. 在代码的最顶部添加Option Explicit
  2. 在代码的最顶部添加Option Explicit
  3. 您没有完全限定对象。使用代码中显示的变量ws来正确限定对象的权限
  4. 您应该使用Cell时使用的是Cells
  5. 您在使用totalRow而不是totalRows时出现了一些错字
  6. 正确缩进代码会走很长一段路。即具有可读性,使您的代码将来更容易更新,并使代码更易于调试,因为它更易于遵循

Option Explicit会发现错误4和5,并通过突出显示未声明的(典型)变量和其他编译错误来引起您的注意。


Option Explicit

Private Sub cmdAdd_Click()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim Addme As Range, str As String, totalRows As Long

Set Addme = ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0)

Application.ScreenUpdating = False

If Me.txtName = "" Or Me.cboAmount = "" Or Me.cboCeti = "" Then
    MsgBox "There is insufficient data, Please return and add the needed information"
    Exit Sub
End If

totalRows = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
totalRows = Application.WorksheetFunction.Max(totalRows, 3)

ws.Cells(totalRows + 1, 1) = txtName.Text

If cbWhatsapp.Value = True Then
    str = "Whatsapp, "
End If

If cbSMS.Value = True Then
    str = str & "SMS, "
End If

If cbEmail.Value = True Then
    str = str & "Email, "
End If

If cbFacebook.Value = True Then
    str = str & "Facebook, "
End If

If cbPhoneCall.Value = True Then
    str = str & "Phone Call, "
End If

str = Left(str, Len(str) - 2)
    ws.Cells(totalRows + 1, 2) = str

If optYes.Value = True Then
    ws.Cells(totalRows + 1, 3) = "Yes"
ElseIf optNo.Value = True Then
    ws.Cells(totalRows + 1, 3) = "No"
End If

ws.Cells(totalRows + 1, 4) = cboAmount.Value
ws.Cells(totalRows + 1, 5) = cboCeti.Value
ws.Cells(totalRows + 1, 6) = txtPhone.Text
ws.Cells(totalRows + 1, 7) = txtEmail.TextG1

ws.Range("A2:G10000").Sort Key1:=Range("E2"), Order1:=xlAscending, Header:=xlGuess

End Sub