Excel 宏复制以前工作过的行,现在不工作但没有错误消息?
我使用下面的宏将信息从一行复制到下面的空白行(我已经生成)。这个宏在~一个月前工作,但当我现在运行它时,没有发生任何事情,尽管没有错误消息Excel 宏复制以前工作过的行,现在不工作但没有错误消息?,excel,vba,Excel,Vba,我使用下面的宏将信息从一行复制到下面的空白行(我已经生成)。这个宏在~一个月前工作,但当我现在运行它时,没有发生任何事情,尽管没有错误消息 Sub FillEveryOther() Dim lastRow As Long Dim i As Long lastRow = Cells(Rows.Count, 12).End(xlUp).Row For i = 2 To i = lastRow Step 2 Rows(i).EntireRow.Select Sel
Sub FillEveryOther()
Dim lastRow As Long
Dim i As Long
lastRow = Cells(Rows.Count, 12).End(xlUp).Row
For i = 2 To i = lastRow Step 2
Rows(i).EntireRow.Select
Selection.Copy
Rows(i + 1).EntireRow.Select
ActiveSheet.Paste
If i = lastRow + 1 Then Stop
Next i
End Sub
由于以下事实,您永远不会进入循环
For i = 2 to i = lastRow Step 2
在功能上与
For i = 2 to False Step 2
因为False
的计算结果为0,所以它实际上是
For i = 2 to 0 Step 2
所以它永远不会进入循环
把它改成
For i = 2 To lastRow Step 2
编辑:
为了清楚起见,无论语句的计算结果是True
还是False
,它都不会进入循环,因为False
的计算结果是0
,True
的计算结果是-1
每隔一行填充上一行
代码
进步?
下面的代码允许行不严格为偶数(2,4,6…),并且下面可以有多个空行。但是,cCol
的单元格中总是必须有数据,或者其中必须有一个公式(IsEmpty
)
还请注意,如果i=lastRow+1,则可以删除
,然后停止,因为这在该循环中永远不会发生。
Sub FillEveryOther()
Const cCol As Variant = 12 ' Last-Row-Column Letter/Number
Const cFirstRow As Long = 2 ' First Row Number
Dim lastRow As Long ' Last Row Number
Dim i As Long ' Cell (Row) Counter
' Calculate Last Row Number in Last-Row-Column.
lastRow = Cells(Rows.Count, cCol).End(xlUp).Row
' Loop through every other cell (row) of Last-Row-Column.
For i = cFirstRow To lastRow Step 2
' Copy current row to the row below.
Rows(i).Copy Rows(i + 1)
Next
End Sub
Sub CopyNotEmpty()
Const cCol As Variant = 12 ' Last-Row-Column Letter/Number
Const cFirstRow As Long = 2 ' First Row Number
Dim lastRow As Long ' Last Row Number
Dim i As Long ' Cell (Row) Counter
' Calculate Last Row Number in Last-Row-Column.
lastRow = Cells(Rows.Count, cCol).End(xlUp).Row
' Loop through cells (rows) of Last-Row-Column.
For i = cFirstRow To lastRow
' Check if there is data in current cell.
If Not IsEmpty(Cells(i, cCol)) Then
' Copy current row a row below.
Rows(i).Copy Rows(i + 1)
' Increase Cell (Row) Counter, because we don't want to copy
' the already copied row.
i = i + 1
End If
Next
End Sub