0

我正在创建一个共享工作表 - 在 O 列中,当他们单击时,我创建了一个用户表单,询问 3 个问题。我需要将这些答案放入隐藏的工作表中。我已经编写了代码,但是当我运行它时,我得到了编译错误方法。由于我对 VBA 很陌生,我确定我错过了一些我不明白的东西。我错过了各种错误。这是我的第一个,我还没有复制和粘贴。所以任何帮助将不胜感激!在这种情况下,我在谷歌上搜索并找到了一个关于我正在寻找的表格,并尝试对其进行调整以适应我需要的内容。可能是我的第一个错误!

它突出了

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
4

2 回答 2

2

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

于 2013-03-27T15:04:48.093 回答
0

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
于 2013-03-27T15:18:41.537 回答