试图制作一个宏,它将在电子表格中每 1000 行插入一行,并将一列的前 1000 行的串联插入到不同列中第 1000 行的单个单元格中。

我正在使用此代码每 1000 行插入一行:

Sub Insert1000()
    Dim rng As Range

    Set rng = Range("A2")
    While rng.Value <> ""
        rng.Offset(1000).EntireRow.Insert

        'code insert csv of 1000 previous rows into a single cell

        Set rng = rng.Offset(1001)
    Wend
End Sub

如果我的描述不清楚,请见谅。这是我希望我的结果的剪辑。

任何帮助,将不胜感激。

最佳答案

编辑:在标记线上添加了缺失的 .EntireRow

Sub InsertCSV()
    Const BLOCK_SIZE As Long = 1000
    Dim rng As Range, num

    Set rng = Range("A2").Resize(BLOCK_SIZE)
    num = Application.CountA(rng)

    Do While num > 0
        rng.Cells(BLOCK_SIZE + 1).EntireRow.Insert
        With rng.Cells(BLOCK_SIZE + 1).EntireRow '<<edited
        .Cells(1, "H").Value = Join(Application.Transpose(rng.Value), ",")
        .Cells(1, "I").Value = Join(Application.Transpose(rng.Offset(0, 1).Value), ",")
        End With
        Set rng = rng.Offset(BLOCK_SIZE + 1)
        num = Application.CountA(rng)
    Loop

End Sub

关于Excel VBA 宏 - 在循环中连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26308690/

10-13 02:07