Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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 运行时错误';1004';Vlookup函数上的应用程序定义或对象定义错误_Excel_Vba_Vlookup_Userform - Fatal编程技术网

Excel 运行时错误';1004';Vlookup函数上的应用程序定义或对象定义错误

Excel 运行时错误';1004';Vlookup函数上的应用程序定义或对象定义错误,excel,vba,vlookup,userform,Excel,Vba,Vlookup,Userform,我试图利用Vlookup功能,根据Textbox1用户在Userform Guntest中输入的值,自动查找枪的相应功能。 然而,该程序目前并没有像它提醒我的那样运行 '运行时错误'1004',方法'Range of object'\u Global'失败 错误出现在Retrieve1=… 如果您能帮助我检查问题所在,我将不胜感激,因为我在使用VBA方面的知识和经验非常有限 提前谢谢 看起来有些对象没有定义,但我不知道在哪里 模块1代码为: Public Guncode As String Op

我试图利用
Vlookup
功能,根据
Textbox1
用户在
Userform Guntest
中输入的值,自动查找枪的相应功能。 然而,该程序目前并没有像它提醒我的那样运行

'运行时错误'1004',方法'Range of object'\u Global'失败

错误出现在
Retrieve1=…

如果您能帮助我检查问题所在,我将不胜感激,因为我在使用VBA方面的知识和经验非常有限

提前谢谢

看起来有些对象没有定义,但我不知道在哪里

模块1代码为:

Public Guncode As String
Option Explicit

Sub Test()

    Call Vlookup

End Sub


它应该能正常运行,但不能。任何帮助都将不胜感激,谢谢

首先,您要传入一个数字作为列字母值。CSTR()不会神奇地将其转换为等效的字母,但我喜欢你的热情

第二,如果找不到值,您的方法将爆炸-因此您需要为其编写自己的错误处理

Sub Vlookup()
    Dim Retrieve1 As String
    Dim Retrieve2 As String
    Dim FinalRow As Long
    Dim FinalColumn As Long
    Dim WholeRange As String
    Dim vArr
    Dim col_Letter As String

    If GunTest.TextBox1 = "" Then
        Exit Sub
        If GunTest.TextBox1 <> "" Then
            MsgBox Guncode
        End If
    End If

    With ThisWorkbook.Sheets("Sheet1")
        FinalRow = .Range("A65536").End(xlUp).Row
        FinalColumn = .Range("IV1").End(xlToLeft).Column
        vArr = Split(Cells(1, FinalColumn).Address(True, False), "$")
        col_Letter = vArr(0)
        WholeRange = "A2:" & col_Letter & CStr(FinalRow) '<---- you were passing a number in as the column value
        Retrieve1 = Application.WorksheetFunction.Vlookup(Trim(Guncode), .Range(WholeRange), 1, False) 'Locate specific tool according to QR code number
        Retrieve2 = Application.WorksheetFunction.Vlookup(Trim(Guncode), .Range(WholeRange), 5, False) 'Locate specific gun type according to QR code number

        If Guncode = "" Then
            MsgBox "This gun doesn't exist in database!"
        Else
            MsgBox "The tool number is:" & Retrieve1 & vbCrLf & "The gun type is:" & Retrieve2
        End If
    End With
End Sub
Sub-Vlookup()
Dim Retrieve1作为字符串
Dim Retrieve2作为字符串
暗淡的最后一行
模糊的最后一栏
像细绳一样的模糊的杂音
暗变
将字母变暗为字符串
如果GunTest.TextBox1=“”,则
出口接头
如果GunTest.TextBox1为“”,则
MsgBox代码
如果结束
如果结束
使用此工作簿。工作表(“工作表1”)
FinalRow=.Range(“A65536”).End(xlUp).Row
FinalColumn=.Range(“IV1”).End(xlToLeft).Column
vArr=Split(单元格(1,FinalColumn).地址(真,假),“$”)
col_字母=vArr(0)

WholeRange=“A2:”&col_Letter&CStr(FinalRow)”在您的
With
语句中,您需要将您的范围设置为so
FinalRow=.Range(“A65536”).End(xlUp).Row
。您需要在引用的范围或之前添加“.”cells@ACohen谢谢我加了一个“.”刚才好像还是有错误,你可以在问题中更新你的代码吗。。并确保您已将
添加到代码中的所有范围中。@A当时正在尝试,但我找不到帖子的“编辑”按钮?对不起,我是这个论坛的新手。首先谢谢你的回答。不过还是有一些地方让人困惑。首先谢谢你的回答。然而,仍有一些地方令人困惑。1.我不确定使用地址(真、假)作为行号的原因是什么。单元格(1,FinalColumn)不应该代表列号吗?你们提到CSTR不会神奇地转换成字母等价物,那个么它会转换成什么呢?你能再详细说明一下吗?2.vArr(0)。我对括号中的参数0代表什么感到困惑。实际上,这是一个关于参数规范的一般性问题。(3) 我试着复制你的代码并运行它,但仍然在同一行提醒我错误。我也不明白你所说的“方法会爆炸”是什么意思。我的程序好像没有循环@Doug CoatsI再次复制了您的代码并运行了它,现在它提醒“无法获取worksheetfunction类的vlookup属性”。我应该删除代码中的“工作表功能”吗@道格Coats@anctorHu不,这是找不到值时收到的错误消息。我确实告诉过你,你必须用自定义错误处理来解释这一点
Option Explicit

Private Sub Label1_Click()

End Sub

Private Sub CommandButton1_Click()

    If TextBox1 = "" Then Exit Sub 'Set condition 1 of exiting the program

    Guncode = GunTest.TextBox1

    With Me

        Call Module1.Test

    End With

End Sub

Private Sub PartID_Click()

End Sub

Private Sub TextBox1_Change()

End Sub

Private Sub UserForm_Click()

End Sub
Sub Vlookup()
    Dim Retrieve1 As String
    Dim Retrieve2 As String
    Dim FinalRow As Long
    Dim FinalColumn As Long
    Dim WholeRange As String
    Dim vArr
    Dim col_Letter As String

    If GunTest.TextBox1 = "" Then
        Exit Sub
        If GunTest.TextBox1 <> "" Then
            MsgBox Guncode
        End If
    End If

    With ThisWorkbook.Sheets("Sheet1")
        FinalRow = .Range("A65536").End(xlUp).Row
        FinalColumn = .Range("IV1").End(xlToLeft).Column
        vArr = Split(Cells(1, FinalColumn).Address(True, False), "$")
        col_Letter = vArr(0)
        WholeRange = "A2:" & col_Letter & CStr(FinalRow) '<---- you were passing a number in as the column value
        Retrieve1 = Application.WorksheetFunction.Vlookup(Trim(Guncode), .Range(WholeRange), 1, False) 'Locate specific tool according to QR code number
        Retrieve2 = Application.WorksheetFunction.Vlookup(Trim(Guncode), .Range(WholeRange), 5, False) 'Locate specific gun type according to QR code number

        If Guncode = "" Then
            MsgBox "This gun doesn't exist in database!"
        Else
            MsgBox "The tool number is:" & Retrieve1 & vbCrLf & "The gun type is:" & Retrieve2
        End If
    End With
End Sub