Excel InputData1变量未使用vlookup函数

Excel InputData1变量未使用vlookup函数,excel,vba,Excel,Vba,我一直试图将vlookup的结果分配给变量,但vlookup值实际上是用户输入,但不起作用 我不知道还能对vba代码进行哪些更改:( 它向我显示了vlookup公式的调试错误 Sub Info() Dim the_sheet As Worksheet Dim table_list_object As ListObject Dim table_object_row As ListRow Dim inputData, inputData1 As String Dim wsFunc

我一直试图将vlookup的结果分配给变量,但vlookup值实际上是用户输入,但不起作用

我不知道还能对vba代码进行哪些更改:(

它向我显示了vlookup公式的调试错误

        Sub Info()

Dim the_sheet As Worksheet
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Dim inputData, inputData1 As String
Dim wsFunc As WorksheetFunction: Set wsFunc = Application.WorksheetFunction
Dim rngLook As Range

Set rngLook = Sheets("AutoZeroDatabase").Range("H1:I12")

Set the_sheet = Sheets("Info")
Set table_list_object = the_sheet.ListObjects("Table1")
Set table_object_row = table_list_object.ListRows.Add

inputData = InputBox("Enter a number from 1 to 12 to select a month i.e. 1 for January", "Input Box Text")

inputData1 = wsFunc.VLookup(CInt(inputData), rngLook, 2, False)

table_object_row.Range(1, 1).Value = inputData1

MsgBox ("Thank you for taking the time to update me :)")

End Sub

出现问题的原因似乎是从InputBox获取字符串值,然后将其与单元格H1:H12中的数值进行比较

我建议您对代码进行以下更改:

Sub Info()

    Dim the_sheet As Worksheet
    Dim table_list_object As ListObject
    Dim table_object_row As ListRow
    Dim inputData
    Dim inputData1 
    Dim rngLook As Range

    Set rngLook = Sheets("AutoZeroDatabase").Range("H1:I12")

    Set the_sheet = Sheets("Info")
    Set table_list_object = the_sheet.ListObjects("Table1")
    Set table_object_row = table_list_object.ListRows.Add

    Do
        inputData = Application.InputBox(Prompt:="Enter a number from 1 to 12 to select a month i.e. 1 for January", _
                                         Title:="Input Box Text", _
                                         Type:=1 + 4) 'Type 1 is number, 4 is boolean
        If TypeName(inputData) = "Boolean" Then
            If Not inputData Then
                inputData1 = "User refused to supply the month!!"
                Exit Do
            End If
        ElseIf inputData <> Int(inputData) Then
            MsgBox "Fractions of a month are not allowed!"
        ElseIf inputData < 1 Or inputData > 12 Then
            MsgBox "Months are numbered 1 to 12 - what am I meant to do with " & inputData & "?!?!?"
        Else
            inputData1 = Application.VLookup(inputData, rngLook, 2, False)
            If IsError(inputData1) Then
                 MsgBox "Something went very, very wrong - I couldn't find that value in the set of valid months!"
            End If
            Exit Do
        End If
    Loop

    table_object_row.Range(1, 1).Value = inputData1

    MsgBox ("Thank you for taking the time to update me :)")

End Sub
子信息()
将工作表变暗为工作表
将表格\列表\对象作为列表对象
将表格\对象\行作为列表行进行调整
Dim输入数据
Dim输入数据1
Dim rngLook As范围
设置rngLook=Sheets(“AutoZeroDatabase”)。范围(“H1:I12”)
设置工作表=工作表(“信息”)
Set table\u list\u object=表.ListObjects(“表1”)
Set table\u object\u row=table\u list\u object.ListRows.Add
做
inputData=Application.InputBox(提示:=“输入1到12之间的数字以选择月份,即1月份”_
标题:=“输入框文本”_
类型:=1+4)“类型1是数字,4是布尔值
如果TypeName(inputData)=“Boolean”,则
如果不输入数据,则
inputData1=“用户拒绝提供月份!!”
退出Do
如果结束
ElseIf inputData Int(inputData)然后
MsgBox“不允许一个月的小数!”
如果inputData<1或inputData>12,则
MsgBox“月份编号为1到12-我该如何处理”&inputData&“?!?”
其他的
inputData1=Application.VLookup(inputData,rngLook,2,False)
如果IsError(inputData1),则
MsgBox“出现了非常非常错误的情况-我在有效月份集合中找不到该值!”
如果结束
退出Do
如果结束
环
表\u对象\u行范围(1,1).Value=inputData1
MsgBox(“感谢您花时间更新我:)”)
端接头

您的问题似乎出现了,因为您正在从InputBox获取字符串值,但随后将其与单元格H1:H12中的数值进行比较

我建议您对代码进行以下更改:

Sub Info()

    Dim the_sheet As Worksheet
    Dim table_list_object As ListObject
    Dim table_object_row As ListRow
    Dim inputData
    Dim inputData1 
    Dim rngLook As Range

    Set rngLook = Sheets("AutoZeroDatabase").Range("H1:I12")

    Set the_sheet = Sheets("Info")
    Set table_list_object = the_sheet.ListObjects("Table1")
    Set table_object_row = table_list_object.ListRows.Add

    Do
        inputData = Application.InputBox(Prompt:="Enter a number from 1 to 12 to select a month i.e. 1 for January", _
                                         Title:="Input Box Text", _
                                         Type:=1 + 4) 'Type 1 is number, 4 is boolean
        If TypeName(inputData) = "Boolean" Then
            If Not inputData Then
                inputData1 = "User refused to supply the month!!"
                Exit Do
            End If
        ElseIf inputData <> Int(inputData) Then
            MsgBox "Fractions of a month are not allowed!"
        ElseIf inputData < 1 Or inputData > 12 Then
            MsgBox "Months are numbered 1 to 12 - what am I meant to do with " & inputData & "?!?!?"
        Else
            inputData1 = Application.VLookup(inputData, rngLook, 2, False)
            If IsError(inputData1) Then
                 MsgBox "Something went very, very wrong - I couldn't find that value in the set of valid months!"
            End If
            Exit Do
        End If
    Loop

    table_object_row.Range(1, 1).Value = inputData1

    MsgBox ("Thank you for taking the time to update me :)")

End Sub
子信息()
将工作表变暗为工作表
将表格\列表\对象作为列表对象
将表格\对象\行作为列表行进行调整
Dim输入数据
Dim输入数据1
Dim rngLook As范围
设置rngLook=Sheets(“AutoZeroDatabase”)。范围(“H1:I12”)
设置工作表=工作表(“信息”)
Set table\u list\u object=表.ListObjects(“表1”)
Set table\u object\u row=table\u list\u object.ListRows.Add
做
inputData=Application.InputBox(提示:=“输入1到12之间的数字以选择月份,即1月份”_
标题:=“输入框文本”_
类型:=1+4)“类型1是数字,4是布尔值
如果TypeName(inputData)=“Boolean”,则
如果不输入数据,则
inputData1=“用户拒绝提供月份!!”
退出Do
如果结束
ElseIf inputData Int(inputData)然后
MsgBox“不允许一个月的小数!”
如果inputData<1或inputData>12,则
MsgBox“月份编号为1到12-我该如何处理”&inputData&“?!?”
其他的
inputData1=Application.VLookup(inputData,rngLook,2,False)
如果IsError(inputData1),则
MsgBox“出现了非常非常错误的情况-我在有效月份集合中找不到该值!”
如果结束
退出Do
如果结束
环
表\u对象\u行范围(1,1).Value=inputData1
MsgBox(“感谢您花时间更新我:)”)
端接头

