我有一张工作表,其中行数是动态的。我正在尝试添加一个宏,该宏在每个活动行之后添加一个新行,并且应该在添加的每个新行的C列下添加文本“ No Show”,并且应该在D列下添加单元格值E5。下面的示例:
当前工作表:
在宏之后:(在E5中测试为假日)
我有一个宏来添加新的空行,但不确定如何集成其中的其他部分。
Sub Insert_Blank_Rows()
Selection.End(xlDown).Select
Do Until ActiveCell.Row = 1
ActiveCell.EntireRow.Insert shift:=xlDown
ActiveCell.Offset(-1, 0).Select
Loop
End Sub
答案 0 :(得分:1)
Sub FFF()
Dim r&, vE5
vE5 = [E5]: r = Cells(Rows.Count, 1).End(xlUp).Row + 1
While r > 1
Rows(r).Insert
Cells(r, 1).Resize(, 4) = Array(Cells(r - 1, 1).Resize(, 2), "No Show", vE5)
r = r - 1
Wend
End Sub
答案 1 :(得分:0)
如果我了解您的问题,您可以:
在此示例中,我假设您在E5单元格中有文字“假期”。
我尝试不更改您的代码
编辑了图像和代码
(因为在我使用E1单元并且没有将AB ...写入新列之前)
在执行宏之前
宏之后
Sub Insert_Blank_Rows()
Dim text, textCell_E5 As String
Dim myRow As Long
text = "no Show" ' this thext goes into column C
textCell_E5 = Cells(5, 5) ' Holiday
ActiveSheet.Range("A1").Select ' or cells(1,1).Activate
Selection.End(xlDown).Select
myRow = ActiveCell.Row + 1
Cells(myRow, 1).Offset(0, 2) = text
Cells(myRow, 1).Offset(0, 3) = textCell_E5
Cells(myRow, 1).Offset(0, 0) = Cells(myRow, 1).Offset(-1, 0)
Cells(myRow, 1).Offset(0, 1) = Cells(myRow, 1).Offset(-1, 1)
Do Until ActiveCell.Row = 1
ActiveCell.EntireRow.Insert shift:=xlDown
myRow = ActiveCell.Row ' get the current row
Cells(myRow, 1).Offset(0, 2) = text ' write into column C the no Show
Cells(myRow, 1).Offset(0, 3) = textCell_E5 ' add Holiday Text
Cells(myRow, 1).Offset(0, 0) = Cells(myRow, 1).Offset(-1, 0) 'write into column A (new row)
Cells(myRow, 1).Offset(0, 1) = Cells(myRow, 1).Offset(-1, 1) ' write into column B (new row)
ActiveCell.Offset(-1, 0).Select
Loop
End Sub
我尝试了代码并工作。
希望这会有所帮助
答案 2 :(得分:0)
向后循环:
Option Explicit
Sub Insert_Blank_Rows()
Dim iRow As Long
Dim myText As String
myText = Range("E5").Text
With Selection
For iRow = .Rows.Count To 1 Step -1
.Rows(iRow + 1).EntireRow.Insert shift:=xlDown
With .Rows(iRow + 1)
.Range("A1:B1").Value = .Offset(-1).Range("A1:B1").Value
.Range("C1:D1").Value = Array("No Show", myText)
End With
Next
End With
End Sub