Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!

[通知] 统计VBA学习小组正式组的积分帖之作业上交贴(第14周)

  [复制链接]
发表于 2012-3-29 16:28 | 显示全部楼层
第11课作业题.zip (16.61 KB, 下载次数: 7)

评分

参与人数 1金币 +8 收起 理由
兰色幻想 + 8 答案正确

查看全部评分

回复

使用道具 举报

发表于 2012-3-29 16:58 | 显示全部楼层
校长,第一个是不是有点复杂了?要判断日期的

  1. Private Sub CommandButton1_Click()
  2. Dim rg As Range
  3. Dim i As Integer
  4. Dim a(100) As Integer
  5. Dim b(1 To 100) As Integer
  6. Dim i_past As Integer
  7. Dim insertrow As String
  8. Dim t As Integer
  9. Dim s As Integer
  10. i = 1
  11. t = 1
  12. s = 2
  13. a(0) = 2
  14. On Error Resume Next
  15. For Each rg In [A2: A16]
  16.     a(i) = Month(rg.Value)
  17.     If a(i - 1) < a(i) Then
  18.        b(i) = i + 1
  19.     End If
  20.     i = i + 1
  21. Next
  22. For i = 1 To 20
  23. If b(i) <> 0 Then
  24.    b(i) = b(i) + t
  25.    Rows(b(i) - 1).Insert
  26.    Range("a" & b(i) - 1) = "小计"
  27.    Range("c" & b(i) - 1) = Application.WorksheetFunction.Sum(Range("c" & s & " :" & "c" & b(i) - 1))
  28.    Range("d" & b(i) - 1) = Application.WorksheetFunction.Sum(Range("d" & s & " :" & "d" & b(i) - 1))
  29.    s = b(i)
  30.    t = t + 1
  31. End If '
  32. Next i
  33. Range("a65536").End(xlUp).Offset(1, 0) = "小计"
  34. Range("a65536").End(xlUp).Offset(0, 2) = Application.WorksheetFunction.Sum(Range("c" & s & " :" & "c" & Range("a65536").End(xlUp).Row - 1))
  35. Range("a65536").End(xlUp).Offset(0, 3) = Application.WorksheetFunction.Sum(Range("D" & s & " :" & "D" & Range("a65536").End(xlUp).Row - 1))
  36. End Sub

  37. Private Sub 按月小计1()
  38. Dim rg As Range
  39. Dim i As Integer
  40. Dim i_past As Integer
  41. Dim insertrow As String

  42. For Each rg In [A2:A16]
  43.     i = Month(rg.Value)
  44.     If i <> i_past Then
  45.         If insertrow <> "" Then
  46.             insertrow = insertrow & "," & rg.Row & ":" & rg.Row
  47.         Else
  48.             insertrow = insertrow & rg.Row & ":" & rg.Row
  49.         End If
  50.     End If


  51. Next

  52. Range("6:6,11:11,15:15").Insert

  53. End Sub

  54. Private Sub CommandButton2_Click()
  55.    
  56.    Range("C2:C16").SpecialCells(xlCellTypeBlanks).Offset(0, 2) = 1
  57.       
  58. End Sub


  59. Private Sub CommandButton3_Click()
  60.    
  61.     Range("B2:B20").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  62.         
  63.         Range("e2:e20").ClearContents
  64. End Sub
复制代码

评分

参与人数 1金币 +7 收起 理由
兰色幻想 + 7 答案正确,是有点复杂了

查看全部评分

回复

使用道具 举报

发表于 2012-3-29 17:03 | 显示全部楼层
校长:
对不起!
13楼的答案中第38行到59行是不要的,忘记删除了。
回复

使用道具 举报

发表于 2012-3-29 20:09 | 显示全部楼层
Private Sub CommandButton1_Click() '按月小计
Dim c, i, x, s1, s2
c = [a65536].End(xlUp).Row + 12
x = 2
s1 = [c2]: s2 = [d2]
For i = 2 To c
    If Range("a" & i) <> "" And Month(Range("a" & i)) <> Month(Range("a" & i + 1)) Then
        Range("a" & i + 1).EntireRow.Insert
        Range("a" & i + 1) = "合计"
        Range("c" & i + 1) = s1
        Range("d" & i + 1) = s2
        x = i + 2
        i = i + 1
        s1 = Range("c" & i + 1)
        s2 = Range("d" & i + 1)
    Else
        s1 = s1 + Range("c" & i + 1)
        s2 = s2 + Range("d" & i + 1)
    End If
