我的代码每X行记录填写一个空白行。我想要做的是有一些代码然后插入那些空行的单元格中的一些静态文本。这是我添加空行的代码...代码不是我的全部我从互联网上抓了它。我需要它做的是将静态文本填入它正在创建的空白行。然后继续填充并添加每50条记录。谢谢!
**********
Sub InsertRowEveryXrows()
Dim rw As Long
Dim lr As Long
Dim cnt As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
rw = 2
cnt = 1
Do
If cnt = 50 Then
Rows(rw).Insert Shift:=xlDown
cnt = 1
Else
cnt = cnt + 1
End If
rw = rw + 1
Loop While rw <> lr
End Sub
*****************
答案 0 :(得分:0)
你唯一要做的就是问cnt什么时候把它变成51并添加你的静态文本(假设列为1),如下所示:
Sub InsertRowEveryXrows()
Dim rw As Long
Dim lr As Long
Dim cnt As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
rw = 2
cnt = 1
Do
If cnt = 50 Then
Rows(rw).Insert Shift:=xlDown
cnt = 1
Else
if cnt = 51 then
cells(rw,1) = "your static text"
else
cnt = cnt + 1
End If
End If
rw = rw + 1
Loop While rw <> lr
End Sub
告诉我它是怎么回事,希望它有所帮助!
答案 1 :(得分:0)
我在代码中添加了注释,以显示发生了什么,但这对我有用,应该更快一些。
Public Sub Sample()
Dim WkSht As Worksheet
Dim LngLR As Long 'Last Row
Dim LngCR As Long 'Current Row
'Connect to the worksheet
Set WkSht = ThisWorkbook.Worksheets("Sheet1")
'Get the last row so we know when to stop
LngLR = WkSht.Range("A" & WkSht.Rows.Count).End(xlUp).Row
LngCR = 51 '51 to account for the first row being a header
'Keep adding the 50th row until when would end up past the last row
Do Until LngCR > LngLR
'Add the new row
WkSht.Rows(LngCR).Insert Shift:=xlDown
'Populate it
WkSht.Range("A" & LngCR) = "Your static text"
'Increase the last row as it will now be one more
LngLR = LngLR + 1
'Got to the next 50th
LngCR = LngCR + 50
Loop
Set WkSht = Nothing
End Sub