Excel精英培训网

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

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

  [复制链接]
发表于 2012-4-23 19:40 | 显示全部楼层
Sub 按钮2_单击()

Dim x As Integer, a As Integer

If IsNull(Range("a1:a15").MergeCells) Then

   Range("a1:a15").UnMerge
   
   End
   
   Else

a = 2

For x = 2 To 15

    If Range("a" & x) <> Range("a" & x + 1) Then
   
       Application.DisplayAlerts = False
   
        Range("a" & a & ":" & "a" & x).Merge
        
        Application.DisplayAlerts = True
        
        a = x + 1
        
     End If
     
Next

End If

End Sub
Sub 第一题_按钮2_单击()

  Dim rg As Range, x As Integer, r, a As Integer
  Range("c1:f5").ClearContents '清除原始数据
   Set rg = Range("a1")
   Set rg = Cells.Find(what:="A", after:=rg.Offset(1, 0), MatchCase:=True, LookAt:=xlWhole, SearchDirection:=xlNext) '从a2后面开始查找
   
      a = 2 '设置初始值为第二行
  Do
    x = x + 1
   
    r = rg.Row '将查找到的行数赋值于r
   
    Range("a" & a & ":" & "a" & r - 1).Copy Cells(1, x + 2) '将第二个查找到的位置前一位连同A进行复制
   
    a = r  '重新赋值下一个A的位置
   
    Set rg = Columns("a").Find(what:="A", after:=rg, MatchCase:=True, LookAt:=xlWhole, SearchDirection:=xlNext)
   
  Loop Until rg.Row < r '当最后一个查找完毕后,EXCEL又会从头找起,这时,找到的行数小于最后一个A的位置,结束查找
           
  Range("a" & a & ":" & "a" & Range("a" & Cells.Rows.Count).End(xlUp).Row).Copy Cells(1, x + 3)  '因为没有找到下一个目标,变量x的值不会再增长,所以此时的列位置为x+3

End Sub

H:19 chenzhi_juan

vba第12课作业.rar

12.36 KB, 下载次数: 2

回复

使用道具 举报

发表于 2012-4-23 21:57 | 显示全部楼层
本帖最后由 byhdch 于 2012-4-23 23:11 编辑

A09:byhdch
Sub 第二题合并()
    Dim i, qi, zh As Integer
    Dim StrMer, IntCot As String
    Application.DisplayAlerts = False
    With ActiveSheet
        If IsNull(Range("a2:c15").MergeCells) = False Then  
        qi = 2
            For i = 2 To .Range("a65536").End(xlUp).Row
                If Range("A" & i) = Range("A" & i + 1) Then
                Else
                    zh = i   
                Range("A" & qi & ":A" & zh).Select
                    With Selection
                        .MergeCells = True
                    End With
                    qi = i + 1
                End If
            Next i
        Else
            For i = 2 To .Range("a65536").End(xlUp).Row
                StrMer = .Range("A" & i).Value
                IntCot = .Range("A" & i).MergeArea.Count
                .Range("A" & i).UnMerge
                .Range(.Range("A" & i), .Range("A" & i + IntCot - 1)).Value = StrMer
                i = i + IntCot - 1
            Next i
        End If
    End With
    Application.DisplayAlerts = True
End Sub

vba第12课作业 A09byhdch.rar (9.84 KB, 下载次数: 2)
回复

使用道具 举报

