Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 17589|回复: 29

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

  [复制链接]
发表于 2012-4-21 13:46 | 显示全部楼层 |阅读模式
活动类型:
作业上交
开始时间:
2012-4-18 13:45 至 2012-4-25 13:45 商定
活动地点:
VBA学习小组
性别:
不限
已报名人数:
24

本帖最后由 冠军欧洲2010 于 2012-5-9 07:58 编辑

说明:
统计帖每个学员只能跟帖回复一次,也就是在原来回复楼层的基础上点编缉,不要一个链接一层楼,否则不计算积分。

各小组学员上交作业时,一定要点击我要参加注明自己的新组编号和论坛ID如果点击过我要参加但没有跟帖提交作业的,扣该学员5积分;如果跟帖提交了作业,但没有点我要参加的,不给予评分。

请各学员看清上面的说明,免得被扣了分分!
本帖为仅楼主可见帖,直接回复即可!
作业链接:http://www.excelpx.com/thread-238836-1-1.html

暂未通过 (24 人)

  留言 申请时间
gaoshuichang1 2012-4-29 21:12
liuho1 2012-4-27 15:25
君子豹变 2012-4-26 13:23
想飞的鸟 2012-4-26 07:59
梅一枝 2012-4-25 23:03
晓梦迷蝶 2012-4-25 12:07
szczm121 2012-4-24 17:23
hactnet 2012-4-24 17:09
发表于 2012-4-21 20:20 | 显示全部楼层
C12:hrpotter
  1. Option Explicit
  2. Sub 拆分()
  3.     Dim i As Integer, j As Integer
  4.     Dim rg As Range
  5.     Dim firstaddress As String
  6.     Range("c:iv").Clear
  7.     Set rg = Range("a:a").Find("A")
  8.     If Not rg Is Nothing Then
  9.         firstaddress = rg.Address
  10.         j = rg.Row
  11.         Do
  12.             i = i + 1
  13.             Set rg = Range("a:a").FindNext(rg)
  14.             If rg.Address <> firstaddress Then
  15.                 Range(Cells(j, 1), Cells(rg.Row - 1, 1)).Copy Cells(1, 2 + i)
  16.             Else
  17.                 Range(Cells(j, 1), Range("a65536").End(xlUp)).Copy Cells(1, 2 + i)
  18.             End If
  19.             j = rg.Row
  20.         Loop While Not rg Is Nothing And rg.Address <> firstaddress
  21.     End If
  22. End Sub
  23. Sub 合并()
  24.     Dim i As Integer
  25.     If IsNull(Range("a1").CurrentRegion.MergeCells) Then
  26.         Range("a1").CurrentRegion.UnMerge
  27.     Else
  28.         For i = Range("a65536").End(xlUp).Row To 2 Step -1
  29.             If Cells(i, 1) = Cells(i - 1, 1) Then
  30.                 Application.DisplayAlerts = False
  31.                 Range(Cells(i - 1, 1), Cells(i, 1)).Merge
  32.                 Application.DisplayAlerts = True
  33.             End If
  34.         Next
  35.     End If
  36. End Sub
复制代码
C12-hrpotter-vba第12课作业.rar (8.85 KB, 下载次数: 16)
回复

使用道具 举报

发表于 2012-4-21 20:56 | 显示全部楼层
Sub 第一题()
Dim Lrow As Long
Dim Lcol As Long, M
Lcol = 2
For Lrow = 2 To Range("A65536").End(3).Row
    If Range("a" & Lrow).Value = "A" Then
        M = 1
        Lcol = Lcol + 1
        Cells(1, Lcol) = Range("a" & Lrow)
    Else
        M = M + 1
        Cells(M, Lcol) = Range("a" & Lrow)
    End If
Next Lrow
End Sub
第二题
Sub 按钮2_单击()
Dim Lrow As Long, M, Y
Application.DisplayAlerts = False
    Y = Range("a65536").End(3).Row
    If IsNull(Range(Cells(1, "a"), Cells(Y, "a")).MergeCells) = False Then
        For Lrow = 2 To Y
        If Range("A" & Lrow) = Range("A" & Lrow + 1) Then
            M = M + 1
        Else
            Range(Cells(Lrow - M, "a"), Cells(Lrow, "a")).Merge
            M = 0
        End If
        Next Lrow
    Else
        Range(Cells(1, "a"), Cells(Y, "a")).UnMerge
    End If
