使用 VLookup 和命名范围设置变量

Setting Variable with VLookup & Named Ranges

使用命名范围时,我无法使 VLookup 函数工作。我确信这与我引用的方式有关 "COA_Range" 但找不到有效的解决方案

我试过[], ([]), (""), [""],([""])......

(下面是代码的更新和扩展部分)

If Transaction_Type = 1 Then
    Debug.Print "Transaction Type :"; Transaction_Type
    Range("n10").Value = "Income"

    Debug.Print "COA # = "; TransactionInfo.Income_COA_Number.Value
    COA_Number = TransactionInfo.Income_COA_Number.Value
    Debug.Print COA_Number

    Range("n12").Value = TransactionInfo.Income_COA_Number.Value

    'thought from STACK OVERFLOW
    Debug.Print Range("COA_Range").Address()

    COA_1 = Application.WorksheetFunction.VLookup(COA_Number, Range("COA_Range"), 2, False)
    Debug.Print COA_1
    Range("n13").Value = COA_1

尝试使用Application.Vlookup而不是使用Application.WorksheetFunction.Vlookup.然后设置一个 Variant 等于这个,如果没有找到匹配,那么它将 return 错误 2042 可以使用 IsError

进行测试

参见下面的示例代码:

Dim ws As Worksheet: Set ws = ActiveSheet            'Change the sheet reference appropriately
Dim rngLookUpTable As Range: Set rngLookUpTable = ws.Range("COA_Range")    
Dim vReturn As Variant

If Transaction_Type = 1 Then
    Range("N10").Value = "Income"
    COA_Number = TransactionInfo.Income_COA_Number.Value
    Range("N12").Value = TransactionInfo.Income_COA_Number.Value
    vReturn = Application.VLookup(COA_Number,rngLookUpTable, 2, False)
    Range("N13").Value = IIF(IsError(vReturn),"Not Found",vReturn)
End If

WorksheetFunction 版本的 VLookup 和 Match 需要错误处理,将您的代码重新路由到错误处理程序,returns 到下一个要评估的语句,等等。使用应用程序函数,您可以避免这种混乱.

在@Jeeped 评论之后,确保你的 User_Form 中的值在文本框“Income_COA_Number 中命名为“TransactionInfo" 有一个数值,因此 Range("COA_Range") 单元格中的所有值。

我添加了 2 个可选解决方案(选择您喜欢的一个):

  • 使用 Application.Match.
  • 使用 Find.

代码

Option Explicit

Sub VLookUpNamedRange()

Dim ws                  As Worksheet
Dim Transaction_Type    As Long
Dim MyCOARng            As Range
Dim COA_1               As Variant
Dim COA_Number          As Long
Dim FndRng              As Range

Set ws = Worksheets("Sheet7") '<-- modify "Sheet7" to your sheet's name    
Set MyCOARng = ws.Range("COA_Range") '<-- set Range to "COA_Range" Named Range

COA_Number = TransactionInfo.Income_COA_Number.Value

' === Option 1: Use Application.Match ===
If Not IsError(Application.VLookup(COA_Number, MyCOARng, 2, False)) Then ' <-- VLookup Successful
    COA_1 = Application.VLookup(COA_Number, MyCOARng, 2, False)
Else ' <-- VLookup failed
    COA_1 = COA_Number & " Not found in 'COA_Range' "
End If

' === Option 2: Use Find ===
Set FndRng = MyCOARng.Find(What:=COA_Number, LookIn:=xlValues, lookat:=xlWhole)
If Not FndRng Is Nothing Then '<-- successful find
    COA_1 = FndRng.Offset(, 2).Value
Else '<-- not found in your range
    COA_1 = COA_Number & " Not found in 'COA_Range' "
End If

Debug.Print COA_1 ' <-- for DEBUG Only

End Sub

特别感谢@jainashish、@Shai Rado 的周到回复。我能够从每个人那里得到一些建议。

不过是@Jeeped 真正解决了我的问题。 "number" 被读取为文本,而 CLng() 表达式对我有用。我在下面添加了更新的代码。

    If Transaction_Type = 1 Then
    Debug.Print "Transaction Type :"; Transaction_Type
        Range("n10").Value = "Income"

        'thought from Whosebug
            'need to make sure that the number taken fromt the userform is ACTUALLY a number and not text that looks like a number
                'use CLng to convert

    Debug.Print "COA # = "; CLng(TransactionInfo.Income_COA_Number.Value)
        COA_Number = CLng(TransactionInfo.Income_COA_Number.Value)
            Debug.Print "COA # = "; COA_Number
        Range("n12").Value = COA_Number

            'thought from STACK OVERFLOW
                Debug.Print Range("COA_Range").Address()
                    'Yes the range is being found...
                        Dim COA_Range As Range
                        Set COA_Range = Range("COA_Range")
                            Debug.Print COA_Range.Address()

        COA_1 = WorksheetFunction.VLookup(COA_Number, Range("COA_Range"), 2, False)
                Debug.Print COA_1
                    Range("n13").Value = COA_1

        COA_2 = WorksheetFunction.VLookup(COA_Number, Range("COA_Range"), 3, False)
                Debug.Print COA_2
                    Range("n14").Value = COA_2

        COA_3 = WorksheetFunction.VLookup(COA_Number, Range("COA_Range"), 4, False)
                Debug.Print COA_3
                    Range("n15").Value = COA_3

        COA_4 = WorksheetFunction.VLookup(COA_Number, Range("COA_Range"), 5, False)
                Debug.Print COA_4
                    Range("n16").Value = COA_4enter code here