如何向当前范围添加列

如何向当前范围添加列

有没有 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

在此处输入图片描述

相关内容