【发布时间】:2019-11-09 12:55:18
【问题描述】:
我正在尝试使用Vlookup功能,根据用户在Userform Guntest中输入的Textbox1值,自动寻找枪支的相应功能。
但是程序目前没有像它提醒我的那样运行
'运行时错误'1004',方法'范围对象'_Global'失败。
错误出现在Retrieve1=…
如果您能帮我检查问题出在哪里,我将不胜感激,因为我在使用 VBA 方面的知识和经验非常有限。
提前致谢。
看起来有些对象是未定义的,但我不知道在哪里。
模块1代码为:
Public Guncode As String
Option Explicit
Sub Test()
Call Vlookup
End Sub
Sub Vlookup()
Dim Retrieve1 As String
Dim Retrieve2 As String
Dim FinalRow As Long
Dim FinalColumn As Long
Dim WholeRange As String
If GunTest.TextBox1 = "" Then
Exit Sub
If GunTest.TextBox1 <> "" Then
MsgBox Guncode
End If
End If
With Sheets(1)
FinalRow = Range("A65536").End(xlUp).Row
FinalColumn = Range("IV1").End(xlToLeft).Column
WholeRange = "A2:" & CStr(FinalColumn) & CStr(FinalRow)
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
用户表单代码为:
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
它应该可以正常运行,但它没有。任何帮助将不胜感激,谢谢!
【问题讨论】:
-
在您的
With语句中,您需要拥有像FinalRow = .Range("A65536").End(xlUp).Row这样的范围。您需要添加“。”在您引用的范围或单元格之前 -
@ACohen 谢谢。我加了“。”只是现在,但似乎错误仍然存在
-
能否请您也更新您的问题中的代码。并确保将
.添加到代码中的所有范围内。 -
@ACohen 正在尝试,但我没有找到帖子的编辑按钮?抱歉,我是这个论坛的新手。
标签: excel vba vlookup userform