Application.DisplayAlerts = True
End Sub

vba第12课作业.xls

38 KB, 下载次数: 6

回复

使用道具 举报

发表于 2012-4-22 18:19 | 显示全部楼层
E学委:sunjing-zxl
vba第12课作业-E学委-sunjing-zxl.rar (12.21 KB, 下载次数: 6)
回复

使用道具 举报

发表于 2012-4-22 22:57 | 显示全部楼层
  1. Option Explicit
  2. Public U%
  3. '合并同类项
  4. Sub Merge_RNG()

  5.       Dim R As Integer
  6.       Dim i As Integer
  7.       Application.DisplayAlerts = False
  8.       With Sheets("第二题")
  9.           R = .Range("A65536").End(xlUp).Row
  10.           For i = R To 2 Step -1
  11.               If .Cells(i, 1).Value = .Cells(i - 1, 1).Value Then
  12.                   .Range(.Cells(i - 1, 1), .Cells(i, 1)).Merge
  13.               End If
  14.           Next
  15.       End With
  16.       Application.DisplayAlerts = True
  17. End Sub
  18. '取消合并
  19. Sub UnMerge_RNG()
  20.       Dim STR As String
  21.       Dim T As Integer
  22.       Dim i As Integer
  23.       With Sheets("第二题")
  24.           For i = 2 To .Range("A65536").End(xlUp).Row
  25.               STR = .Cells(i, 1).Value
  26.               T = .Cells(i, 1).MergeArea.Count
  27.               .Cells(i, 1).UnMerge
  28.               .Range(.Cells(i, 1), .Cells(i + T - 1, 1)).Value = STR
  29.               i = i + T - 1
  30.           Next
  31.       End With
  32.   End Sub
  33. Sub 第二题()
  34.     If IsNull(Range("A2:A" & [A65536].End(3).Row).MergeCells) Then
  35.         Call UnMerge_RNG
  36.     Else
  37.         Call Merge_RNG
  38.     End If
  39. End Sub
  40. Sub 第一题()

  41. U = (U + 1) Mod 2

  42. If U = 1 Then
  43.     Call RF
  44.     MsgBox "这是用正则完成的!"
  45. Else
  46.     Call FF
  47.     MsgBox "这是用FIND完成的!"
  48. End If

  49. End Sub
  50. '投机取巧法,目的复习正则表达式
  51. Sub RF()
  52. Dim STR As String
  53. Dim i, J As Integer
  54. Dim arr, K, M
  55. Dim rg

  56. Set rg = CreateObject("VBScript.RegExp")
  57. arr = Range("a2:a" & [A65536].End(3).Row)
  58. STR = Join(Application.Transpose(arr), "@")

  59. With rg
  60.     .Global = True
  61.     .Pattern = "\bA[^A]+\b"
  62.     Set K = .Execute(STR)
  63. End With

  64. J = 3
  65. For i = 0 To K.Count - 1
  66.     M = Application.Transpose(Split(K(i), "@"))
  67.     Cells(1, J).Resize(UBound(M), 1) = M
  68.     J = J + 1
  69. Next
  70. End Sub
  71. 'FIND方法
  72. Sub FF()
  73.     Dim Cel1, Cel2, RNG As Range
  74.     Dim i, R, C As Integer
  75.     i = Application.CountIf(Range("A:A"), "A")
  76.     C = 3
  77.     Set Cel1 = Range("A2")
  78.     With Range("A:A")
  79.     For R = 1 To i
  80.         Set Cel2 = .Find(What:="A", After:=Cel1, LookIn:=xlValues, LookAt:=xlWhole)
  81.         If Cel2.Address = "$A$2" Then Set Cel2 = [A65536].End(3).Offset(1, 0)
  82.         Set RNG = Range(Cel1, Cel2.Offset(-1, 0))
  83.         RNG.Copy Cells(1, C)
  84.         C = C + 1
  85.         Set Cel1 = Cel2
  86.     Next

  87.     End With
  88. End Sub
复制代码

vba第12课作业.rar

14.19 KB, 下载次数: 8

回复

使用道具 举报

