Excel VBA:选择包含数据的列旁边的列,然后插入3列 [英] Excel VBA: Select the column next to the column with data and then insert 3 columns

查看:114
本文介绍了Excel VBA:选择包含数据的列旁边的列,然后插入3列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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!

谢谢!

推荐答案

您可以将整个代码替换为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

这篇关于Excel VBA:选择包含数据的列旁边的列,然后插入3列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