如何自动将文本添加到插入的行

时间:2018-03-27 16:37:47

标签: excel excel-vba vba

下面是一些代码,我必须自动在工作表上的每个预先存在的行项下面插入6行。有没有办法用预呈现的文本填充6行中的每一行?文本可以分别按行,' item1',' item2',' item3' ...

Dim lastRow As Long
lastRow = Range("B" & Rows.Count).End(xlUp).Row

Dim rowCnt As Long
For rowCnt = lastRow To 2 Step -1

Range("B" & rowCnt).Resize(6, 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove    

Next

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

尝试将文本添加为​​转置数组。

dim itms as variant

itms = array("item1", "item2", "item3", "item4", "item5", "item6")

...

For rowCnt = lastRow + 1 To 2 Step -1

    Range("B" & rowCnt).Resize(6, 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove    
    Range("B" & rowCnt).Resize(6, 1) = application.transpose(itms)

Next

答案 1 :(得分:1)

这是一种方式。

Sub x()

Dim lastRow As Long

lastRow = Range("B" & Rows.Count).End(xlUp).Row

Dim rowCnt As Long, i As Long

For rowCnt = lastRow To 2 Step -1
    Range("B" & rowCnt).Resize(6, 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("B" & rowCnt + 7).Resize(6).Value = Application.Transpose(Array("Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"))
Next

End Sub