Excel VBA: Select the column next to the column wi

2019-09-13 05:34发布

Sub New
ActiveSheet.Range(“c9”).End(xlRight).Offset(1,0).Select
Selection.Insert Shift:xlToRight
Selection.Insert Shift:xlToRight
Selection.Insert Shift:xlToRight
End Sub

This doesn't work at all and gives me an error. Any help would be greatly appreciated!

Thanks!

1条回答
【Aperson】
2楼-- · 2019-09-13 06:28

You can replace your entire code with 1 line:

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

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

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)

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
查看更多
登录 后发表回答