假设您在单元格H1:H12中有数字,请确保通过使用
wsFunc.VLookup(CInt(inputData),rngLook,2,False)
inputData
变量转换为整数。或者使用Application.InputBox而不仅仅是InputBox,并使用值为1的
Type
参数。您就是男人:)。是,该范围的信息在第1列有数字,在第2列有文本。我相信如果我把变量改成整数,这个问题是可以解决的。(a)使用
wsFunc.VLookup(CInt(inputData),rngLook,2,False)
将是最简单的解决方法-它将
inputData
的内容强制为整数,然后将与H1:H12数据匹配。(但我建议您对
InputData
进行一些有效性检查,以便在用户输入“xyz”或“13”等时给出适当的警告)。(b)使用
应用程序。类型为1的InputBox
将至少确保用户输入一个数字,但您仍应在使用前进行有效性检查。我已经更新了Cint,它工作得很好,但现在又失败了,我并没有改变任何事情。我已经更新了主要问题上的代码假设您在单元格H1:H12中有数字,请确保您使用wsFunc.VLookup(CInt(inputData),rngLook,2,False)
inputData
变量转换为整数。或者使用Application.InputBox而不仅仅是InputBox,然后使用值为1的
Type
参数。是,该范围的信息在第1列有数字,在第2列有文本。我相信如果我把变量改成整数,这个问题是可以解决的。(a)使用
wsFunc.VLookup(CInt(inputData),rngLook,2,False)
将是最简单的解决方法-它将
inputData
的内容强制为整数,然后将与H1:H12数据匹配。(但我建议您对
InputData
进行一些有效性检查,以便在用户输入“xyz”或“13”等时给出适当的警告)。(b)使用
应用程序。使用