ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的酷贴 > Excel VBA > 怎样使得用For Each历遍Range("C1:D2,D2:E3")中的单元格时不重复呢?

怎样使得用For Each历遍Range("C1:D2,D2:E3")中的单元格时不重复呢?

作者:绿色风 分类: 时间:2022-08-18 浏览:76
楼主
xyh9999
Q:怎样使得用For Each历遍Range("C1:D2,D2:E3")中的单元格时不重复呢?
A:
方法一:
gvntw的代码:

  1. Sub test()
  2.     Dim c1 As Range, c2 As Range, c3 As Range, c4 As Range, c5 As Range
  3.     Range("A:A") = Empty
  4.     Set c1 = Range("C1:D2")
  5.     Set c2 = Range("D2:E3")
  6.     Set c3 = Range("A1")
  7.     For Each c4 In c1
  8.         c3 = c4.Address
  9.         Set c3 = c3(2)
  10.     Next
  11.     For Each c4 In c2
  12.         Set c5 = Application.Intersect(c1, c4)
  13.         If c5 Is Nothing Then
  14.             c3 = c4.Address
  15.             Set c3 = c3(2)
  16.         End If
  17.     Next
  18. End Sub
方法二:

  1. Sub testxx()
  2.   Cells.ClearContents
  3.   Range("C1:D2,D2:E3").Select
  4.   TempCC = "+"
  5.   For Each lsRang In Range("C1:D2,D2:E3")
  6.     If InStr(TempCC, "+" & lsRang.Address & "+") = 0 Then Cells(Cells(Rows.Count, "A").End(xlUp).Row + 1, 1).Value = lsRang.Address: TempCC = TempCC & lsRang.Address & "+"
  7.   Next lsRang
  8. End Sub


方法三:
kevinchengcw的代码:

  1. sub test()
  2. dim Dic
  3. dim Rng as range
  4. set dic=createobject("scripting.dictionary")
  5. for each rng in Range("C1:D2,D2:E3")
  6.   if not dic.exists(rng.address) then
  7.       rng=rng+1
  8.       dic.add rng.address,""
  9.   end if
  10. next rng
  11. set dic=nothing
  12. end sub
2楼
xyf2210
学习

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap