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

问题描述 投票:0回答:3

使用命名范围时,我无法使用

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
vba excel vlookup named-ranges
3个回答
1
投票

在@Jeeped 评论之后,确保文本框“Income_COA_Number”中名为“TransactionInfo”的 User_Form 中的值具有数字值,因此

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

0
投票

尝试使用 Application.Vlookup 而不是使用 Application.WorksheetFunction.Vlookup。然后将 Variant 设置为等于此,如果未找到匹配项,则它将返回错误 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

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


0
投票

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

然而,@Jeeped 真正解决了我的问题。 “数字”被读取为文本,而 CLng() 表达式对我有用。我在下面添加了更新的代码。

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

        'thought from STACKOVERFLOW
            '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
© www.soinside.com 2019 - 2024. All rights reserved.