Excel精英培训网

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

[通知] 统计VBA学习小组正式组第九课(第十一讲)的积分帖之作业上交贴(第12周)

  [复制链接]
发表于 2012-3-15 22:33 | 显示全部楼层

Sub selects()

Dim rg As Range, x As Integer, a As Integer

a = 0

   For x = 2 To 11
   
      If Cells(x, "d") > 20 Then
      
      a = a + 1
   
         If a = 1 Then
         
            Set rg = Rows(x)
            
            Else
            
          Set rg = Union(Rows(x), rg)
            
         End If
         
        End If
        
     Next
     
   rg.Select
  
End Sub







VBA基础入门第10课作业题.rar

6.01 KB, 下载次数: 3

评分

参与人数 1金币 +7 收起 理由
兰色幻想 + 7 赞一个!

查看全部评分

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2012-3-15 22:58 | 显示全部楼层
A09:byhdch
方法1
Sub 同时选取金额大于20的行1()
  Sheets("sheet3").Select
    Dim rg As Range, i As Integer
      For i = 2 To Range("d65536").End(xlUp).Row
        If Cells(i, 4).Value > 20 Then
            If rg Is Nothing Then
            Set rg = Rows(i)
        Else
           Set rg = Union(rg, Rows(i))
            End If
        End If
     Next i
   rg.Select
End Sub

方法2
Sub 同时选取金额大于20的行2()
  Sheets("sheet3").Select
    Dim rg As Range, i As Integer
      For i = 2 To Range("d65536").End(xlUp).Row
        If Range("d" & i).Value > 20 Then
            If rg Is Nothing Then
            Set rg = Range("d" & i)
        Else
            Set rg = Union(rg, Range("d" & i))
            End If
        End If
     Next i
   rg.EntireRow.Select
End Sub

方法3
Sub 同时选取金额大于20的行3()
  Sheets("sheet3").Select
    Dim rg As Range, i As Integer
      For i = 2 To Range("d65536").End(xlUp).Row
        If Cells(i, 4).Value > 20 Then
            If rg Is Nothing Then
            Set rg = Cells(i, 4)
        Else
           Set rg = Union(rg, Cells(i, 4))
            End If
        End If
     Next i
   rg.EntireRow.Select
End Sub

第10课作业题 A09byhdch.rar (7.99 KB, 下载次数: 7)

评分

参与人数 1金币 +10 收起 理由
兰色幻想 + 10

查看全部评分

回复

使用道具 举报

发表于 2012-3-16 11:44 | 显示全部楼层
21组 ID号:jxncfxsf
Sub 选取符合条件的单元格1()
Dim x, y As Long
Dim st As String
For x = 2 To Range("d" & Rows.Count).End(xlUp).Row
  If Cells(x, 4).Value > 20 Then
     st = st & x & ":"
     For y = x + 1 To Range("d" & Rows.Count).End(xlUp).Row
      If Cells(y, 4) > 20 Then
       x = x + 1
       Else: Exit For
         End If
        Next y
        st = st & x & ","
        End If
     Next x
st = Left(st, Len(st) - 1)
Range(st).Select
End Sub

Sub 选取符合条件的单元格2()
  Dim x As Long
  Dim rg As Range
  For x = 2 To Range("d" & Rows.Count).End(xlUp).Row
   If Cells(x, 4) > 20 Then
   If rg Is Nothing Then
      Set rg = Rows(x)
      Else
      Set rg = Union(rg, Rows(x))
     End If
     End If
     Next x
     rg.Select
End Sub

Sub 选取符合条件的单元格3()
  Dim x As Long
  Dim rg As Range
  For x = 2 To Range("d" & Rows.Count).End(xlUp).Row
   If Cells(x, 4) > 20 Then
   If rg Is Nothing Then
   Set rg = Cells(x, 1).Resize(1, Columns.Count)
   Else
   Set rg = Union(rg, Cells(x, 1).Resize(1, Columns.Count))
     rg.Select
     End If
     End If
     Next x
    End Sub

VBA基础入门第10课作业题.rar

14.71 KB, 下载次数: 7

评分

参与人数 1金币 +10 收起 理由
兰色幻想 + 10

