I'm a newbie to VBA and this is my first post. I've searched but can't seem to find anything that matches my scenario without using set ranges.
I have a worksheet that holds a list of account numbers in column A (no blank cells in between) and dates across the top row. Each day the user downloads a balances report from the bank and imports it in to this macro.
I want the macro to vlookup the accounts that are in column A of my list (sheetname is Balances) and return the balances for each account from the imported list (sheetname is Todays Bals - Account is Col B and Balance is Col C).
I'm trying to get a vlookup to work from the next empty cell in the top data row (row 2) to the bottom of the account list but it's going past the last non-empty cell and overwriting totals (with #N/A) that are at the bottom of the sheet, formulated ready for when the vlookup is completed for that day. (The totals have a blank row in between the list of accounts).
I think I may have contradicted the lastrow and activecell in the code, but I don't know how to fix it.
Any help is appreciated.
Below is my code so far:
Sub Vlookup()
Dim LastRow As Long
Worksheets("Balances").Activate
If Range("C2").Value > 0 Then '1st day of balances start in C2
Range("C2").End(xlToRight).Offset(0, 1).Select
Else
Range("C2").Select
End If
If Sheets("Todays Bals").Range("A2") <> Sheets("Balances").Cells(1, ActiveCell.Column) Then
MsgBox "These balances are for a different day. Please import the correct day"
Exit Sub
Else
'Above is fine
'Below is where the issue is
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(0, 0).End(xlDown)).Offset(0, 0).Formula = _
"= vlookup(A2,'Todays Bals'!B:C,2,FALSE)"
End If
End Sub