i copied a working code and changed it a little bit to fit the current table but it gives me the application defined error.
Basically the code goes through a list of data, make sure that 2 labels (state and then label A) matches and increase the counter by 1.
From what i have seen, the error appears when state matches but label A does not match.
Sub SPENDING_update()
STORE_CODE = Application.Match("STORE_CODE", Sheets("BaseData(ClientVersion)").Range("1:1"), False)
Spending = Application.Match("SPENDING", Sheets("BaseData(ClientVersion)").Range("1:1"), False)
STORE_TYPE = Application.Match("STORE_TYPE", Sheets("BaseData(ClientVersion)").Range("1:1"), False)
WEIGHTING = Application.Match("WEIGHTING", Sheets("BaseData(ClientVersion)").Range("1:1"), False)
Lastrow = Sheets("BaseData(ClientVersion)").Range("A" & Rows.Count).End(xlUp).Row
table1ref = Application.Match(2, Sheets("Extras").Range("A:A"), False)
counter = 0
Sheets("Extras").Activate
Sheets("Extras").Cells(table1ref, 1).Offset(2, 2).Select
For j = 3 To 6
Do Until ActiveCell.Offset(0, 2 - j) = "Total"
For i = 2 To Lastrow
Select Case Val(Left(Sheets("BaseData(ClientVersion)").Cells(i, STORE_CODE), 1))
Case Is = 1: state = "VIC"
Case Is = 2: state = "NSW & ACT"
Case Is = 3: state = "WA"
Case Is = 4: state = "QLD"
End Select
If state = Sheets("Extras").Cells(table1ref, 1).Offset(1, j - 1) Then
If ActiveCell.Offset(0, 2 - j) = Sheets("BaseData(ClientVersion)").Cells(i, Spending) Then
k = Sheets("BaseData(ClientVersion)").Cells(i, WEIGHTING)
counter = counter + (1 * k)
End If
End If
Next i
ActiveCell = counter
counter = 0
ActiveCell.Offset(1, 0).Select
Loop
Sheets("Extras").Cells(table1ref, 1).Offset(2, j).Select
Next j
j = 7
Sheets("Extras").Cells(table1ref, 1).Offset(2, 6).Select
Do Until ActiveCell.Offset(0, 2 - j) = "Total"
For i = 2 To Lastrow
state = Val(Left(Sheets("BaseData(ClientVersion)").Cells(i, STORE_CODE), 1))
If state = 1 Or state = 2 Or state = 4 Then
If Sheets("BaseData(ClientVersion)").Cells(i, STORE_TYPE).Value = "Corporate Store" Then
If ActiveCell.Offset(0, 2 - j) = Sheets("BaseData(ClientVersion)").Cells(i, LATEST_PROD) Then
counter = counter + 1
End If
End If
End If
Next i
ActiveCell = counter
counter = 0
ActiveCell.Offset(1, 0).Select
Loop
End Sub
+---+-----------+-----+-----+----+----------+
| | NSW & ACT | QLD | VIC | WA | COHO ESB |
+---+-----------+-----+-----+----+----------+
| A | | | | | |
| B | | | | | |
| C | | | | | |
| D | | | | | |
| E | | | | | |
+---+-----------+-----+-----+----+----------+
The code that was highlighted by VBA is "If ActiveCell.Offset(0, 2 - j) = Sheets("BaseData(ClientVersion)").Cells(i, Spending) Then" This code is right after the select Case.
I have included what the table which is used for matching looks like. the expected result should be counter increase by 1 if both state and label matches and then once the loop finished at lastrow, it paste the counter value in the table, reset the counter back to 0 and move on the next state and label.
Thanks