且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Excel VBA:选择包含数据的列旁边的列,然后插入3列

更新时间:2023-12-01 09:35:28

您可以将整个代码替换为1行:

You can replace your entire code with 1 line:

ActiveSheet.Range("C9").Offset(0, 1).Resize(, 3).EntireColumn.Insert

第一部分 ActiveSheet.Range("C9").Offset(0,1)选择单元格"C9"右侧的单元格.

The first part ActiveSheet.Range("C9").Offset(0, 1) you select the cell on the right side of Cell "C9".

第二部分 .Resize(,3).EntireColumn.Insert 在右侧一次插入3列(而不是重复同一行3次)

The second part .Resize(, 3).EntireColumn.Insert you insert 3 columns at once on the right side (instead of repeating the same line 3 times)

如果要在第9行中找到包含数据的最后一列,例如 Range("C9").End(xlRight),请使用以下代码:

In case you meant to find the last column in row 9 with data, as in Range("C9").End(xlRight), use the code below:

With ActiveSheet
    ' find last column with data in row 9
    LastColumn = .Cells(9, .Columns.Count).End(xlToLeft).Column

    .Range(Cells(9, LastColumn), Cells(9, LastColumn)).Offset(0, 1).Resize(, 3).EntireColumn.Insert
End With