0
votes

I am using vba in excel and I want to do a vlookup from two other excels and store it in the current 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. 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).

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:

  1. Extract.csv - Sheet name as 'extract' containing two columns ID and Name.
  2. Extract2.csv - Sheet name as 'extract2' containing two columns "ID" and "Name".

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.

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. aCell2 is the cell with column "Name" in Extract2.csv file. aCell1 is the cell with column 'Name" in Extract.csv file.

1

1 Answers

0
votes
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.

Your lookup range is only a single row though, so it's not clear what you intend here.