查看全部评分

回复

使用道具 举报

发表于 2012-3-16 14:14 | 显示全部楼层
只会2种
Sub range方法()
Dim i As Byte
Dim str As String
    For i = 2 To 11
        If Cells(i, 4) > 20 Then
            str = str & i & ":" & i & ","
        End If
    Next i
    str = Left(str, Len(str) - 1)
    Range(str).Select
End Sub

Sub union方法()
Dim i As Byte
Dim rg As Range
    For i = 2 To 11
        If Cells(i, 4) > 20 Then
            If rg Is Nothing Then
                Set rg = Rows(i)
            Else
                Set rg = Union(rg, Rows(i))
            End If
        End If
    Next i
    rg.Select
End Sub

评分

参与人数 1金币 +10 收起 理由
兰色幻想 + 10

查看全部评分

回复

使用道具 举报

发表于 2012-3-16 14:51 | 显示全部楼层
  1. Sub RgSelect1()     'range选择法

  2. Dim i As Integer
  3. Dim rg As Range
  4. Dim rowstr As String

  5. i = [D65536].End(xlUp).Row

  6. rowstr = ""

  7. For Each rg In Range("D2:D" & i)
  8.     If rg > 20 Then
  9.         If rowstr = "" Then
  10.             rowstr = rowstr & rg.Row & ":" & rg.Row
  11.         Else
  12.             rowstr = rowstr & "," & rg.Row & ":" & rg.Row
  13.         End If
  14.     End If
  15. Next

  16. Range(rowstr).Select

  17. End Sub

  18. Sub RgSelect2()     'union选择法

  19. Dim i As Integer
  20. Dim j As Integer
  21. Dim rg As Range
  22. Dim rgs As Range

  23. i = [D65536].End(xlUp).Row

  24. j = 0

  25. For Each rg In Range("D2:D" & i)
  26.     If rg > 20 Then
  27.         If j = 0 Then
  28.             Set rgs = rg
  29.             j = j + 1
  30.         Else
  31.             Set rgs = Union(rgs, rg)
  32.         End If
  33.     End If
  34. Next

  35. rgs.EntireRow.Select

  36. End Sub

  37. Sub RgSelect3()     'rows选择法

  38. Dim i As Integer
  39. Dim j As Integer
  40. Dim rg As Range
  41. Dim rgs As Range


  42. i = [D65536].End(xlUp).Row

  43. j = 0

  44. For Each rg In Range("D2:D" & i)
  45.     If rg > 20 Then
  46.         If j = 0 Then
  47.             Set rgs = Rows(rg.Row & ":" & rg.Row)
  48.             j = j + 1
  49.         Else
  50.             Set rgs = Union(rgs, Rows(rg.Row & ":" & rg.Row))
  51.         End If
  52.     End If
  53. Next

  54. rgs.Select

  55. End Sub
复制代码

VBA基础入门第10课作业题.zip

15.13 KB, 下载次数: 3

评分

参与人数 1金币 +10 收起 理由
兰色幻想 + 10 赞一个!

查看全部评分

回复

使用道具 举报

发表于 2012-3-16 15:39 | 显示全部楼层

Sub 成range()
    Dim x As Integer
    Dim y
    For x = 2 To 11
        If Range("d" & x).Value > 20 Then
            If y = "" Then
                y = Range("d" & x).Address
            Else
                y = y & "," & Range("d" & x).Address
            End If
        End If
    Next x
    Range(y).EntireRow.Select
End Sub


Sub 成union()
    Dim x As Integer
    Dim y As Range
    Dim k As Range
    For x = 2 To 11
        If Range("d" & x).Value > 20 Then
            Set y = Range("d" & x)
            If k Is Nothing Then
                Set k = y
            Else
                Set k = Union(k, y)
            End If
        End If
    Next x
    k.EntireRow.Select
End Sub

Sub 成rows()
    Dim x As Integer
    Dim y As Range
    For x = 2 To 11
        If Range("d" & x).Value > 20 Then
            If y Is Nothing Then
                Set y = Rows(x)
            Else
                Set y = Union(y, Rows(x))
            End If
        End If
    Next x
    y.Select
End Sub

