2017-06-29 11 views
0
 Dim sourcewb As Workbook 
     Dim targetWorkbook As Workbook 
     Dim filter As String 
     Dim filter2 As String 
     Dim rw As Long 
     Dim lookup As String 
     Dim X As Range 
     Dim y As Range 
     Dim a, b As Variant 

     Set sourcewb = ActiveWorkbook 
     Set X = sourcewb.Worksheets(1).Range("A:G") 
     Dim sourceSheet As Worksheet 
     Set sourceSheet = sourcewb.Worksheets(1) 
     MsgBox sourceSheet.Name 
     X.Select 

    MsgBox sourcewb.Name 

    filter = "(*.xls),*.xls" 
    Caption = "Please Select an input file " 
    Application.ScreenUpdating = False 
    Filename = Application.GetOpenFilename(filter, , Caption) 
    Set targetWorkbook = Application.Workbooks.Open(Filename) 
    Set y = targetWorkbook.Worksheets(1).Range("A:G") 
    y.Select 

    Dim targetSheet As Worksheet 
    Set targetSheet = targetWorkbook.Worksheets(1) 
    MsgBox targetSheet.Name & " This is the country code sheet name " 

    Set targetWorkbook = ActiveWorkbook 
    MsgBox targetWorkbook.Name 
    y.Select 
           sourcewb.Activate 
         MsgBox ActiveWorkbook.Name & " IS the active workbook" 

         MsgBox sourcewb.Name 

         MsgBox sourcewb.Name & " This is the source workbook " 
         MsgBox targetWorkbook.Name & " This is the target workbook " 
         MsgBox "Trying to map from target to source " 



         With sourcewb.Worksheets(1) 
         For rw= 2 To Cells(Rows.Count, 1).End(xlUp).Row 

          Cells(rw, 4) = Application.VLookup(Cells(rw, 1).Value, y, 4, False) 
          'MsgBox Cells(a, 4).Value2 
           Next rw 
         End With 


         MsgBox "All required columns from source mapped to target file " 
         Set sourcewb = ActiveWorkbook 
         MsgBox ActiveWorkbook.Name 
         Application.ScreenUpdating = False 

私はワークブックsourcewbを持っています。私はソースワークブックから別のワークブックtargetworkbookを開きます。 sourcewbの私の列のSLいいえ、国コードは私のtargetwbは、私がそこにあるようsourcewbで国コードから列をリージョンを取得しようとしていますVBA - Vlookupがテキスト参照値からフェッチしない

  country code  country name    Region  
       AL    Algeria     EMEA  
       US    USA      Americas 
       UK    United Kingdom   Europe 

で、国名

slno   country code  country name    Region  
    1    AL    Algeria      
    2    US    USA       
    3    UK    United Kingdom    

ですtargetwbのslnoと国コードの順序はsourcewbと同じではありません。

私は2042エラーが発生します。私は文字列、int、長い、バリアントと目標値を格納しようとしましたが、今まで何も働いていません。

ご意見やご協力は本当に役に立ちます。

+1

(1)あなたの代わりにVBAの式を使用していない、なぜ私が求めることができますか?単純な '= VLOOKUP()'はここで動作するはずです。 (2)[VBAのベストプラクティス:SELECTまたはACTIVATEの使用を避ける](https://stackoverflow.com/documentation/excel-vba/1107/vba-best-practices/9292/avoid-using-select- or-activate)と[ExcelでのActiveCellまたはActiveSheetの使用を避ける](https://stackoverflow.com/documentation/excel-vba/1107/vba-best-practices/7264/avoid-using-active-cell-act-activesheet-in -excel)を使用して問題を回避します。 –

答えて

1

元のコードにいくつかの「クリーンアップ」と構成を加えて、以下のコードを試してみてください。

3コメント:あなたはWith文を使用している場合は

  1. .と内部の巣にすべてのオブジェクトを忘れないでください。
  2. SelectActivateを使用しないようにする必要がないだけでなく、コードの実行時間も遅くなります。
  3. Application.VLookupが値を見つけられないシナリオをトラップする必要があり、実行時エラーが発生します。

コメント内のコード内の説明。

コード

Option Explicit 

Sub AutoVLookup() 

Dim sourcewb As Workbook 
Dim targetWorkbook As Workbook 
Dim sourceSheet As Worksheet 
Dim targetSheet As Worksheet 
Dim X As Range 
Dim y As Range 

Dim filter As String 
Dim filter2 As String 
Dim rw As Long 
Dim lookup As String 
Dim a, b As Variant 

Set sourcewb = ActiveWorkbook ' set Activeworkbook object 
Set sourceSheet = sourcewb.Worksheets(1) ' set source sheet 
Set X = sourceSheet.Range("A:G") ' set source range 

filter = "(*.xls),*.xls" 
Caption = "Please Select an input file " 
Application.ScreenUpdating = False 
Filename = Application.GetOpenFilename(filter, , Caption) 

Set targetWorkbook = Workbooks.Open(Filename) ' set target workbook object 
Set targetSheet = targetWorkbook.Worksheets(1) ' set target sheet 
Set y = targetSheet.Range("A:G") ' set target range 

With sourceSheet 
    For rw = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row ' get last row in column A 
     ' make sure VLoookup found a match, otherwise you will get a run-time error 
     If Not IsError(Application.VLookup(.Cells(rw, 1).Value, y, 4, False)) Then 
      .Cells(rw, 4) = Application.VLookup(.Cells(rw, 1).Value, y, 4, False) ' this will fetch column "E" values 
      'MsgBox Cells(a, 4).Value2 
     End If 
    Next rw 
End With 

MsgBox "All required columns from source mapped to target file " 

Application.ScreenUpdating = True 

End Sub 
+0

こんにちは@シャイラドー、あなたの説明をありがとう。私はプログラムを少しきれいにすることについて学んだ。残念なことに、これは私自身の関心事なので、私は書籍よりも多くのインターネットを参照するマクロを書いています。私は改善しようとします。出力に来ると、私は.Cells、.Rowsなどを追加しました。残念ながら、国コードを参照すると空白が返ってきて、他の列をフェッチするのは残念です。 –

関連する問題