收到Complie错误方法或数据成员未find – 使用VBA

我对VBA和这个网站都是新手,如果我没有提供所有必要的信息的话,那么我就是光荣的。 我正在创build一个共享的工作表 – 在O列,当他们点击我有一个用户表单创build问3个问题。 我需要将这些答案放入隐藏的工作表中。 我写了代码,但是当我运行它时,我得到了编译错误的方法。 由于我对VBA是如此的新鲜,我确信我错过了一些我不明白的东西。 有可能我错过了各种各样的错误。 这是我的第一个,我不是刚刚复制和粘贴。 所以任何帮助将不胜感激! 在这种情况下,我search了一下,find了一个我正在寻找的表单,并试图调整它以适应我所需要的。 可能是我的第一个错误!

这是突出的

iRow = ws.Cells.Find(What:="*", SearchOrder:=x1Rows, _ SearchDirection:=x1Previous, LookIn:=x1Values).Row + 1 

这是我的整个代码。 请帮忙!

 Private Sub cmdAdd_Click() Dim iRow As Long Dim ws As Worksheet Set ws = Worksheets("WebLeadInfo") 'find first empty row database iRow = ws.Cells.Find(What:="*", SearchOrder:=x1Rows, _ SearchDirection:=x1Previous, LookIn:=x1Values).Row + 1 'check for a contact If Trim(Me.txtContact.Value) = " " Then Me.txtContact.SetFocus MsgBox "Please enter info" Exit Sub End If 'copy the data to the database 'use protect and unprotect lines, ' with your password ' if worksheet is protected With ws ' .Unportect Password:="sunway12" .Cells(lRow, 1).Value = Me.txtContact.Value .Cells(lRow, 3).Value = Me.txtFind.Value .Cells(lRow, 4).Value = Me.txtSearch.Value .Protect Password:="sunway12" End With 'clear the data Me.txtContact.Value = " " Me.txtFind.Value = " " Me.txtSearch.Value = " " Me.txtContact.SetFocus End Sub Private Sub cmdClose_Click() Unload Me End Sub 

Find方法的文档中,您可以看到参数SearchOrder可以是以下XlSearchOrder常量之一:

 xlByRows xlByColumns 

SearchDirection可以是以下XlSearchDirection常量之一:

 xlNext xlPrevious 

最后LookIn参数应该被设置为:

 xlValues 

所以你的情况是这样的:

 'find first empty row database Dim findResult As Range Set findResult = ws.Cells.Find(What:="*", SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues) iRow = 1 ' in case that the sheet contains no data the first empty row is the first one If (Not findResult Is Nothing) Then iRow = findResult.Row + 1 ' ... With ws ' .Unportect Password:="sunway12" .Cells(iRow, 1).Value = Me.txtContact.Value .Cells(iRow, 3).Value = Me.txtFind.Value .Cells(iRow, 4).Value = Me.txtSearch.Value .Protect Password:="sunway12" End With 

x1Rows(以及其他)。 他们在那里使用一个“一个”字符。 但是他们应该用“L”XLROWS拼写,而不是X1ROWS