VBA用零填充范围内的空白单元格

时间:2018-12-19 13:23:01

标签: excel-vba

在VBA中,我要填充零(0)范围内的空白单元格

下面是我的功能:

Function FillEmptyCellWith0(r As Range)
Dim n_rangeWidth As Integer
Dim n_rangeHeight As Integer
Dim n_i As Integer
Dim n_j As Integer
Dim n_value As Integer

n_rangeWidth = r.Columns.Count
n_rangeHeight = r.Rows.Count

MsgBox "Range width = " & n_rangeWidth
MsgBox "Range height = " & n_rangeHeight

For n_j = 1 To n_rangeWidth
  For n_i = 1 To n_rangeHeight
    n_value = r.Cells(n_j, n_i).value
    MsgBox "Cell (" & n_j & ", " & n_i & ") = " & n_value <----- MARK1

    If IsEmpty(r.Cells(n_j, n_i)) Then
        MsgBox "Empty cell"
        r.Cells(n_j, n_i).value = "0" <----- MARK2
    End If
  Next n_i
Next n_j

具有下表:

------+-------
|   1 |      |
------+-------
|     |     3|
------+-------

我有两个空单元格,我想在(1,2)和(2,1)中用零填充。

我这样调用函数:FillEmptyCellWith0(A1:B2)

当我到达“ MARK1”时,在循环的第二圈,它显示:

Cell (1, 2) = 0
Empty cell

然后停止。

当我跟踪代码时,我可以看到它在MARK2处停止。

你知道为什么吗?

您能告诉我如何为这些空单元格分配零值(0)吗?

编辑1

显然,我不能那样做。

https://support.microsoft.com/en-us/help/170787/description-of-limitations-of-custom-functions-in-excel

5 个答案:

答案 0 :(得分:2)

不需要循环。

由于您没有从函数返回值,请改用 Sub 并让函数告诉您是否更改了任何单元格:

Sub MAIN()
    Dim r As Range
    Set r = Selection
    x = FillEmptyCellWith0(r)
    MsgBox x
End Sub

Public Function FillEmptyCellWith0(r As Range) As String
    Dim re As Range
    On Error Resume Next
        Set re = r.Cells.SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0
    If re Is Nothing Then
        FillEmptyCellWith0 = "Nothng changed"
        Exit Function
    End If
    re.Value = 0
    FillEmptyCellWith0 = "done"
End Function

上面的代码告诉您是否有空容器可以更改为零。

答案 1 :(得分:1)

修改适当的范围,然后尝试:

Option Explicit

Sub test()

    Dim rng As Range, Cell As Range

    'Set your range
    Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:B5")

    For Each Cell In rng

        If IsEmpty(Cell) Then
            Cell.Value = "0"
        End If

    Next

End Sub

答案 2 :(得分:0)

您应该改用内置功能

Sub FillZero()
    Dim rg As Range

    Set rg = Range("A1:B4")

    rg.Replace What:="", Replacement:="0", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False


End Sub

答案 3 :(得分:0)

有很多方法可以做到这一点:

Sub Fill0s1()
    Dim rng As Range: Set rng = Range("G5:I7")
    Dim Cell As Range

    For Each Cell In rng
        Cell.value = IIf(IsEmpty(Cell), "0", Cell.value)
    Next
End Sub

Sub Fill0s2()
    Dim rng As Range: Set rng = Range("G5:I7")
    Dim Cell As Range

    For Each Cell In rng.SpecialCells(xlCellTypeBlanks)
        Cell.value = "0"
    Next
End Sub

Sub Fill0s3()
    Dim rng As Range: Set rng = Range("G5:I7")
    rng.Replace What:="", Replacement:="0", LookAt:=xlWhole
End Sub

答案 4 :(得分:0)

尝试

Sub test()
    FillEmptyCellWith0 Range("a1: c10")
End Sub

Sub FillEmptyCellWith0(rngDb As Range)
    Dim vDB As Variant
    Dim r As Long, c As Integer
    Dim i As Long, j As Integer

    vDB = rngDb
    r = UBound(vDB, 1)
    c = UBound(vDB, 2)

    For i = 1 To r
        For j = 1 To c
            If IsEmpty(vDB(i, j)) Then
                vDB(i, j) = 0
            End If
        Next j
    Next i
    rngDb = vDB
End Sub