Next i
End Sub


Private Sub CommandButton2_Click() '根据以列空在E列填数字1
Dim b
b = [c65536].End(xlUp).Row
Intersect(Columns("e:e"), Range("c1:c" & b).SpecialCells(xlCellTypeBlanks).EntireRow) = 1
End Sub


Private Sub CommandButton3_Click() '恢复表格
Dim a
a = [b65536].End(xlUp).Row + 1
Range("e2:e" & a).ClearContents
Range("b1:b" & a).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub

评分

参与人数 1金币 +8 收起 理由
兰色幻想 + 8 答案正确

查看全部评分

回复

使用道具 举报

发表于 2012-3-29 23:08 | 显示全部楼层
Option Explicit
Private Sub CommandButton1_Click()
    Dim n%, mo%, m1%
    m1 = 2
    For n = 2 To 100
        If Month(Range("A" & n)) <> Month(Range("A" & n + 1)) Then
            n = n + 1
            Rows(n).Insert
            Cells(n, 1) = "小计"
            Cells(n, 3) = Application.WorksheetFunction.Sum(Range("C" & m1 & ":C" & n - 1))
            Cells(n, 4) = Application.WorksheetFunction.Sum(Range("D" & m1 & ":d" & n - 1))
            n = n + 1
            m1 = n
        End If
    Next n
End Sub
Private Sub CommandButton2_Click()
    Application.Intersect(Range("D2:D" & Range("a63556").End(xlUp).Row).SpecialCells(xlCellTypeBlanks).EntireRow, Range("E:E")) = 1
