作者:绿色风
分类:
时间:2022-08-18
浏览:110
楼主 xyh9999 |
Q:怎样使得用For Each历遍Range("C1:D2,D2:E3")中的单元格时不重复呢? A: 方法一: gvntw的代码:
- Sub test()
- Dim c1 As Range, c2 As Range, c3 As Range, c4 As Range, c5 As Range
- Range("A:A") = Empty
- Set c1 = Range("C1:D2")
- Set c2 = Range("D2:E3")
- Set c3 = Range("A1")
- For Each c4 In c1
- c3 = c4.Address
- Set c3 = c3(2)
- Next
- For Each c4 In c2
- Set c5 = Application.Intersect(c1, c4)
- If c5 Is Nothing Then
- c3 = c4.Address
- Set c3 = c3(2)
- End If
- Next
- End Sub
方法二:
- Sub testxx()
- Cells.ClearContents
- Range("C1:D2,D2:E3").Select
- TempCC = "+"
- For Each lsRang In Range("C1:D2,D2:E3")
- If InStr(TempCC, "+" & lsRang.Address & "+") = 0 Then Cells(Cells(Rows.Count, "A").End(xlUp).Row + 1, 1).Value = lsRang.Address: TempCC = TempCC & lsRang.Address & "+"
- Next lsRang
- End Sub
方法三: kevinchengcw的代码:
- sub test()
- dim Dic
- dim Rng as range
- set dic=createobject("scripting.dictionary")
- for each rng in Range("C1:D2,D2:E3")
- if not dic.exists(rng.address) then
- rng=rng+1
- dic.add rng.address,""
- end if
- next rng
- set dic=nothing
- end sub
|
2楼 xyf2210 |
学习 |
免责声明
有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素,
经与ExcelTip.Net站长Apolloh商议并征得其同意,
现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示,
供有需要的人士查询使用,也慰缅曾经的论坛时代。
所示各个帖子的原作者如对版权有异议,
可与本人沟通提出,或于本站点留言,我们会尽快处理。
在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一