第10课作业题A06 梅一枝.zip (13.28 KB, 下载次数: 3)

评分

参与人数 1金币 +10 收起 理由
兰色幻想 + 10

查看全部评分

回复

使用道具 举报

发表于 2012-3-16 15:46 | 显示全部楼层
range方法
  1. Sub t101()
  2.     Range("3:3, 5:5, 10:11").Select
  3. End Sub
复制代码
rows方法
  1. Sub t102()
  2.     Union(Rows("3:3"), Rows("5:5"), Rows("10:11")).Select
  3. End Sub
复制代码
union方法
  1. Sub t103()
  2.     Union(Range("3:3"), Range("5:5"), Range("10:11")).Select
  3. End Sub
复制代码
请老师批改指点,非常感谢!

点评

如果能自动找出行最好  发表于 2012-3-20 09:20

评分

参与人数 1金币 +3 收起 理由
兰色幻想 + 3

查看全部评分

回复

使用道具 举报

发表于 2012-3-17 13:46 | 显示全部楼层
兰版:作业里要求用Range,row和union三种选取方法,这3个方法我都用上了,但体现在两个程序里,第3种程序的实现方法想不出来了,不知道可不可以算通过。
Sub RangSelect()
    Dim sAddress As String
    Dim rg As Range
    For Each rg In Range("d2:d11")
        If rg.Value > 20 Then
            If sAddress = "" Then
                sAddress = rg.Address
            Else
                sAddress = sAddress & "," & rg.Address
            End If
        End If
    Next rg
    sAddress = "" & sAddress & ""
    Range(sAddress).EntireRow.Select
End Sub
Sub UnionSelect()
    Dim sAddress As String
    Dim rg, rg1 As Range
    For Each rg In Range("d2:d11")
        If rg.Value > 20 Then
            If rg1 Is Nothing Then
                Set rg1 = Rows(rg.Row)
            Else
                Set rg1 = Union(rg1, Rows(rg.Row))
            End If
        End If
    Next rg
    rg1.Select
End Sub
VBA基础入门第10课作业题.rar (7.52 KB, 下载次数: 3)

评分

参与人数 1金币 +10 收起 理由
兰色幻想 + 10

查看全部评分

回复

使用道具 举报

发表于 2012-3-17 15:41 | 显示全部楼层
我也做好了作业,不知是否正确?请批改。谢谢! 兰江自由鱼__VBA基础入门第10课作业题.rar (13.03 KB, 下载次数: 3)

评分

参与人数 1金币 +10 收起 理由
兰色幻想 + 10

查看全部评分

回复

使用道具 举报

发表于 2012-3-17 17:37 | 显示全部楼层
A07:一缕忧兰

  1. Sub 方法1()
  2. Dim i As Integer
  3. Dim m
  4. For i = 2 To 11
  5. If Range("D" & i).Value > 20 Then
  6.     If m = "" Then
  7.         m = Range("D" & i).Address
  8.     Else
  9.         m = m & "," & Range("D" & i).Address
  10.     End If
  11.   End If
  12. Next i
  13. Range(m).EntireRow.Select
  14. End Sub

  15. Sub 方法2()
  16. Dim i
  17. Dim R As Range
  18. For i = 2 To 11
  19. If Cells(i, "D") > 20 Then
  20.   If R Is Nothing Then
  21.     Set R = Range("D" & i)
  22.   Else
  23.     Set R = Application.Union(R, Range("D" & i))
  24.   End If
  25. End If
  26. Next i
  27. R.EntireRow.Select
  28. End Sub

  29. Sub 方法3()
  30. Dim i
  31. Dim R As Range
  32. For i = 2 To 11
  33. If Cells(i, "D") > 20 Then
  34.   If R Is Nothing Then
  35.     Set R = Rows(i)
  36.   Else
  37.     Set R = Application.Union(R, Rows(i))
  38.   End If
  39. End If
  40. Next i
  41. R.Select
  42. End Sub
复制代码
VBA基础入门第10课作业题.zip (12.86 KB, 下载次数: 5)

评分

参与人数 1金币 +10 收起 理由
兰色幻想 + 10

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 01:53 , Processed in 0.681199 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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