发表于 2012-4-22 23:19 | 显示全部楼层
Sub 第一题()
    Range("c1:f5").ClearContents
    Dim i As Integer
    Dim j As Integer
    Dim x As Integer
    Dim y As Integer
    Dim arr
    i = Range("a65536").End(xlUp).Row
    For j = i To 2 Step -1
        y = y + 1
        If Cells(j, 1) = "A" Then
            arr = Range(Cells(j, 1), Cells(i, 1))
            [f1].Offset(0, -x).Resize(y, 1) = arr
            x = x + 1
            y = 0
        End If
    Next j
End Sub
'---------------------------------------------------------------------------
Sub 第二题()
    Dim r As Integer
    Dim i As Integer
    Dim mergestr As String
    Dim mergecot As Integer
    If [a3] <> "" Then
        Application.DisplayAlerts = False
        With Sheet2
            r = .Cells(Rows.Count, 1).End(xlUp).Row
            For i = r To 2 Step -1
                If .Cells(i, 1).Value = .Cells(i - 1, 1).Value Then
                    .Range(.Cells(i - 1, 1), .Cells(i, 1)).Merge
                End If
            Next
        End With
        Application.DisplayAlerts = True
    Else
        With Sheet2
            r = .Cells(.Rows.Count, 1).End(xlUp).Row
            For i = 2 To r
                mergestr = .Cells(i, 1).Value
                mergecot = .Cells(i, 1).MergeArea.Count
                .Cells(i, 1).UnMerge
                .Range(.Cells(i, 1), .Cells(i + mergecot - 1, 1)).Value = mergestr
                i = i + mergecot - 1
            Next
        End With
    End If
End Sub
回复

使用道具 举报

发表于 2012-4-22 23:29 | 显示全部楼层
A学委:qushui
  1. Sub 第一题_单击()
  2.     Dim rg1 As Range, rg2 As Range, f As String, x As Integer
  3.     With Sheets("第一题")
  4.         .Range(.Cells(1, 2), .Cells(10, 50)).Clear
  5.         Set rg1 = .Columns(1).Find(what:="A", after:=.[a1], Lookat:=xlWhole, MatchCase:=True)
  6.         f = rg1.Address
  7.         x = 1
  8.         Do
  9.             x = x + 1
  10.             Set rg2 = .Columns(1).FindNext(rg1)
  11.             If rg2.Address <> f Then
  12.                 .Range(rg1, rg2.Offset(-1, 0)).Copy .Cells(1, x)
  13.                 Set rg1 = rg2
  14.             Else
  15.                 .Range(rg1, .[a65536].End(3)).Copy .Cells(1, x)
  16.             End If
  17.         Loop Until rg2.Address = f
  18.     End With
  19. End Sub
  20. [code]Sub 第二题_单击()
  21.     Dim m%, n%, i%
  22.     Application.DisplayAlerts = False
  23.     With Sheets("第二题")
  24.         m = 2
  25.         For i = 2 To .[a65536].End(3).Row - 1
  26.             If .Cells(i + 1, 1) <> "" And .Cells(i, 1) <> .Cells(i + 1, 1) Then
  27.                 n = i
  28.                 If .Range(.Cells(m, 1), .Cells(n, 1)).MergeCells Then
  29.                     .Range(.Cells(m, 1), .Cells(n, 1)).UnMerge
  30.                     .Range(.Cells(m, 1), .Cells(n, 1)) = .Cells(m, 1).Value
  31.                 Else
  32.                     .Range(.Cells(m, 1), .Cells(n, 1)).Merge
  33.                 End If
  34.                 m = i + 1
  35.             End If
  36.             If i = .[a65536].End(3).Row - 1 Then
  37.                 If .Cells(m, 1).MergeArea.Address <> .Cells(m, 1).Address Then
  38.                     .Cells(m, 1).MergeArea.Select
  39.                     Selection.UnMerge
  40.                     Selection = .Cells(m, 1).Value
  41.                 Else
  42.                     .Range(.Cells(m, 1), .[a65536].End(3)).Merge
  43.                 End If
  44.             End If
  45.         Next i
  46.     End With
  47.     Application.DisplayAlerts = True
  48. End Sub
复制代码
[/code]

vba第12课作业.zip

13.41 KB, 下载次数: 6

回复

使用道具 举报

发表于 2012-4-23 14:42 | 显示全部楼层
第2题的思路实在是不满意 vba第12课作业.rar (12.68 KB, 下载次数: 2)
回复

