我需要遍历标题行,并根据特定条件在左侧插入整列。
Sub InsertCol()
Dim i As Integer
For i = 1 To 20
If i Mod 2 = 0 Then
Cells(1, i).Columns.Insert ' this only inserts a cell..need an entire column
End If
Next i
End Sub
答案 0 :(得分:1)
您需要使用Columns(i).Insert
Sub InsertCol()
Dim i As Integer
For i = 1 To 20
If i Mod 2 = 0 Then
Columns(i).Insert
End If
Next i
End Sub
请注意,由于您要插入列,因此您可能需要向后退一步:
Sub InsertCol()
Dim i As Integer
For i = 20 To 1 Step -1
If i Mod 2 = 0 Then
Columns(i).Insert
End If
Next i
End Sub
答案 1 :(得分:0)
您可能正在寻找的东西更像
Columns("B:B").Insert Shift:=xlToLeft
此将插入到左侧指定范围的一个柱(“B:B”)。在我的例子
您也不需要参考完整范围,您可以仅参考i。
Columns(i).Insert Shift:=xlToLeft