使用VBA代码填充空白单元格为零

时间:2016-02-03 18:20:48

标签: excel-vba vba excel

Sub FillEmptyCell()
    Dim rng As Range
    Dim i As Long
    Dim cell As Range
    Dim sht As Worksheet
    Set sht = ActiveWorkbook.Sheets("Sheet1")
    sht.Activate

    Set rng = Range(Range("C12"), Range("AD" & sht.UsedRange.Rows.Count))

    For Each cell In rng
        If cell.Value = "" Then
            cell.Value = "0"
        End If

    Next
End Sub

我试图用动态零填充表格中的空白区域。但是, 我不希望这个在行中填充没有数据的零。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

了解这是如何运作的,

Sub ZeroStuff()
    Dim LstRw As Long, rng As Range, sh As Worksheet, c As Range

    Set sh = Sheets("Sheet1")

    With sh
        LstRw = .Cells(.Rows.Count, "C").End(xlUp).Row + 1
        Set rng = .Range("C12:C" & LstRw).SpecialCells(xlCellTypeBlanks)

        For Each c In rng.Cells
            .Range(c.Offset(, 1), c.Offset(, 27)) = 0
        Next c

    End With

End Sub