2017-06-28 8 views
0

私はExcelでvbaを使用しています。私は2つの他の優れた点からvlookupを行い、それを現在のExcelに保存します。しかし、私はいくつかの問題に直面しています。 誰かがこれで私を助けるのに十分な親切なことができますか? ユーザー入力を使用して、2つのExcelからそれぞれ "lookup_value"と "table_array"(vlookup用)のセルアドレスを抽出しました。そして、私はvlookupを実装しており、結果を現在のExcelに貼り付けたい(これが私が問題に直面しているポイントです)。table_arrayとしてセルのアドレスを使用しているVlookup

Below is the code: 

Public Sub CommandButton4_Click() 
Dim Dept_Row As Long 
Dim Dept_Clm As Long 
Dim myFileName11 As String 
Dim E_name1 As String 
Dim E_name12 As String 
Dim aCell1 As Range 
Dim aCell12 As Range 
Dim myFileName1 As String 
Dim mySheetName1 As String 
Dim wkb1 As Workbook 
Dim sht1 As Worksheet 


Set wkb1 = Workbooks.Open("C:\Users\shashank_khanna\Desktop\extract.csv") 
wkb1.Sheets("extract").Activate 
Set sht1 = wkb1.Sheets("extract") 

E_name1 = InputBox("Enter the matching field name in the Extract.csv :") 
If Len(E_name1) > 0 Then 

Set aCell1 = sht1.Rows(1).Find(What:=E_name1, LookIn:=xlValues, _ 
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
MatchCase:=False, SearchFormat:=False) 
myFileName1 = wkb1.Name 
myFileName11 = myFileName1 
mySheetName1 = sht1.Name 


Else 
MsgBox ("You entered an invalid value") 
End If 
E_name12 = InputBox("Enter the output field name in the Extract.csv :") 
If Len(E_name12) > 0 Then 

Set aCell12 = sht1.Rows(1).Find(What:=E_name12, LookIn:=xlValues, _ 
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
MatchCase:=False, SearchFormat:=False) 

Else 
MsgBox ("You entered an invalid value") 
End If 



Dim E_name2 As String 
Dim E_name22 As String 
Dim aCell2 As Range 
Dim aCell22 As Range 
Dim myFileName2 As String 
Dim mySheetName2 As String 
Dim wkb2 As Workbook 
Dim sht2 As Worksheet 


Set wkb2 = Workbooks.Open("C:\Users\shashank_khanna\Desktop\extract2.csv") 
wkb2.Sheets("extract2").Activate 
Set sht2 = wkb2.Sheets("extract2") 

E_name2 = InputBox("Enter the matching field name in the Extract2.csv :") 
If Len(E_name2) > 0 Then 

Set aCell2 = sht2.Rows(1).Find(What:=E_name2, LookIn:=xlValues, _ 
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
MatchCase:=False, SearchFormat:=False) 

myFileName2 = wkb2.Name 
mySheetName2 = sht2.Name 

Else 
MsgBox ("You entered an invalid value") 
End If 
E_name22 = InputBox("Enter the output field name in the Extract2.csv :") 
If Len(E_name22) > 0 Then 

Set aCell22 = sht2.Rows(1).Find(What:=E_name22, LookIn:=xlValues, _ 
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
MatchCase:=False, SearchFormat:=False) 

Else 
MsgBox ("You entered an invalid value") 
End If 

Dim cellAddress As String 
Dim cellAddress1 As String 
Dim cellAddress2 As String 

Dim Table2 As Worksheet 
Dim Table1 As Range 

Workbooks("extract.csv").Activate 
'Set Table1 = wkb1.Sheets("extract").Columns(aCell1.Column).Select 
Set Table1 = Worksheets("extract").Range(aCell1.Address).End(xlDown) 

Dim CellString1 As Range 
Set CellString1 = Range(aCell2.Address) 
Dim CellString2 As Range 
Set CellString2 = Range(aCell22.Address) 
If (aCell2.Column > aCell22.Column) Then 

Workbooks("RunVlookup.xlsm").Activate 
Worksheets("Sheet1").Select 

For Each cl In Table1 

**Worksheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Value = _ 
    WorksheetFunction.VLookup("c1", 
sht2.Range(Cells(2,       aCell22.Column), 
Cells(2, aCell2.Column)), 2, False)** 
//// I am facing "error 1004 Application defined" on this line. 

Next cl 
MsgBox "Done" 
End If 


MyErrorHandler: 
If Err.Number = 1004 Then 
    MsgBox "Employee Not Present in the table." 
End If 

End Sub 

ありがとうございます。 2列のIDと名前を含む 'エキス' としてシート名 -

  1. Extract.csv:


    私は2つのワークブックを持っています。

  2. Extract2.csv - 2つの列「ID」と「名前」を含む 'extract2'としてのシート名。

私は別のRunVlookup.xlsmを持っており、抽出と抽出2のブックを参照して、RunVlookup.xlsmのSheet1で結果を得る必要があります。

これを達成する方法について私を助けてください、私が選択している検索範囲で私を修正してください。

aCell22は、Extract2.csvファイルの列「ID」を持つセルです。 aCell2はExtract2.csvファイルの列 "Name"を持つセルです。 aCell1がExtract.csvファイルの列「名前」を持つセルである。

答えて

0
WorksheetFunction.VLookup("c1", _ 
      sht2.Range(sht2.Cells(2 aCell22.Column), _ 
        sht2.Cells(2, aCell2.Column)), 2, False) 

修飾されていないCells()デフォルトactivesheetに、SHT2がアクティブでない限り、あなたのコードが失敗した。

あなたの検索範囲のみですあなたがここで意図しているものは明確ではありません。

関連する問題