Team,
In active file (name DATA_1, where I want to run a macro), in column "A" I have some data (numbers: 123, 160, 740, 100 etc).
Now I want open file (DATA_2 ) with source data.
In DATA_1, column "B" I want o use VlookUp, because I need to assigned value from DATA_2, coulmn 4. (should be something like this: 123 = Opel, 160 = Audi, 740 = BMW etc).
My code:
I see for this moment one mistake - VB ask me twice to open file (DATA_2) . But I'm sure that my code need to be more modified.
Please help me with that.
regards
PvK
In active file (name DATA_1, where I want to run a macro), in column "A" I have some data (numbers: 123, 160, 740, 100 etc).
Now I want open file (DATA_2 ) with source data.
In DATA_1, column "B" I want o use VlookUp, because I need to assigned value from DATA_2, coulmn 4. (should be something like this: 123 = Opel, 160 = Audi, 740 = BMW etc).
My code:
Code:
Dim wkbSource As Workbook
Dim wksSource As Worksheet
Dim wksDest As Worksheet
Dim strFile As String
Set wksDest = Worksheets("Sheet1")
MsgBox "Open file with source data"
strFile = Application.GetOpenFilename( _
FileFilter:="Excel Files (*.xls), *.xls)", _
Title:="Select a File", _
MultiSelect:=False)
If strFile = "False" Then Exit Sub
Set wkbSource = Workbooks.Open(strFile)
Set wksSource = wkbSource.Worksheets("Sheet1")
With wksSource
LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
LastRow = .Cells(.Rows.Count, LastColumn).End(xlUp).Row
Set Rng = Range(.Cells(1, 1), .Cells(LastRow, LastColumn))
End With
wksDest.Activate
x_rows = Application.WorksheetFunction.CountA(Columns(1))
Range("B1").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],[wksSource]Sheet1!Rng,4,0)"
Selection.AutoFill Destination:=Range("x_rows"), Type:=xlFillDefault
I see for this moment one mistake - VB ask me twice to open file (DATA_2) . But I'm sure that my code need to be more modified.
Please help me with that.
regards
PvK