2017-02-07 7 views
0

VBAの新機能 - Excel内でVlookup関数を記述して、lookup_valueを値の範囲として参照しようとしています。私はlookup_rangeと呼ばれる範囲に自分の値を格納していますが、vlookupの値をセルに出力するとN/Aになります。vlookup VBAでlookup_valueを範囲として記述する方法

私はインターネット上で検索しましたが、私の質問には解決策がありません。

Sub Vlookup_values() 

    Dim Vlkp_cell, n As Double 
    Dim Search_range, row_end, col_end As Double 

    Sheets("Movement").Activate 
    Range("A1").End(xlDown).Select 
    Vlkp_cell = ActiveCell.Row 

    ReDim Lookup_range(Vlkp_cell) As String 
    n = 0 
    Range("A1").Select 
    For n = 1 To Vlkp_cell 
     ActiveCell.Offset(1, 0).Select 
     Lookup_range(n) = ActiveCell.Value 
     ActiveCell.Offset(0, 9).Value = Lookup_range(n) 
    Next n 

    Sheets("Mapping").Activate 
    Range("A1").End(xlDown).Select 
    row_end = ActiveCell.Row 

    Range("A1").End(xlToRight).Select 
    col_end = ActiveCell.Column 

    Range(Cells(1, 1), Cells(row_end, col_end)).Name = "Search_range" 
    Range("Search_Range").Select 

    Sheets("Movement").Activate 
    Range("A1").Select 
    ActiveCell.Offset(0, 1).Select 
    n = 0 
    For n = 1 To Vlkp_cell 

     ActiveCell.Offset(1, 0).Select 
     ActiveCell.Value = Application.VLookup(Lookup_range(n), Search_range, 2, False) 

    Next n 


End Sub 

答えて

0

私はあなたのコードから達成しようとしているかを理解することができた場合は、下記のコードを試してください:ifeelcrazy123はあなたが私の答えでコードをテストしている@

Option Explicit 

Sub Vlookup_values() 

    Dim Vlkp_cell As Long, n As Long 
    Dim Search_range As Range, row_end As Long, col_end As Long 

    With Sheets("Mapping") 
     row_end = .Range("A1").End(xlDown).Row 
     col_end = .Range("A1").End(xlToRight).Column 

     Set Search_range = .Range(.Cells(1, 1), .Cells(row_end, col_end)) ' <-- set the Range for the VLookup 
    End With 

    With Sheets("Movement") 
     Vlkp_cell = .Range("A1").End(xlDown).Row 

     For n = 1 To Vlkp_cell 
      .Range("A" & n).Offset(0, 9).Value = .Range("A" & n).Value ' <-- not sure why you need the values copied 9 column to the right ? 
      .Range("B" & n).Value = Application.VLookup(.Range("A" & n).Value, Search_range, 2, False) 
     Next n 
    End With 

End Sub 
+0

を? –

+0

ありがとうございました。それはうまくいった! (返事が遅れて申し訳ありません)。 FYI、前回の8つのデータが他のデータで満たされているため、私は右の値の9桁の値を必要としました。このデータは後で比較し、後で財務システムに提出する必要があります。ありがとうございました! – ifeelcrazy123

関連する問題