发表于 2012-4-23 22:02 | 显示全部楼层
  1. Sub 按钮2_单击()

  2. Dim m As Integer, n As Integer, k As Integer

  3. Application.DisplayAlerts = False

  4. 'n = Range("a65536").End(xlUp).Row

  5. n = ActiveSheet.UsedRange.Rows.Count

  6. If IsNull(Range("a:a").MergeCells) = True Then

  7.     Range("a:a").UnMerge
  8.    
  9.     For m = 2 To n
  10.    
  11.         If Cells(m, 1) = "" Then
  12.             
  13.             Cells(m, 1) = Cells(m - 1, 1)
  14.                               
  15.         End If
  16.    
  17.     Next m
  18.    
  19. Else

  20. k = 2

  21.     For m = 3 To n + 1
  22.    
  23.         If Cells(m, 1) <> Cells(m - 1, 1) Then
  24.             
  25.             Range(Cells(k, 1), Cells(m - 1, 1)).Merge
  26.             
  27.             k = m
  28.         
  29.         End If
  30.    
  31.     Next m
  32.       
  33. End If

  34. Application.DisplayAlerts = True

  35. End Sub
  36. Sub 第一题_按钮2_单击()

  37. Dim rg As Range, r As Integer, m As Integer, n As Integer, rr As Integer
  38. n = 3
  39. m = Range("a65536").End(xlUp).Row
  40. Set rg = Range("a1")
  41. Set rg = Range("a:a").Find("A", after:=rg, MatchCase:=True, LookAt:=xlWhole, SearchDirection:=xlNext)
  42. r = rg.Row
  43. Do
  44.    
  45.     Set rg = Range("a:a").Find("A", after:=rg, MatchCase:=True, LookAt:=xlWhole, SearchDirection:=xlNext)
  46.    
  47.     rr = rg.Row
  48.    
  49.     If rg.Row > r Then
  50.    
  51.         Range(Cells(r, 1), Cells(rg.Row - 1, 1)).Copy Cells(1, n)
  52.         
  53.         r = rr
  54.             
  55.         n = n + 1
  56.    
  57.     Else
  58.    
  59.         Range(Cells(r, 1), Cells(m, 1)).Copy Cells(1, n)

  60.     End If
  61.         
  62. Loop Until r > rg.Row

  63. End Sub
复制代码


vba第12课作业1.rar

11.63 KB, 下载次数: 2

回复

使用道具 举报

发表于 2012-4-23 22:52 | 显示全部楼层

  1. Option Explicit

  2. Sub 按钮2_单击()
  3.     Dim i As Long, previous As Long
  4.     Application.DisplayAlerts = False
  5.     previous = 2
  6.     For i = 3 To 16
  7.         If (Cells(i, 1) <> "" And Cells(i, 1) <> Cells(i - 1, 1)) Or i = 16 Then
  8.             With Cells(previous, 1).Resize(i - previous)
  9.                 If .MergeCells Then
  10.                     .UnMerge
  11.                     .FillDown
  12.                 Else
  13.                     .Merge
  14.                 End If
  15.             End With
  16.             Debug.Print Cells(previous, 1).Resize(i - previous).Address()
  17.             previous = i
  18.         End If
  19.     Next i
  20.     Application.DisplayAlerts = True
  21. End Sub
  22. Sub 第一题_按钮2_单击()
  23.     Dim i As Long, j As Long, previous As Long
  24.     Dim buf
  25.     previous = 2
  26.     j = 3
  27.     For i = 3 To Cells(Rows.Count, 1).End(3).Row + 1
  28.         If Cells(i, 1) = "A" Or i = Cells(Rows.Count, 1).End(3).Row + 1 Then
  29.             buf = Cells(previous, 1).Resize(i - previous)
  30.             'Debug.Print Cells(previous, 1).Resize(i - previous).Address()
  31.             Cells(1, j).Resize(i - previous) = buf
  32.             j = j + 1
  33.             previous = i
  34.         End If
  35.     Next i
  36. End Sub
复制代码


vba第12课作业BL5062.rar

11.96 KB, 下载次数: 2

回复

使用道具 举报

发表于 2012-4-24 00:32 | 显示全部楼层
D15兰江自由鱼_vba第12课作业.rar (12.45 KB, 下载次数: 2)
回复

使用道具 举报

发表于 2012-4-24 09:56 | 显示全部楼层
C09:sliang28
第一题代码:
  1. Sub 第一题_按钮2_单击()
  2. Dim a(20, 20) As String
  3. Dim b(20, 20) As String
  4. Dim dyg As Range
  5. Dim i!, j!
  6. i = 0
  7. For Each dyg In [A2:A18]
  8. If dyg = "A" Then
  9. i = i + 1
  10. j = 0
  11. b(i, j) = dyg
  12. j = j + 1
  13. Else
  14. b(i, j) = dyg
  15. j = j + 1
  16. End If
  17. Next
  18. For i = 1 To 10
  19. For j = 0 To 10
  20. Sheet1.Cells(j + 1, i + 2) = b(i, j)
  21. Next j
  22. Next i
  23. End Sub
