![如何向当前范围添加列](https://linux22.com/image/1580821/%E5%A6%82%E4%BD%95%E5%90%91%E5%BD%93%E5%89%8D%E8%8C%83%E5%9B%B4%E6%B7%BB%E5%8A%A0%E5%88%97.png)
有没有 Excel VBA 示例,可以向当前范围添加 2 列,然后在这些列中插入数据。谢谢。
答案1
基于原始数据集维度的一个简单示例:
Sub qwerty()
Dim r As Range, rNew As Range, rCombined As Range
Dim nLastRow As Long, nFirstRow As Long
Dim nLastColumn As Long, nFirstColumn As Long
Set r = Range("B2:D7")
nLastRow = r.Rows.Count + r.Row - 1
nLastColumn = r.Columns.Count + r.Column - 1
nFirstRow = r.Row
nFirstColumn = r.Column
Set rNew = Range(Cells(nFirstRow, nLastColumn + 1), Cells(nLastRow, nLastColumn + 2))
Set rCombined = Union(r, rNew)
r.Value = "x"
rNew.Value = "y"
MsgBox rCombined.Address
End Sub