使用前一个单元格的值填充空白单元格

时间:2017-11-10 20:45:30

标签: excel-vba vba excel

我的代码没有按照我的想法运行:

`Sub FillRow()
    Dim Name As String
    For Each c In Range("G1:G" & Cells(Rows.Count, 2).End(xlUp).Row)
        If Cells(c.Row, 1) > "" Then
       MsgBox ActiveCell.Address
            Name = Cells(c.Row, 1)
        Else
            Cells(c.Row, 1).Value = Name

        End If
    Next
End Sub`

尝试在G列中获取一个空白单元格以填充上面的值,如果它有值,则单独留下。什么都没发生 - 调试似乎表明行没有增加。我虽然之前有效

1 个答案:

答案 0 :(得分:0)

你的列错了。试试这个:

Sub FillRow()
    Dim name As String
    Dim c As Excel.Range

    For Each c In Range("G1:G" & Cells(Rows.Count, "G").End(xlUp).Row).Cells
        If Len(CStr(Cells(c.Row, "G").Value2)) > 0 Then
            name = Cells(c.Row, "G")
        Else
            Cells(c.Row, "G").Value = name
        End If
    Next
End Sub