VBA coding import data 1 excel file to other excel file using vlookup, FSO etc...
Hi Nick,
Question: Two excel files are there i.e 1 raw excel file & 1 master excel file. In
both the excel files one common column is "F". To extract data from raw file
to master file using common column i.e Column name is "F"
Requirement: We have to select folder & file location and then using
vlookup extarct data common column "f" of master file to common column "F" of
raw file as per requirmenet of selected columns i.e Column name is N,O,P,Q,R,S,T,U &
V
Hope you understand my requirement.
Regards,
Trimal
»
- trimalakumar's blog
- Login or register to post comments
- 31233 reads
I'm sure there are better ways but.....
Try this:
It assumes the list in the master file starts at cell F2 and is terminated with an empty cell.
I used integers in my test sheet, hence converting "myLookup" to an integer before matching it
Sub trimalakumar()
Dim FileToOpen As String
Dim MasterFile As String
Dim RawFile As String
Dim myLookup As String
Dim myIndex As Variant
Dim i As Integer
FileToOpen = Application.GetOpenFilename("All files (*.*), *.*", , "Select Master File")
If FileToOpen = "False" Then Exit Sub
Workbooks.Open FileToOpen
MasterFile = ActiveWorkbook.Name
FileToOpen = Application.GetOpenFilename("All files (*.*), *.*", , "Select Raw File")
If FileToOpen = "False" Then Exit Sub
Workbooks.Open FileToOpen
RawFile = ActiveWorkbook.Name
i = 2
myLookup = Workbooks(MasterFile).Worksheets(1).Cells(i, 6)
While myLookup <> ""
On Error Resume Next
Err.Clear
myIndex = Application.Match(CInt(myLookup), Workbooks(RawFile).Worksheets(1).Range("F:F"), False)
If Err.Number = 0 Then
''''''''''''''''''''''''''''''''
' Value was found. Grab your raw data and store it somewhere
''''''''''''''''''''''''''''''''
Workbooks(MasterFile).Worksheets(1).Cells(i, 7) = Workbooks(RawFile).Worksheets(1).Cells(myIndex, 14)
Else
''''''''''''''''''''''''''''''''
' Value was not found. Error code goes here.
''''''''''''''''''''''''''''''''
End If
i = i + 1
myLookup = Workbooks(MasterFile).Worksheets(1).Cells(i, 6)
Wend
Workbooks(MasterFile).Close
Workbooks(RawFile).Close
End Sub
VBA coding for lookup for more columns..
Hi. Thanks...i m trying for same...
Please help me VBA Coding for vba lookup from master file of "F" Column to Raw file "F" column w.r.t Column N,O,P,Q,R,S,T,U,V etc.... Once Vlookup is done 1st raw file and then it will ask for another raw file to continue lookup the same manner for 4-5 raw file.
Please help it is ugent... Please help it possible for chating if any doubt....
Regards,
with multiple raw files
This code will continually ask for new raw files until you hit cancel. It will record the contents of columns N,O,P,Q,R,S,T,U,V into column G (and then H etc for subsequent raw files).
Sub trimalakumar()
Dim FileToOpen As String
Dim MasterFile As String
Dim RawFile As String
Dim myLookup As String
Dim myIndex As Variant
Dim i As Integer
Dim j As Integer
FileToOpen = Application.GetOpenFilename("All files (*.*), *.*", , "Select Master File")
If FileToOpen = "False" Then Exit Sub
Workbooks.Open FileToOpen
MasterFile = ActiveWorkbook.Name
FileToOpen = Application.GetOpenFilename("All files (*.*), *.*", , "Select Raw File")
j = 0
While FileToOpen <> "False"
Workbooks.Open FileToOpen
RawFile = ActiveWorkbook.Name
i = 2
myLookup = Workbooks(MasterFile).Worksheets(1).Cells(i, 6)
While myLookup <> ""
Debug.Print myLookup
On Error Resume Next
Err.Clear
myIndex = Application.Match(CInt(myLookup), Workbooks(RawFile).Worksheets(1).Range("F:F"), False)
If Err.Number = 0 Then
''''''''''''''''''''''''''''''''
' Value was found. Grab your raw data and store it somewhere
''''''''''''''''''''''''''''''''
Workbooks(MasterFile).Worksheets(1).Cells(i, 7 + j) = Workbooks(RawFile).Worksheets(1).Cells(myIndex, 14) & _
Workbooks(RawFile).Worksheets(1).Cells(myIndex, 14) & _
Workbooks(RawFile).Worksheets(1).Cells(myIndex, 15) & _
Workbooks(RawFile).Worksheets(1).Cells(myIndex, 16) & _
Workbooks(RawFile).Worksheets(1).Cells(myIndex, 17) & _
Workbooks(RawFile).Worksheets(1).Cells(myIndex, 18) & _
Workbooks(RawFile).Worksheets(1).Cells(myIndex, 19) & _
Workbooks(RawFile).Worksheets(1).Cells(myIndex, 20) & _
Workbooks(RawFile).Worksheets(1).Cells(myIndex, 21)
Else
''''''''''''''''''''''''''''''''
' Value was not found. Error code goes here.
''''''''''''''''''''''''''''''''
End If
i = i + 1
myLookup = Workbooks(MasterFile).Worksheets(1).Cells(i, 6)
Wend
Workbooks(RawFile).Close
j = j + 1
FileToOpen = Application.GetOpenFilename("All files (*.*), *.*", , "Select Raw File")
Wend
Workbooks(MasterFile).Close
End Sub
Vlookup output not coming..
Thank you....
One more thing vlookup is not happening from Master file column "F" to Raw file "F" column w.r.t N,O,P,Q,R,S,T,U,V etc...
Please suggest for more....
MyVLookup = Workbooks(MasterFile).Worksheets(1).Cells(i, 6)
Not using vlookup
The code does not use Vlookup at all, instead it uses the match function to return the matching row in the raw file. To return 9 columns of data would need 9 vlookups which would be inefficient IMHO.
vba code pending...plz help
Thank You...
My Requirement vlookup from "F" column of master file to "F" column of Raw file w.r.t raw file column i.e N,O,P,Q,R,S,T,U,V etc as it continue 1 by 1 raw file (upto 7 raw file) and finally if raw file not found it complete program.
Note: 1 Master file and 7-8 Raw file. Common column in all file is "F Column" and lookup from "F column of master file" column respact to "F" column of all Raw file of column i.e N,O,P,Q,R,S,T,U,V etc...
Hope your got my point and please help asap.
Thanks...
Unclear
Really don't understand what else you need - where are you getting the raw file names from?
VBA coding...
Hi,
My Requirement vlookup master column "F" column to raw file of "F" column w.r.t raw file column i.e N,O,P,Q,R,S,T,U,V etc as it continue 1 by 1 raw file (upto 7 raw file) in folder & finally if raw file not found it complete program.
Note: 1 Master file and 7-8 Raw file. Common column in all file is "F Column" and lookup from "F column of master file" column respact to "F" column of all Raw file of column i.e N,O,P,Q,R,S,T,U,V etc...
Hope your understand and please help for coding asap. Thanks...
Plz plz help
VBA Coding for Looping, Vlookup as per my requirement
Hi,
My Requirement vlookup master column "F" column to raw file of "F" column w.r.t raw file column i.e N,O,P,Q,R,S,T,U,V etc as it continue 1 by 1 raw file (upto 7 raw file) in folder & finally if raw file not found it complete program.
Note: 1 Master file and 7-8 Raw file. Common column in all file is "F Column" and lookup from "F column of master file" column respact to "F" column of all Raw file of column i.e N,O,P,Q,R,S,T,U,V etc...
Hope your understand and please help for coding asap. Thanks...
Plz plz help
Clarification
I'm trying to clarify your question:
You want to open 2 files 1 raw and 1 master.
For each entry in column F in Master, lookup the corresponding row in Raw and record columns N,O,P,Q,R,S,T,U & V into Master?
best regards, ijb