ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1496|回复: 9

[讨论] Find与 Set Range 组合应用。

[复制链接]

TA的精华主题

TA的得分主题

发表于 2009-10-22 14:43 | 显示全部楼层 |阅读模式
数组源
A        B
aa        11
bb        12
aa        13
cc        14
aa        15
dd        16
ee        17
ff        18
aa        19

用find函数 .Find("aa", , , xlWhole)
Set rr 得到的Range结果如下
aa        13
aa        15
aa        19

TA的精华主题

TA的得分主题

发表于 2009-10-22 14:48 | 显示全部楼层
可以用字典或ADO
用find可用after参数或findnext

TA的精华主题

TA的得分主题

 楼主| 发表于 2009-10-22 15:00 | 显示全部楼层
原帖由 lb_bn 于 2009-10-22 14:48 发表
可以用字典或ADO
用find可用after参数或findnext


目标需求不连续的Range

TA的精华主题

TA的得分主题

发表于 2009-10-22 15:03 | 显示全部楼层
建议楼主上传附件。
示意想要得到的结果。

TA的精华主题

TA的得分主题

 楼主| 发表于 2009-10-22 15:12 | 显示全部楼层
在A列中,满足aa的结果生成一个oRange
for eache ss in oRange

Book2.rar

4.53 KB, 下载次数: 8

TA的精华主题

TA的得分主题

发表于 2009-10-22 15:20 | 显示全部楼层
如果数据多,用字典最快,不过一般用find确定行数,然后用UNION(rr, Rows(i)) 就可以了啊

TA的精华主题

TA的得分主题

 楼主| 发表于 2009-10-22 15:45 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub 文本定位()
  Dim aa As Range
  Set rr = Range("A1:A9")

  For Each ss In rr
    Debug.Print ss
  Next

  For Each a In rr
    If a Like "*tt*" Then
      If aa Is Nothing Then
        Set aa = a.Cells
      Else
        Set aa = Union(aa, a.Cells)
      End If
    End If
  Next
  aa.EntireRow.Select
End Sub
http://club.excelhome.net/thread-191495-1-1.html
Book2.rar (9.89 KB, 下载次数: 4)

[ 本帖最后由 ningyong58 于 2009-10-22 16:03 编辑 ]

TA的精华主题

TA的得分主题

发表于 2009-10-22 15:53 | 显示全部楼层
仿照FIND的HELP文档写的。

Dim rng As Range, i As Long, r As Long
Sub test()
With ActiveSheet.Range("A:A")
Set rr = .Find("aa")
If Not rr Is Nothing Then
    firstAddress = rr.Address
        Do
            If rng Is Nothing Then
            Set rng = Range("A" & rr.Row & ":" & "B" & rr.Row)
            Else: Set rng = Union(rng, Range("A" & rr.Row & ":" & "B" & rr.Row))
            End If
            Set rr = .FindNext(rr)
        Loop While Not rr Is Nothing And rr.Address <> firstAddress
rng.Copy [e11]
End If
End With
End Sub

Book2.zip

8.15 KB, 下载次数: 20

TA的精华主题

TA的得分主题

 楼主| 发表于 2009-10-22 16:30 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
原帖由 blazetwf 于 2009-10-22 15:53 发表
仿照FIND的HELP文档写的。

Dim rng As Range, i As Long, r As Long
Sub test()
With ActiveSheet.Range("A:A")
Set rr = .Find("aa")
If Not rr Is Nothing Then
    firstAddress = rr.Address
        D ...


谢谢,回复
rng.Copy [e11] --[e11]是什么意思。

Dim rng As Range, i As Long, r As Long

Sub test()

With ActiveSheet.Range("A:A")

Set rr = .Find("aa")

If Not rr Is Nothing Then
    firstAddress = rr.Address
        Do
            If rng Is Nothing Then
            Set rng = Range("A" & rr.Row & ":" & "B" & rr.Row)
            Else: Set rng = Union(rng, Range("A" & rr.Row & ":" & "B" & rr.Row))
            End If
            Set rr = .FindNext(rr)
        Loop While Not rr Is Nothing And rr.Address <> firstAddress

rng.Copy [e11]
For Each rrr In rng
  rrr.Interior.ColorIndex = 34
Next

End If
End With

End Sub

  rrr.Interior.ColorIndex = 34
只能用循环变色吗??

TA的精华主题

TA的得分主题

发表于 2009-10-22 17:20 | 显示全部楼层
不用循环啊,直接在LOOP 下面那行加上
rng.Interior.ColorIndex = 34

rng.copy [e11]就是把刚才UNION了的RANGE复制到E11单元格。。。如果你不想复制单元格样式,可以这样
rng.Interior.ColorIndex = 34
rng.Copy
[e11].PasteSpecial Paste:=xlPasteValues
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-11 16:39 , Processed in 0.043574 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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