End Sub
Private Sub CommandButton3_Click()
    Range("B:B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    Range("E2:e16") = ""
End Sub

G05-mfksypss.rar

22.36 KB, 下载次数: 7

评分

参与人数 1金币 +8 收起 理由
兰色幻想 + 8 答案正确

查看全部评分

回复

使用道具 举报

发表于 2012-3-29 23:20 | 显示全部楼层
Private Sub CommandButton1_Click()

    Application.ScreenUpdating = False

    Dim FirstNumer, LastNumber As Integer
    Dim x As Long

    FirstNumer = 2

    For x = 2 To Rows.Count
        If Range("a" & x) = "" Then Exit Sub
        If VBA.Month(Range("a" & x)) <> VBA.Month(Range("a" & x + 1)) Then
            LastNumber = x
            Range("a" & x + 1).EntireRow.Insert
            Range("a" & x + 1).Range("a1") = "小计:"
            Range("a" & x + 1).Range("c1") = WorksheetFunction.Sum(Range("c" & FirstNumer & ":" & "c" & LastNumber))
            Range("a" & x + 1).Range("d1") = WorksheetFunction.Sum(Range("d" & FirstNumer & ":" & "d" & LastNumber))
            x = x + 1
            FirstNumer = x + 1
        End If
    Next x

    Application.ScreenUpdating = True

End Sub

Private Sub CommandButton2_Click()

    Application.ScreenUpdating = False

    Dim rg As Range
    For Each rg In Range("c1:c" & Range("c" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeBlanks)
        rg.Range("c1") = 1
    Next rg

    Application.ScreenUpdating = True

End Sub

Private Sub CommandButton3_Click()

    On Error Resume Next
    Range("b1:b" & Range("c" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    Range("e2:e" & Rows.Count).ClearContents

End Sub

第11课作业题.rar (13.89 KB, 下载次数: 3)

评分

参与人数 1金币 +8 收起 理由
兰色幻想 + 8 答案正确

查看全部评分

回复

使用道具 举报

发表于 2012-3-30 00:09 | 显示全部楼层
Private Sub CommandButton1_Click()
    Dim a As Integer, c As Integer, i As Integer
    a = [a65536].End(3).Row + 1
    c = 2: i = 2
    Do Until i >= a
        If Month(Cells(i, 1)) <> Month(Cells(i + 1, 1)) Then
            Cells(i + 1, 1).EntireRow.Insert
            a = a + 1: i = i + 1
            Cells(i, 1) = "小计"
            Cells(i, 3) = Application.Sum(Range("C" & c, "C" & i - 1))
            Cells(i, 4) = Application.Sum(Range("D" & c, "D" & i - 1))
            c = i + 1
        End If
        i = i + 1
    Loop
End Sub
Private Sub CommandButton2_Click()
Range("c1:c" & [a65536].End(3).Row).SpecialCells(xlCellTypeBlanks).Offset(0, 2) = 1
End Sub
第11课作业题.rar (26.96 KB, 下载次数: 3)

评分

参与人数 1金币 +8 收起 理由
兰色幻想 + 8 答案正确

查看全部评分

回复

使用道具 举报

发表于 2012-3-30 11:25 | 显示全部楼层
第11课作业题.zip (16.56 KB, 下载次数: 3)

评分

参与人数 1金币 +8 收起 理由
兰色幻想 + 8 答案正确

查看全部评分

回复

使用道具 举报

发表于 2012-3-30 14:21 | 显示全部楼层
作业代码如下,请批改。谢谢!
Private Sub CommandButton1_Click()   '分月小计
  Dim i As Integer, j As Integer, k As Integer, m As Integer
  m = Range("a65536").End(xlUp).Row
  i = 2
  Do
      j = i
      k = VBA.Month(Range("a" & i))
      Do
         i = i + 1
       Loop Until VBA.Month(Range("a" & i)) <> k
       Rows(i).Insert
       m = m + 1
       Range("a" & i) = "  小计 "
       Range("C" & i).Formula = "=sum(C" & j & ":C" & i - 1 & ")"
       Range("D" & i).Formula = "=sum(D" & j & ":D" & i - 1 & ")"
     i = i + 1
   Loop Until i > m
End Sub

Private Sub CommandButton2_Click()  '筛选
  Range("D2:D" & Range("a65536").End(xlUp).Row).SpecialCells(xlCellTypeBlanks).Offset(0, 1) = 1
End Sub

Private Sub CommandButton3_Click()  '恢复
   Range("B1:B20").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
   Range("E2:E20") = ""
End Sub


兰江自由鱼_第11课作业题.rar (24.26 KB, 下载次数: 8)

评分

参与人数 1金币 +8 收起 理由
兰色幻想 + 8 答案正确

查看全部评分

回复

使用道具 举报

发表于 2012-3-30 15:18 | 显示全部楼层
Private Sub CommandButton1_Click()
      Dim x, heji1, heji2
      heji1 = 0
      heji2 = 0

  For x = 2 To 30
    If Cells(x, 1) <> "" And Month(Cells(x, 1)) = Month(Cells(x + 1, 1)) Then
              heji1 = heji1 + Range("c" & x)
              heji2 = heji2 + Range("d" & x)
   ElseIf Cells(x, 1) <> "" And Month(Cells(x, 1)) <> Month(Cells(x + 1, 1)) Then
              heji1 = heji1 + Range("c" & x)
              heji2 = heji2 + Range("d" & x)
              Rows(x + 1).Insert
              Range("a" & x + 1) = "小计"
              Range("c" & x + 1) = heji1
              Range("d" & x + 1) = heji2
              x = x + 1
              heji1 = 0
              heji2 = 0
              End If
   Next
End Sub

Private Sub CommandButton2_Click()

    Range("E2").FormulaR1C1 = "=IF(RC[-1]="""",1,"""")"
    Range("E2").Select
    Selection.AutoFill Destination:=Range("e2:e19")
        '老师,我不知道怎样自动判断是最后一行为空,所以做了个死的,虽然能完成作业,但不完善,请帮一下

End Sub
Private Sub CommandButton3_Click()

Range("B:B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Range("e2:e65536").ClearContents
End Sub


评分

参与人数 1金币 +3 收起 理由
兰色幻想 + 3 不能用公式

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-25 01:09 , Processed in 0.523998 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表