使用道具 举报

发表于 2012-4-23 14:54 | 显示全部楼层
Sub 按钮2_单击() '题二
Application.DisplayAlerts = False
Dim i, rng As Range, x
With Range("a2:a" & Range("a" & Rows.Count).End(xlUp).Row + 1)
    If IsNull(.MergeCells) = False Then    '合并
        Set rng = [a2]
        For i = 2 To .Rows.Count
            If Cells(i, 1) = Cells(i + 1, 1) Then
                Set rng = Union(rng, Cells(i + 1, 1))
            Else
                rng.Merge
                Set rng = Cells(i + 1, 1)
            End If
         Next i
    Else                                   '拆分
        .UnMerge
        x = [a2]: Set rng = Nothing
        For Each rng In Range("a3:a" & Range("a" & Rows.Count).End(xlUp).Row + 1)
            If rng = "" Then
                rng = x
            Else
                x = rng
            End If
        Next rng
    End If
Application.DisplayAlerts = True
End With
End Sub

Sub 第一题_按钮2_单击()
Dim 始 As Range, 末 As Range, 保持 As Range
Dim 查找值总数&, 查找值$, 循环&
查找值 = "A"                                                                   ' 可用inputbox来引入值
查找值总数 = WorksheetFunction.CountIf(Range("a1").CurrentRegion, 查找值)
With Range("a1").CurrentRegion
    Set 始 = .Find(查找值, LOOKAT:=xlWhole)                 '赋值区域中第一个符合条件的单元格
    If 始 Is Nothing Then                                                  '如果区域中查找不着的处理
        MsgBox "区域中没有查找值" & 查找值
        Exit Sub
    Else
        Set 保持 = 始                                                        '赋值第一个符合条件的单元格(此变量固定不动)
        For 循环 = 3 To 2 + 查找值总数                           '建立循环
            Set 末 = .FindNext(始)                                      '从第一个查找到后面继续查找下一个
            If 保持.Row = 末.Row Then                               '如果下一个查找到的单元格的行和第一个单元格一样
                始.Resize(.Rows.Count - 始.Row + 1).Copy   '复制当前的 始 至当前区域的末单元格区域
            Else
                始.Resize(末.Row - 始.Row).Copy                 '复制当前的 始 至 当前的 末 之间的单元格区域
                Set 始 = 末                                                  '重置变量 始
            End If
            Cells(1, 循环).Select                                          '粘贴
            ActiveSheet.Paste
        Next 循环
    End If
End With
End Sub
yu20078-第12课作业.rar (9.33 KB, 下载次数: 6)
回复

使用道具 举报

发表于 2012-4-23 15:54 | 显示全部楼层
Sub 按钮2_单击()
Dim q, i, n As Long
Dim rg As Range
Dim str As String
If IsNull(Range("a:a").MergeCells) Then
q = 1
Do
q = q + 1
If Range("a" & q).MergeCells = True Then
str = Range("a" & q).MergeArea.Address
Range("a" & q).UnMerge

Range(str) = Range("a" & q)
End If
Loop Until Range("a" & q) = ""

Else
Set rg = Range("a1")
Application.DisplayAlerts = False
Do
i = rg.Row + 1
Set rg = Range("A:A").Find(what:=rg.Offset(1, 0), after:=Range("a65536"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
n = rg.Row
Range(Cells(i, 1), Cells(rg.Row, 1)).Merge
Loop Until rg.Offset(1, 0) = ""
Application.DisplayAlerts = True
End If
End Sub
Sub 第一题_按钮2_单击()
Dim i, n As Long
Dim rg As Range
n = 1
Set rg = Range("a:a").Find("A", after:=Range("a1"))
Do
n = n + 1
i = rg.Row
Set rg = Range("A:a").Find("A", after:=rg.Offset(1, 0))
If rg.Row < i Then
   Range("a" & i & ":A" & Range("a" & Rows.Count).End(xlUp).Row).Copy Range("a1").Offset(0, n)
   Else
Range("a" & i & ":A" & rg.Row - 1).Copy Range("a1").Offset(0, n)
End If

Loop Until rg.Row < i
End Sub

vba第12课作业.rar

12.62 KB, 下载次数: 2

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 21:25 , Processed in 0.385600 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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