在VBA中插入字符串和每隔一列的顺序号

时间:2016-08-31 01:59:10

标签: excel vba excel-vba

我无法弄清楚这一点。

First Few Columns of data

现在我有一堆500-1000列数据的工作表。每列旁边都是我插入的空格。我试图插入文本" Column"接下来是一个序号" Column1" "列2" "栏3"等,直到数据结束。

到目前为止,我一直试着玩

Sub insert_column_every_other()
Dim lColumn As Long
lColumn = ws.Cells(2, Columns.Count).End(xlToLeft).Column
For colx = 2 To col.last Step 2
Columns(colx).Value = "Column" +1
Next
End Sub

但它甚至没有关闭。有人知道怎么做吗?

1 个答案:

答案 0 :(得分:2)

试试这个

Sub insert_column_every_other()
    Dim lColumn As Long, iColumn As Long

    With Worksheets("MySheetName") ' change "MySheetName" to your actual worksheet name
        lColumn = .Cells(2, .Columns.Count).End(xlToLeft).Column
        For iColumn = 2 To lColumn Step 2
            .Cells(2, iColumn).Value = "Column" & iColumn/2
        Next iColumn
    End With
End Sub