如果所选范围为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
行或区域的情况下这样做。
答案 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