Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何在单击时向命令按钮添加VLookUp代码_Excel_Vba - Fatal编程技术网

Excel 如何在单击时向命令按钮添加VLookUp代码

Excel 如何在单击时向命令按钮添加VLookUp代码,excel,vba,Excel,Vba,我有一个userform,它根据用户输入将数据输入Excel。单击“确定”命令按钮后,输入数据,表单清除并移动到下一个空行。在单击命令输入数据之前,我想通过弹出确认来验证文本输入——“员工ID是Bob Smith”——如果正确,用户可以选择Ok,数据输入代码将继续,如果不正确,将返回到userform进行更正。我尝试使用vlookup代码,但出现了运行时或堆栈溢出错误 这是我的密码: Private Sub OkButton1_Click() Dim myLookupValue As Stri

我有一个userform,它根据用户输入将数据输入Excel。单击“确定”命令按钮后,输入数据,表单清除并移动到下一个空行。在单击命令输入数据之前,我想通过弹出确认来验证文本输入——“员工ID是Bob Smith”——如果正确,用户可以选择Ok,数据输入代码将继续,如果不正确,将返回到userform进行更正。我尝试使用vlookup代码,但出现了运行时或堆栈溢出错误

这是我的密码:

Private Sub OkButton1_Click()

Dim myLookupValue As String
Dim myFirstColumn As Long
Dim myLastColumn As Long
Dim myColumnIndex As Long
Dim myFirstRow As Long
Dim myLastRow As Long
Dim myVLookupResult As Long

Dim myTableArray As Range

myLookupValue = IDTextBox
myFirstColumn = 1
myLastColumn = 2
myColumnIndex = 2
myFirstRow = 2
myLastRow = 500

    With Worksheets("Sheet2")
        Set myTableArray = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
    End With

    myVLookupResult = WorksheetFunction.VLookup(myLookupValue, myTableArray, myColumnIndex, False)

    MsgBox "Employee Id" & myLookupValue & " is " & myVLookupResult
        Cancel = True

If Ok Then

Dim emptyRow As Long

'Make Sheet1 active
Sheet1.Activate

'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'Transfer information
Dim x As Integer

    x = 0

    For Each cCont In Me.Controls
        If TypeName(cCont) = "CheckBox" Then
            If cCont.Value = True Then
                x = x + 1
            End If
        End If
    Next

    If x = 0 Then
        MsgBox "Please Select a Lunch Option"
        Else
        Cells(emptyRow, 1).Value = IDTextBox.Value
        End If

If MealCheckBox1.Value = True Then Cells(emptyRow, 2).Value = 5

If ExtraCheckBox2.Value = True Then Cells(emptyRow, 3).Value = 1

If DessertCheckBox3.Value = True Then Cells(emptyRow, 4).Value = 1

If DrinkCheckBox4.Value = True Then Cells(emptyRow, 5).Value = 1

Call Lunch_Initialize

IDTextBox.SetFocus

End If

End Sub

按原样使用代码时出现运行时错误“1004”,无法获取WorksheetFunction类的VLookup属性

当找不到匹配项时,您需要处理错误:谢谢Scott,我测试的值在我的表中,因此应该找到匹配项。在
myTableArray
的第一列中找不到该值,请检查错误。检查数据,确保单元格真正相等,并且没有类型不匹配的文本编号。或者没有额外的空间。