【问题标题】:Run time error '1004' Application defined or object defined error on Vlookup functionVlookup 函数上的运行时错误“1004”应用程序定义或对象定义错误
【发布时间】: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


【解决方案1】:

首先,您传入了一个数字作为列字母值。 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

1.我不确定使用 Address(True, False) 作为行号的原因是什么。

这来自这两个功能的组合。真/假设置告诉函数在地址中使用/不使用绝对引用。

拆分(表达式 [,delimiter] [,limit] [,compare] ) https://www.techonthenet.com/excel/formulas/split.php

expression.Address(RowAbsolute、ColumnAbsolute、ReferenceStyle、External、RelativeTo) https://docs.microsoft.com/en-us/office/vba/api/excel.range.address

Cell (1, FinalColumn) 不应该代表列号吗?

不,单元格函数基本上返回行和列的交集/地址。

试试这个例子:debug.Print; thisworkbook.Sheets("Sheet1").Cells(2,2)

您提到 CSTR 不会神奇地转换为等效字母,那么它会转换成什么?您能否进一步详细说明?

这是一个数据类型转换函数。 CSTR(666) 本质上是这样做的:这个 666 变成了这个“666”

2. vArr(0)。我对括号中的参数 0 代表什么感到困惑。实际上,这是我一直对参数规范提出的一般性问题。

这是一个数组位置引用。 split 函数返回一个字符串数组。由于我们用于捕获列标签值,因此我们只需要引用第一个位置。

(3) 我尝试复制您的代码并运行它,但仍然在同一行提醒我错误。

对我来说很好,除非没有返回值,这会返回一个错误,这就是我所说的“炸弹”。

【讨论】:

  • 首先感谢您的回答。然而,仍有一些令人困惑的地方。
  • 首先感谢您的回答。但是,仍然有一些令人困惑的地方。 1. 我不确定使用 Address(True, False) 作为行号的原因是什么。 Cell (1, FinalColumn) 不应该代表列号吗?您提到 CSTR 不会神奇地转换为等效字母,那么它会转换为什么?你能进一步详细说明吗? 2. vArr(0)。我对括号中的参数 0 代表什么感到困惑。实际上,这是我关于参数规范的一个普遍问题。 (3) 我尝试复制你的代码并运行它,但仍然在同一行提醒我错误。
  • 我也不明白“方法会爆炸”是什么意思。我的程序中似乎没有循环。 @Doug Coats
  • 我再次复制你的代码并运行它,现在它提醒“无法获取工作表函数类的vlookup属性”。我应该删除代码中的“worksheetfunction”吗? @Doug Coats
  • @anctorHu 不,这就是您在找不到值时收到的错误消息。我确实告诉过你,你必须通过自定义错误处理来解决这个问题
猜你喜欢
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2016-01-14
相关资源
最近更新 更多