[英]Vlookup using the cell address as table_array
I am using vba in excel and I want to do a vlookup from two other excels and store it in the current excel. 我在excel中使用vba,我想从另外两个excel中进行vlookup并将其存储在当前excel中。 But I am facing some issue. 但是我面临一些问题。 Could anyone be kind enough to help me out in this? 有人能在这方面帮助我吗? I have extracted the cell address for "lookup_value" and "table_array" (for the vlookup) from the two excels respectively by using the user input. 我通过使用用户输入分别从两个Excel中提取了“ lookup_value”和“ table_array”(对于vlookup)的单元格地址。 And then I am implementing the vlookup and want to paste the result in the current excel(this is the point at which I am facing the issue). 然后我要实现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
Thank you. 谢谢。
I have two workbooks: 我有两本工作簿:
I have another excel RunVlookup.xlsm and I need to do the look up from extract and extract2 workbooks and have the result on Sheet1 of RunVlookup.xlsm. 我还有另一个Excel RunVlookup.xlsm,我需要从extract和extract2工作簿中进行查找,并将结果显示在RunVlookup.xlsm的Sheet1上。
Could you please help me out on how to achieve this and correct me on the lookup range I am selecting. 您能否帮我解决这个问题,并根据我选择的查询范围对我进行更正。
aCell22 is the cell with column "ID" in Extract2.csv file. aCell22是在Extract2.csv文件中具有“ ID”列的单元格。 aCell2 is the cell with column "Name" in Extract2.csv file. aCell2是Extract2.csv文件中带有“名称”列的单元格。 aCell1 is the cell with column 'Name" in Extract.csv file. aCell1是Extract.csv文件中具有“名称”列的单元格。
WorksheetFunction.VLookup("c1", _
sht2.Range(sht2.Cells(2 aCell22.Column), _
sht2.Cells(2, aCell2.Column)), 2, False)
An unqualified Cells()
defaults to the activesheet, so your code fails unless sht2 is active. 不合格的Cells()
默认为活动表,因此除非sht2处于活动状态,否则您的代码将失败。
Your lookup range is only a single row though, so it's not clear what you intend here. 但是,您的查询范围仅是一行,因此尚不清楚您打算在这里做什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.