简体   繁体   English

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

[英]VBA Fill empty cells in a range with a zero

In VBA, I want to fill empty cells in a range with a zero (0)在 VBA 中,我想用零 (0) 填充范围内的空单元格

Below if my function:下面如果我的功能:

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

With the following table:与下表:

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

I have two empty cells that I want to fill in with a zero in (1, 2) and (2, 1).我有两个空单元格,我想用 (1, 2) 和 (2, 1) 中的零填充。

I call the function like this: FillEmptyCellWith0(A1:B2)我这样调用函数: FillEmptyCellWith0(A1:B2)

When I reach "MARK1", at the second turn of the loop, it displays:当我到达“MARK1”时,在循环的第二圈,它显示:

Cell (1, 2) = 0
Empty cell

and then stops.然后停止。

When I trace the code I can see it stops at MARK2.当我跟踪代码时,我可以看到它停在 MARK2 处。

Do you see why?你明白为什么吗?

Can you tell me how to assign a zero value (0) to these empty cells?您能告诉我如何为这些空单元格分配零值 (0) 吗?

EDIT 1编辑 1

Apparently, I cannot do that.显然,我不能那样做。

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

Loops are not necessary.循环不是必需的。

Since you are not returning a value from the function, use a Sub instead and have the function tell you if any cells were changed:由于您没有从函数返回值,请改用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

The code above tells you if there were any empties to change to zeros.上面的代码会告诉您是否有任何要更改为零的空。

Modify with the appropriate range and try:使用适当的范围进行修改并尝试:

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

There are a number of ways to do this:有多种方法可以做到这一点:

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")
    With rng.SpecialCells(xlCellTypeBlanks)
        .Value = "0"
    End With
End Sub

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

You should use built in functionality instead您应该改用内置功能

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

Try尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM