I'm struggling to get the below code to function due to Run-time error '1004': Application-define or object-defined error.
Usually when I've encountered this error is was due to sheet protection, I've ensured sheets are unprotected thus the line at the start of the sub.
I'll outlay the scenario here and the end goal:
Sheet1 = Live Contracts
This sheet contains a table of 6 columns and dynamic number of rows. column 1 contains a contract reference number. Columns 2 - 6 are irrelevant to this.
Sheet 4 = Contract Sums
This sheet contains all contracts, the corresponding department and the value of the contract.
Goal
Create a module which can be called via a userform button.
The module needs to view column A of sheet1 and for each cell -if there is a value- create a vlookup in column G with the following parameters.
Lookup Value = Cell.offset(0, -6)
Table array = Sheet4.range("A3:C676")
Col_Index_Num = 1
FALSE - Exact match
The module would need to repeat this process for all cells within the range.
Now for what I've produced:
Worksheets("Live Contracts").Unprotect
Dim rng As range
Dim lastrow As Long
Dim cell As range
Dim contractrange As range
'Find dynamic range
lastrow = Worksheets("Live Contracts").range("A" & Rows.Count).End(xlUp).Row
Set contractrange = Worksheets("Contract Sums").range("A3:C676")
Set rng = Worksheets("Live Contracts").range("A2:A" & lastrow)
For Each cell In rng
'If cell does not equal blank then for each cell in column A, offset to column G.
If cell.Value <> "" Then
'In column G, vlookup column A cell value in contractrange's column C, only return exact match
cell.Offset(0, 6).Value = Application.VLookup(cell.Offset(0, -6), contractrange, 1, False)
'In column H, vlookup column A cell value in contractrange's column A, only return exact match
cell.Offset(0, 7).Value = Application.VLookup(cell.Offset(0, -7), contractrange, 3, False)
End If
'Repeat for all cells in range
Next cell
End Sub
The first line beginning with cell.offset is being highlighted when debugging
P.S I'm relatively new to VBA, apologies for poor code!
lastrow
is being used before it is defined? I believe the error 1004 is because you haven't definedcontractsum
. – Jaromcell.offset(0,-6)
You can only offset to the right of column A. – Jarom