Hello everyone,
I hope someone will be able to help. I'm new to macros and I have trouble writing a VBA Vlookup across 2 sheets in the same workbook:
1. Sheet "ACT HOURS VS BUDGET" is where the data will be imported
2. sheet 1 is where the data I need is located.
3. in both sheets the common data is situated in Column A
4. the data in sheet 1 has 4 columns: I've managed to do a VBA to name the Range as DATA. I want to get the data from column 4 from DATA
when I do it without VBA in sheet "ACT HOURS VS BUDGET"--> Vlookup=(a2,data,4,0).
5.Sheet ("ACT HOURS VS BUDGET") is dynamic: I add columns everytime I work on it. The formula is inserted in the 1st unused cell of row 2. I've managed to do a VBA to find and name the 1st unused column "NEW DATE" but don't know how to reference row 2 to insert the VLookup.
This is what I have so far.
Sub VLookup()
Dim LastRow As Long
Dim LastCol As Long
Dim ws1, ws2 As Worksheet
Set ws1 = Worksheets("ACT HOURS VS BUDGET")
Set ws2 = Worksheets("sheet1")
'Finds first blank column
NewCol = ws1.Cells(1, Columns.Count).End(xlToLeft).Column + 1
' Puts your header at the top of the first blank column
Cells(1, NewCol).Value = "New Date"
' Works out the first blank row under the new header
LastRow = ws1.Cells(Rows.Count, NewCol).End(xlUp).Row + 1
' Updates the first blank row of the first blank column with your desired formula
Cells(LastRow, NewCol).Formula = "=vlookup=('ACT HOURS VS BUDGET'!A2,DDATA,4,FALSE)"
I really appreciate the help you can give me. I've uploaded the file as it may help you?
I hope someone will be able to help. I'm new to macros and I have trouble writing a VBA Vlookup across 2 sheets in the same workbook:
1. Sheet "ACT HOURS VS BUDGET" is where the data will be imported
2. sheet 1 is where the data I need is located.
3. in both sheets the common data is situated in Column A
4. the data in sheet 1 has 4 columns: I've managed to do a VBA to name the Range as DATA. I want to get the data from column 4 from DATA
when I do it without VBA in sheet "ACT HOURS VS BUDGET"--> Vlookup=(a2,data,4,0).
5.Sheet ("ACT HOURS VS BUDGET") is dynamic: I add columns everytime I work on it. The formula is inserted in the 1st unused cell of row 2. I've managed to do a VBA to find and name the 1st unused column "NEW DATE" but don't know how to reference row 2 to insert the VLookup.
This is what I have so far.
Sub VLookup()
Dim LastRow As Long
Dim LastCol As Long
Dim ws1, ws2 As Worksheet
Set ws1 = Worksheets("ACT HOURS VS BUDGET")
Set ws2 = Worksheets("sheet1")
'Finds first blank column
NewCol = ws1.Cells(1, Columns.Count).End(xlToLeft).Column + 1
' Puts your header at the top of the first blank column
Cells(1, NewCol).Value = "New Date"
' Works out the first blank row under the new header
LastRow = ws1.Cells(Rows.Count, NewCol).End(xlUp).Row + 1
' Updates the first blank row of the first blank column with your desired formula
Cells(LastRow, NewCol).Formula = "=vlookup=('ACT HOURS VS BUDGET'!A2,DDATA,4,FALSE)"
I really appreciate the help you can give me. I've uploaded the file as it may help you?