首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

VBA不成功,大家看下原因,该怎么处理

2012-03-22 
VBA不成功,大家看下原因VB codePrivate Sub CommandButton2_Click()Dim i As IntegerDim j As IntegerDim

VBA不成功,大家看下原因

VB code
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, 9) = "企业所得税" Or sh1.Cells(i, 9) = "个人所得税" Then sh2.Cells(2, 2) = sh2.Cells(2, 2) + VAL(sh1.Cells(i, 8))
这句统计的数据正确,若用If sh1.Cells(i, 9) = "企业所得税" Then sh2.Cells(2, 2) = sh2.Cells(2, 2) + VAL(sh1.Cells(i, 8)) ElseIf If sh1.Cells(i, 9) = "个人所得税" Then sh2.Cells(2, 2) = sh2.Cells(2, 2) + VAL(sh1.Cells(i, 8))这样的话,数据就不正常了,求大鸟们说下那儿出了问题???

注:这个EXCEL文件我不会上传,那位说下怎么传到这个贴子上来???



[解决办法]
问题找到了,呵呵。
咋一看,这程序,还以为会出编译错误呢,但仔细瞧了瞧,明白个中的问题所在了。
出错代码段如下:
VBScript code
    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的。

热点排行