单行 VBA 代码包含多个范围

单行 VBA 代码包含多个范围

我是否必须将每个单元格区域放在单独的行中,就像这样

Range("A6:B" & Cells.SpecialCells(xlCellTypeLastCell).Row).HorizontalAlignment = xlLeft
Range("G6:H" & Cells.SpecialCells(xlCellTypeLastCell).Row).HorizontalAlignment = xlLeft

或者我能否以某种方式将不相邻的列组(从某个单元格一直向下)包含在一行中?下面的代码似乎对我不起作用。

Range("A6:B,G6:H" & Cells.SpecialCells(xlCellTypeLastCell).Row).HorizontalAlignment = xlLeft

答案1

您可以使用Union

Union(Range("A6:B" & Cells.SpecialCells(xlCellTypeLastCell).Row), Range("G6:H" & Cells.SpecialCells(xlCellTypeLastCell).Row)).HorizontalAlignment = xlLeft

相关内容