2017-03-22 96 views
1

使用命名範圍時,我無法使VLookup函數正常工作。我確信它與我如何參考"COA_Range"有關,但無法找到可行的解決方案使用VLookup和命名範圍設置變量

我試過[],([]),(「」),[「」],([「」 ])......

下面是代碼

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

什麼是'TransactionInfo.Income_COA_Number'?你也可以通過'Thisworkbook.Names(「COA_Range」)。RefersToRange'來引用命名範圍。 – PatricK

+0

它是從用戶窗體中獲取的值。 TransactionInfo.Income_COA_Number.Value貫穿我的宏的其餘部分。您的「thisworkbook.names ...」不成功 –

+2

嘗試添加'Debug.Print Range(「COA_Range」).Address()'以確保找到範圍。 –

回答

0

特別感謝@jainashish,@Shai瑞士雷達表的深思熟慮的迴應。我能夠從中挑選出幾個指標。

這是@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 
0

嘗試的更新和擴展部分使用Application.Vlookup而不是使用Application.WorksheetFunction .Vlookup。然後設置等於一個變種這一點,如果沒有找到匹配,則它會返回錯誤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和匹配的版本需要錯誤處理將代碼重新路由到錯誤處理程序,返回到下一個語句進行評估等。使用應用程序功能,可以避免混亂。

+0

謝謝你的迴應!我在 –

1

繼@Jeeped評論,確保名爲「TransactionInfo」,在文本框「Income_COA_Number」你User_Form值在Range("COA_Range")細胞有一個數值,所以所有的值。

Iv'e加2級可選的解決方案(接你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 
+0

以下發布了我的確切解決方案謝謝您的回覆!我在下面發佈了我的確切解決方案 –