VBA不成功,大家看下原因
Private Sub CommandButton2_Click()Dim i As IntegerDim j As IntegerDim sh1, sh2Set sh1 = Worksheets(2)Set sh2 = Worksheets(3)For i = 2 To 6 sh2.Cells(2, i) = "" sh2.Cells(3, i) = "" sh2.Cells(4, i) = ""Next'每次统计前清空各栏数据For i = 2 To sh1.UsedRange.Rows.Count If sh1.Cells(i, 22) = "A镇" Then '若乡镇相符则分乡镇统计各税种的合计数 If sh1.Cells(i, 9) = "企业所得税" Or sh1.Cells(i, 9) = "个人所得税" Then sh2.Cells(2, 2) = sh2.Cells(2, 2) + VAL(sh1.Cells(i, 8)) ElseIf sh1.Cells(i, 9) = "价格调节基金" Then sh2.Cells(2, 5) = sh2.Cells(2, 5) + val(sh1.Cells(i, 8)) ElseIf sh1.Cells(i, 9) = "地方教育附加" Then sh2.Cells(2, 4) = sh2.Cells(2, 4) + val(sh1.Cells(i, 8)) ElseIf sh1.Cells(i, 9) = "排污费" Then sh2.Cells(2, 6) = sh2.Cells(2, 6) + val(sh1.Cells(i, 8)) Else: sh2.Cells(2, 3) = sh2.Cells(2, 3) + val(sh1.Cells(i, 8)) End IfNextEnd Sub
If sh1.Cells(i, 22) = "A镇" Then '若乡镇相符则分乡镇统计各税种的合计数 If sh1.Cells(i, 9) = "企业所得税" Or sh1.Cells(i, 9) = "个人所得税" Then sh2.Cells(2, 2) = sh2.Cells(2, 2) + VAL(sh1.Cells(i, 8))''''''''''''''''''''' 就是些ElseIf,和下面的Else,其实他们对应的If是上面的【 If sh1.Cells(i, 22) = "A镇"】这个条件'''''''''''''''''''''原因就是上面的那个If语句中,If和Then写在了同一行上,那么就是一个独立的If语句。 ElseIf sh1.Cells(i, 9) = "价格调节基金" Then sh2.Cells(2, 5) = sh2.Cells(2, 5) + val(sh1.Cells(i, 8)) ElseIf sh1.Cells(i, 9) = "地方教育附加" Then sh2.Cells(2, 4) = sh2.Cells(2, 4) + val(sh1.Cells(i, 8)) ElseIf sh1.Cells(i, 9) = "排污费" Then sh2.Cells(2, 6) = sh2.Cells(2, 6) + val(sh1.Cells(i, 8)) Else: sh2.Cells(2, 3) = sh2.Cells(2, 3) + val(sh1.Cells(i, 8)) End If'''''''''''''''''''''''''''''''''改写如下''''''''''''''''''''''''''''''''''''''''' If sh1.Cells(i, 22) = "A镇" Then '若乡镇相符则分乡镇统计各税种的合计数 If sh1.Cells(i, 9) = "企业所得税" Or sh1.Cells(i, 9) = "个人所得税" Then sh2.Cells(2, 2) = sh2.Cells(2, 2) + VAL(sh1.Cells(i, 8)) ElseIf sh1.Cells(i, 9) = "价格调节基金" Then sh2.Cells(2, 5) = sh2.Cells(2, 5) + val(sh1.Cells(i, 8)) ElseIf sh1.Cells(i, 9) = "地方教育附加" Then sh2.Cells(2, 4) = sh2.Cells(2, 4) + val(sh1.Cells(i, 8)) ElseIf sh1.Cells(i, 9) = "排污费" Then sh2.Cells(2, 6) = sh2.Cells(2, 6) + val(sh1.Cells(i, 8)) Else sh2.Cells(2, 3) = sh2.Cells(2, 3) + val(sh1.Cells(i, 8)) End If End If
[解决办法]
..............
找出原因就好。
逻辑问题,旁人是难以找出原因的.......
[解决办法]
中间的if语句少了一个对应的End If
看我上面回复的代码,应该有两个End If的。