如何在A列中选择与所选范围平行的范围(所选范围是非连续范围),而不进行循环

时间:2016-01-18 00:35:13

标签: excel vba excel-vba

如果所选范围为Rng1 = C1:D2,C4:D5,则结果范围必须为Rng2 = A1:A2,A4:A5

如果Rng1是连续的,我使用两种方法可以正常工作,但如果Rng1是非连续范围,则两种方式都不起作用:

Sub test()
  Dim Rng1 As Range, Rng2 As Range
  Set Rng1 = Range("C1:D2,C4:D5")
'first way:
   Set Rng2 = Rng1.Resize(, 1).Offset(, 1 - Rng1.Column)
'second way:
  Set Rng2 = Range(Cells(Rng1(1).Row, 1), Cells(Rng1(Rng1.Count).Row, 1))
  Rng2.Select
End Sub

我们可以在不循环Rng1行或区域的情况下这样做。

1 个答案:

答案 0 :(得分:2)

Sub test()
  Dim Rng1 As Range, Rng2 As Range
  Set Rng1 = Range("C1:D2,C4:D5")
  Set Rng1 = Rng1.EntireRow

  Set Rng2 = Intersect(Rng1, Rng1.Parent.Columns(1))

End Sub