Excel精英培训网

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

[已解决]【求助】对批量选取整行的续问

[复制链接]
发表于 2009-11-10 17:31 | 显示全部楼层 |阅读模式

原题:[已解决]变量选择整行的问题.

http://www.excelpx.com/forum.php?mod=viewthread&tid=99445

内容:工作表的A列有数值9,找到这几个9且选择整行,用V实现.

解答:

Sub test()
    Dim Ro As Integer
    Dim Rng As Range
    Dim RngCount As Integer
    For Ro = 1 To [A65536].End(xlUp).Row
        If Cells(Ro, 1).Value = 9 Then
            RngCount = RngCount + 1
            If RngCount = 1 Then
                Set Rng = Cells(Ro, 1)
            Else
                Set Rng = Union(Rng, Cells(Ro, 1))
            End If
        End If
    Next
    Rng.EntireRow.Select
    Set Rng = Nothing
End Sub

请老师指教的是:不是选择整行,而是都选择第1到6列

或者是

选择第1到6列及第9到16列怎么改变上面的答案(这里是同时选择)??

[此贴子已经被作者于2009-11-10 19:06:00编辑过]
最佳答案
2009-11-10 19:21
Sub aa()
    Dim Ro As Integer
    Dim Rng As Range
    Dim RngCount As Integer
    For Ro = 1 To [A65536].End(xlUp).Row
        If Cells(Ro, 1).Value = 9 Then
            RngCount = RngCount + 1
            If RngCount = 1 Then
                Set Rng = Union(Range(Cells(Ro, 1), Cells(Ro, 6)), Range(Cells(Ro, 9), Cells(Ro, 16)))
            Else
                Set Rng = Union(Rng, Range(Cells(Ro, 1), Cells(Ro, 6)))
                Set Rng = Union(Rng, Range(Cells(Ro, 9), Cells(Ro, 16)))
            End If
        End If
    Next
    Rng.Select
    Set Rng = Nothing
End Sub
发表于 2009-11-10 17:49 | 显示全部楼层

把Set Rng = Union(Rng, Cells(Ro, 1))改成

 Set Rng = Union(Rng, range(Cells(Ro, 1),cells(ro,6))

Set Rng = Union(Rng, range(Cells(Ro, 9),cells(ro,16))

上边一句替换成下边2句试试

把Rng.EntireRow.Select

改成Rng.Select




回复

使用道具 举报

 楼主| 发表于 2009-11-10 18:36 | 显示全部楼层

QUOTE:
以下是引用搁浅2008在2009-11-10 17:49:00的发言:

把Set Rng = Union(Rng, Cells(Ro, 1))改成

 Set Rng = Union(Rng, range(Cells(Ro, 1),cells(ro,6))

Set Rng = Union(Rng, range(Cells(Ro, 9),cells(ro,16))

上边一句替换成下边2句试试

把Rng.EntireRow.Select

改成Rng.Select




怎么写上去这2句出现为红色字符呢??

提示:语法错误

提示:缺少列表分隔符或)

    Dim Ro As Integer
    Dim Rng As Range
    Dim RngCount As Integer
    For Ro = 1 To [A65536].End(xlUp).Row
        If Cells(Ro, 1).Value = 9 Then
            RngCount = RngCount + 1
            If RngCount = 1 Then
                Set Rng = Cells(Ro, 1)
            Else
Set Rng = Union(Rng,range(Cells(Ro,1),cells(ro,6))
Set Rng = Union(Rng,range(Cells(Ro,9),cells(ro,16))

            End If
        End If
    Next
    Rng.Select
    Set Rng = Nothing
End Sub

回复

使用道具 举报

发表于 2009-11-10 19:21 | 显示全部楼层    本楼为最佳答案   

Sub aa()
    Dim Ro As Integer
    Dim Rng As Range
    Dim RngCount As Integer
    For Ro = 1 To [A65536].End(xlUp).Row
        If Cells(Ro, 1).Value = 9 Then
            RngCount = RngCount + 1
            If RngCount = 1 Then
                Set Rng = Union(Range(Cells(Ro, 1), Cells(Ro, 6)), Range(Cells(Ro, 9), Cells(Ro, 16)))
            Else
                Set Rng = Union(Rng, Range(Cells(Ro, 1), Cells(Ro, 6)))
                Set Rng = Union(Rng, Range(Cells(Ro, 9), Cells(Ro, 16)))
            End If
        End If
    Next
    Rng.Select
    Set Rng = Nothing
End Sub
回复

使用道具 举报

 楼主| 发表于 2009-11-10 19:26 | 显示全部楼层

QUOTE:
以下是引用搁浅2008在2009-11-10 19:21:00的发言:
Sub aa()
    Dim Ro As Integer
    Dim Rng As Range
    Dim RngCount As Integer
    For Ro = 1 To [A65536].End(xlUp).Row
        If Cells(Ro, 1).Value = 9 Then
            RngCount = RngCount + 1
            If RngCount = 1 Then
                Set Rng = Union(Range(Cells(Ro, 1), Cells(Ro, 6)), Range(Cells(Ro, 9), Cells(Ro, 16)))
            Else
                Set Rng = Union(Rng, Range(Cells(Ro, 1), Cells(Ro, 6)))
                Set Rng = Union(Rng, Range(Cells(Ro, 9), Cells(Ro, 16)))
            End If
        End If
    Next
    Rng.Select
    Set Rng = Nothing
End Sub

感觉原来或者上面的命令式可以简短的。

应该一句if   then  结果就行了

怎么还有个ELSE来做判断有2种选择结果呢??????????

回复

使用道具 举报

发表于 2009-11-10 19:31 | 显示全部楼层

我只是比着上一个改的 我没有试验行不行[em04]
回复

使用道具 举报

发表于 2009-11-10 19:33 | 显示全部楼层

 Set Rng = Union(Rng, Range(Cells(Ro, 1), Cells(Ro, 6)))

这里的rng要有一个初始值所以才要加的

回复

使用道具 举报

 楼主| 发表于 2009-11-10 19:34 | 显示全部楼层

QUOTE:
以下是引用搁浅2008在2009-11-10 19:31:00的发言:
我只是比着上一个改的 我没有试验行不行[em04]

谢谢,找DJ问问

------dj没有在[em06]
[此贴子已经被作者于2009-11-10 19:40:02编辑过]
回复

使用道具 举报

 楼主| 发表于 2009-11-10 19:39 | 显示全部楼层

QUOTE:
以下是引用搁浅2008在2009-11-10 19:33:00的发言:

 Set Rng = Union(Rng, Range(Cells(Ro, 1), Cells(Ro, 6)))

这里的rng要有一个初始值所以才要加的

可以想个方法,把初始值与后面的都找到,然后一次选取不就好了,就没有else,命令也简短了,搁浅老师帮帮忙了。
回复

使用道具 举报

发表于 2009-11-10 22:56 | 显示全部楼层

Sub test()
   j = 1
  For i = 1 To [A65536].End(xlUp).Row
         If Sheets("Sheet2").Cells(i, 1).Value = 9 Then
          Sheets("Sheet1").Cells(j, 1).Resize(1, 6).Value = Sheets("Sheet2").Cells(i, 1).Resize(1, 6).Value
           Sheets("Sheet1").Cells(j, 9).Resize(1, 2).Value = Sheets("Sheet2").Cells(i, 9).Resize(1, 2).Value
           j = j + 1
           End If
    Next
End Sub
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-15 18:24 , Processed in 0.311864 second(s), 5 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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