2017-02-13 4 views
0

Vlookupでユーザーフォームを作成したいと思います。情報は別のブックに格納されます。次のVBAコードを使用して、別のExcelブックから必要な情報をテキストフィールドに抽出するにはどうすればよいですか?vlookup別のExcelブックの情報を抽出する

Private Sub Textan_AfterUpdate() 

'check to see if value exists   
If WorksheetFunction.CountIf(C:\Users\poury\Desktop\ADDON Order Tool\AL010.xlsx.Sheet2.Range("B:B"), Me.Textan.Value) = 0 Then 
    MsgBox "This is an incorrect Article Number" 
    Me.Textan.Value = ""  
    Exit Sub 
End If 

With Me  
    Textan1 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 2, 0) 
    Textan2 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 3, 0) 
    Textan3 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 4, 0) 
    Textan4 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 5, 0) 
    Textan5 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 6, 0) 
    Textan6 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 7, 0) 
    Textan7 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 8, 0) 
    Textan8 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 9, 0)  
End With 

End Sub 

答えて

1

ただ、舞台裏ブックを開く:

Private Sub Textan_AfterUpdate() 

Application.Screenupdating = false 

Dim wb as Workbook 
Set wb = Workbooks.Open("C:\Users\poury\Desktop\ADDON Order Tool\AL010.xlsx") 

Dim Sheet2 as Worksheet 
Set Sheet2 = wb.Worksheets("Sheet2") 'change name as needed 

'check to see if value exists   
If WorksheetFunction.CountIf(Sheet2.Range("B:B"), Me.Textan.Value) = 0 Then 
    MsgBox "This is an incorrect Article Number" 
    Me.Textan.Value = ""  
    Exit Sub 
End If 

With Me  
    Textan1 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 2, 0) 
    Textan2 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 3, 0) 
    Textan3 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 4, 0) 
    Textan4 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 5, 0) 
    Textan5 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 6, 0) 
    Textan6 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 7, 0) 
    Textan7 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 8, 0) 
    Textan8 = Application.WorksheetFunction.VLookup(CLng(Me.Textan), Sheet2.Range("Lookup"), 9, 0)  
End With 

wb.Close false 

End Sub 
1

あなたはあなたのコードをリファクタリングして

を取得するために、あなたのテキストボックスの名前と「検索」の範囲の列間の関係のようなものを利用することができ
Private Sub Textan_AfterUpdate() 
    Dim rowIndex as Variant 

    Application.Screenupdating = False 

    With Workbooks.Open("C:\Users\poury\Desktop\ADDON Order Tool\AL010.xlsx").Worksheets("Sheet2").Range("Lookup") '<--| open needed workbook and reference its "Sheet2" "Lookup" range (change "Sheet2" to your actual sheet name) 
     rowIndex = Application.Match(Me.Textan.Value, .Columns(1), 0) '<--| try searching "Lookup" range first column for 'Textan' value 
     If IsError(rowIndex) Then 'check to see if value exists 
      MsgBox "This is an incorrect Article Number" 
      Me.Textan.Value = ""     
     Else 
      For iText = 1 to 8 
       Me.Controls("Textan" & iText) = .Cells(rowIndex, iText+ 1) 
      Next 
     End If 
    End With 
    ActiveWorkbook.Close False '<--| close opened workbook 
    Application.Screenupdating = True 
End Sub 
+0

ありがとうございました。それは私の問題を解決しました –

+0

あなたは大歓迎です。受け入れられた回答をマークすることができます。ありがとうございます – user3598756

+0

@PouryaAshena https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235 – 0m3r

関連する問題