使用命名范围时,我无法使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
答案 0 :(得分:1)
关注@Jeeped评论后,请确保User_Form中的值名为" TransactionInfo "在TextBox" Income_COA_Number "具有数值,因此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
答案 1 :(得分:0)
尝试使用 Application.Vlookup ,而不是使用 Application.WorksheetFunction.Vlookup 。然后将Variant设置为等于此,如果未找到匹配,则返回Error 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需要错误处理,将代码重新路由到错误处理程序,返回到要评估的下一个语句,等等。使用Application函数,可以避免这种混乱。
答案 2 :(得分:0)
特别感谢@jainashish,@ Shai Rado的深思熟虑的回应。我能够从每个指针中找到一些指针。
然而@Jeeped究竟解决了我的问题。 &#34;数字&#34;被读作文本,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