我复制了一个工作代码,并对其进行了一些更改以适合当前表,但这给了我应用程序定义的错误。
基本上,代码将遍历数据列表,确保2个标签(状态然后为标签A)匹配,并将计数器增加1。
从我所见,状态匹配但标签A不匹配时出现错误。
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 | | | | | |
+---+-----------+-----+-----+----+----------+
VBA突出显示的代码是“如果ActiveCell.Offset(0,2-j)= Sheets(“ BaseData(ClientVersion)”)。Cells(i,Spending)然后” 该代码位于选择的案例之后。
我已经包含了用于匹配的表的外观。 如果状态和标签都匹配,则预期结果应该是将计数器增加1,然后在最后一次循环完成后,将计数器值粘贴到表中,将计数器重置为0,然后继续下一个状态和标签。>
谢谢