Vlookup使用单元格地址作为table_array

时间:2017-06-28 06:29:09

标签: vba excel-vba excel

我在excel中使用vba,我想从另外两个excel中进行vlookup并将其存储在当前的excel中。但我面临一些问题。 有人可以帮助我解决这个问题吗? 我已经提取了" lookup_value"的单元格地址。和" table_array" (对于vlookup)分别通过使用用户输入从两个excel中进行。然后我正在实现vlookup并希望将结果粘贴到当前的excel中(这是我面临问题的那一点)。

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

谢谢。

我有两本工作簿:

  1. Extract.csv - 工作表名称为'提取'包含两列ID和名称。
  2. Extract2.csv - 工作表名称为' extract2'包含两列" ID"和"姓名"。
  3. 我有另一个excel RunVlookup.xlsm,我需要从extract和extract2工作簿中查找并在RunVlookup.xlsm的Sheet1上得到结果。

    你能帮我解决一下如何实现这个目标,并在我选择的查找范围上纠正我。

    aCell22是具有列" ID"在Extract2.csv文件中。 aCell2是具有列"名称"在Extract2.csv文件中。 aCell1是具有列' Name"在Extract.csv文件中。

1 个答案:

答案 0 :(得分:0)

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

非限定Cells()默认为活动表,因此除非sht2处于活动状态,否则您的代码将失败。

你的查找范围只有一行,所以目前还不清楚你的意图。