可能的方法很少,但我不确定哪种方法最适合您。我的第一个可能也是最简单的建议如下:
Set rngData = shtData.Range(Columns(1), Columns(2), Columns(5))
set rngData = Intersect(rngData, shtData.Rows("5:10"))
但是,您当前的范围无论如何都不起作用。你可以改用这样的东西:
Set rngData = shtData.Range("A:B, E:E")
或者,如果您需要保留列的数字索引,则可以采用这种方式(完整代码):
Set rngData = Union(shtData.Columns(1), shtData.Columns(2), shtData.Columns(5))
set rngData = Intersect(rngData, shtData.Rows("5:10"))
编辑-扩展和解释:见下面的子程序和里面的注释
Sub Test_Selection()
'Variant A- select range from column 1 to 5
ActiveSheet.Range(Columns(1), Columns(5)).Select
'Variant B- select range of columns: 1, 2 and 5
With ActiveSheet
Union(.Columns(1), .Columns(2), .Columns(5)).Select
End With
'This will not work
ActiveSheet.Range(Columns(1), Columns(2), Columns(5)).Select
End Sub
因此,上面的变体 B 能够像这样选择不连续的范围:
对于所有感兴趣的人,关于 Intesect 的最终解释。此过程将给出如下图所示的结果:
Sub Final_Combination()
Dim shtData As Worksheet
Set shtData = ActiveSheet
Dim rngData As Range
Set rngData = Union(shtData.Columns(1), shtData.Columns(2), shtData.Columns(5))
Set rngData = Intersect(rngData, shtData.Rows("5:10"))
'or simply combined in one line of code
Set rngData = Intersect(Union(shtData.Columns(1), _
shtData.Columns(2), _
shtData.Columns(5)), _
Rows("5:10"))
rngData.Select
End Sub