Private Sub CommandButton2_Click()
Dim i As Integer
Dim j As Integer
Dim sh1, sh2
Set 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 If
Next
End 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文件我不会上传,那位说下怎么传到这个贴子上来???

解决方案 »

  1.   

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

  2.   

    成功就好!话说都2012了,一个乡还用excel表格来管理数据,真也悲催啊
      

  3.   

    只是数据统计而已,每个月就一次,没有必要去编个什么程序吧Private Sub CommandButton2_Click()
    Dim i As Integer
    Dim j As Integer
    Dim sh1, sh2
    Set sh1 = Worksheets(2)
    Set sh2 = Worksheets(3)
    For i = 2 To 6
     sh2.Cells(2, i) = ""
     sh2.Cells(3, i) = ""
     sh2.Cells(4, i) = ""
    NextFor 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) + sh1.Cells(i, 8)
           ElseIf sh1.Cells(i, 9) = "价格调节基金" Then
              sh2.Cells(2, 5) = sh2.Cells(2, 5) + sh1.Cells(i, 8)
           ElseIf sh1.Cells(i, 9) = "地方教育附加" Then
              sh2.Cells(2, 4) = sh2.Cells(2, 4) + sh1.Cells(i, 8)
           ElseIf sh1.Cells(i, 9) = "排污费" Then
              sh2.Cells(2, 6) = sh2.Cells(2, 6) + sh1.Cells(i, 8)
           Else
              sh2.Cells(2, 3) = sh2.Cells(2, 3) + sh1.Cells(i, 8)
           
         End If
    Next
    End Sub
    总是提示最后一个NEXT没有FOR,怎么回事?
      

  4.   

    中间的if语句少了一个对应的End If
    看我上面回复的代码,应该有两个End If的。