复制代码
第二题代码:(校长,这题我不明白什么叫合并同类项,意思不理解。在我影响当中合并同类项是:比如{3A;3C;2A;2C},合并同类项变成{5A;5C},因为题意不理解,我是按照合并单元格写的,点击一下合并,再点击一下拆分)
  1. Sub 按钮2_单击()
  2. Application.DisplayAlerts = False
  3. Dim k, i As Integer
  4. Dim c As Range
  5. Dim first, last As Integer
  6. k = 2
  7. If Sheet2.Cells(k, 1).MergeCells Then
  8. For Each c In [a2:a16]
  9. If c.MergeCells Then
  10. c.Select
  11. c.UnMerge
  12. Selection.Value = c.Value
  13. End If
  14. Next c
  15. Else
  16. first = 1
  17. For i = 1 To 20 Step 1
  18. If Sheet2.Range("A" & i) = Sheet2.Range("A" & i + 1) Then
  19. Else
  20. last = i
  21. Sheet2.Range("A" & first & ":A" & last).Select
  22. Selection.Merge
  23. first = i + 1
  24. End If
  25. Next
  26. End If
  27. Application.DisplayAlerts = True
  28. End Sub
复制代码

vba第12课作业.zip

11.79 KB, 下载次数: 2

回复

使用道具 举报

发表于 2012-4-24 15:42 | 显示全部楼层
游客,如果您要查看本帖隐藏内容请回复

回复

使用道具 举报

发表于 2012-4-24 17:08 | 显示全部楼层
H组 H15:hactnet
交下作业,请老师检查!

Sub 按钮2_单击()

Dim tt As String
Dim x As Integer, y As Integer
Application.DisplayAlerts = False

If IsNull(Range("a:a").MergeCells) Then                 '判断A列是否有合并单元格

For x = 2 To Range("a65536").End(xlUp).Row
tt = Cells(x, 1)                                        '记录值
y = Cells(x, 1).MergeArea.Count                         '合并单元格计数
Cells(x, 1).UnMerge                                     '解除合并
Range(Cells(x, 1), Cells(x + y - 1, 1)) = tt            '解除合并的单元格取值
x = x + y - 1
Next x

Else

For x = Range("a65536").End(xlUp).Row To 2 Step -1      '倒循环防止多行相同合并时,首次合并后下次合并时出错
If Cells(x, 1) = Cells(x - 1, 1) Then                   '判断相邻单元格是否相等
Range(Cells(x - 1, 1), Cells(x, 1)).Merge               '合并相邻单元格
End If
Next x
End If

Application.DisplayAlerts = True

End Sub

'*************************************
Sub 第二题方法2()

Dim x
Application.DisplayAlerts = False
    If IsNull(Range("a:a").MergeCells) Then Range("a:a").UnMerge
   
    For x = Range("a65536").End(xlUp).Row To 2 Step -1
        If Cells(x, 1) = Cells(x - 1, 1) Then Range(Cells(x - 1, 1), Cells(x, 1)).Merge
    Next x
    Application.DisplayAlerts = True
End Sub
'************************************


Sub 第一题_按钮2_单击()

  Dim rg As Range, x As Integer, r, y, 目标区列总数, 目标区开始列
   Range("c1:f10") = ""
   Set rg = Range("a1")
   
   目标区列总数 = Application.CountIf(Range("A:A"), "A")
   目标区开始列 = 3
   
   r = Range("A:A").Find("A", LookAt:=xlWhole).Row              '第一个A
   
   For x = 2 To 目标区列总数
   
        y = r
        Set rg = Cells.Find("A", after:=rg.Offset(1, 0), MatchCase:=True, LookAt:=xlWhole, SearchDirection:=xlNext)
        r = rg.Row
        Cells(y, 1).Resize(r - y).Copy Cells(1, 目标区开始列)
        目标区开始列 = 目标区开始列 + 1
   Next x
   Cells(r, 1).Resize(Range("A65536").End(xlUp).Row).Copy Cells(1, 目标区开始列)

End Sub

vba第12课作业-H15-hactnet.rar

10.58 KB, 下载次数: 2

回复

使用道具 举报

发表于 2012-4-24 17:25 | 显示全部楼层
g17:szczm121 vba第12课作业szczm121.rar (11.49 KB, 下载次数: 2)
回复

使用道具 举报

发表于 2012-4-24 19:54 | 显示全部楼层
vba第12课作业.rar (10.05 KB, 下载次数: 2)
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 04:12 , Processed in 0.555710 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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