用VLookup和命名范围设置variables

使用命名区域时无法使VLookup函数正常工作。 我相信它与我如何引用"COA_Range"但找不到"COA_Range"的解决scheme

我努力了 [], ([]), (””), [””],([””])……

以下是代码的更新和扩展部分

 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 

在@Jeeped注释之后,请确保文本框“ Income_COA_Number ”中名为“ TransactionInfo ”的User_Form中的值具有一个数值,因此Range("COA_Range")单元格中的所有值都具有该数值。

Iv'e添加了2个可选的解决scheme(select一个你preffer):

  • 使用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 

尝试使用Application.Vlookup而不是使用Application.WorksheetFunction.Vlookup 。 然后设置一个variables等于这个,如果没有find匹配,那么它将返回错误2042,可以使用IsError进行testing

请参阅下面的示例代码:

 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版本需要error handling,将您的代码重新路由到error handling程序,返回到下一个语句进行评估等等。使用应用程序function,可以避免混乱。

特别感谢@jainashish,@Shai Rado的深思。 我能够从每一个拿起几个指针。

然而,这是@Jeeped谁实际上解决了我的问题。 “数字”正在被读取为文本和CLng()expression式为我工作。 我在下面添加了我的更新代码。

  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