为什么此Excel VBA脚本向下复制并重复到最后选择的单元格?

时间:2012-09-20 17:49:20

标签: excel excel-vba vba

为什么这会将数据复制到下一个空白单元格直到下一个完整单元格,然后获取该单元格的数据并重新开始?

Option Explicit

Sub CopyDown()

    Dim wsPOD As Worksheet
    Dim lastrow

    Set wsPOD = Sheets("PO Data")

With wsPOD
    lastrow = wsPOD.Range("F" & Rows.Count).End(xlUp).Row
            wsPOD.Range("A2:D" & lastrow).Select
            Selection.SpecialCells(xlCellTypeBlanks).Select
            Selection.FormulaR1C1 = "=R[-1]C"
    End With
End Sub

我理解大部分脚本,但我不明白这一部分:

        Selection.SpecialCells(xlCellTypeBlanks).Select
        Selection.FormulaR1C1 = "=R[-1]C"

最后一部分对我来说尤其是胡言乱语,有人可以帮我理解吗?

1 个答案:

答案 0 :(得分:2)

根据我的评论,您可以使用

整理代码

选项明确

Sub CopyDown()

    Dim wsPOD As Worksheet
    Dim lastrow as long
    Dim EmptyCells as range

    Set wsPOD = Sheets("PO Data")

With wsPOD
    lastrow = .Range("F" & .Rows.Count).End(xlUp).Row
            set EmptyCells = .Range("A2:D" & lastrow).SpecialCells(xlCellTypeBlanks)
            EmptyCells.FormulaR1C1 = "=R[-1]C"
    End With
End Sub