Help modifying a code for filling blanks in selected range with zeroes

edge37

Board Regular
Joined
Sep 1, 2016
Messages
87
Office Version
  1. 2021
Platform
  1. Windows
Hi, I am using this code to fill a selected range of cells with zeros if they are blank, the thing is that this code fills the blank cells one by one, and could take some time to finish. Can you help me please modify this code so it fills blank cells from a selected range all at once automatically?

VBA Code:
Sub InsertZeroes()
    For Each c In Selection.Cells
    If c.Value = "" Then c.Value = 0
    Next
End Sub

Thank you
 

Attachments

  • 20240706_093629-ezgif.com-video-to-gif-converter.gif
    20240706_093629-ezgif.com-video-to-gif-converter.gif
    127.6 KB · Views: 4

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this:
VBA Code:
Sub InsertZeroes()
    Dim data As Variant
    Dim i As Long, j As Long
    Dim t As Double: t = Timer
    
    data = Selection.Value
    For i = LBound(data, 1) To UBound(data, 1)
        For j = LBound(data, 2) To UBound(data, 2)
            If IsEmpty(data(i, j)) Then data(i, j) = 0
        Next j
    Next i
    
    Selection.Value = data
    MsgBox Round(Timer - t, 2) & " seconds" 
End Sub
 
Upvote 1
Solution
Maybe
VBA Code:
Sub trythis()
    Selection.SpecialCells(xlBlanks).Value = 0
End Sub
 
Upvote 1
Maybe
VBA Code:
Sub trythis()
    Selection.SpecialCells(xlBlanks).Value = 0
End Sub
I have tried this approach before suggesting the loop however it doesn't seem to work on a completely blank range. There must be at least 1 non-blank value in the range.
 
Upvote 0
Thank you both, wonderful
You're welcome, but try this. I've added an error handler to @edge37 suggestion above it seems to work a lot faster.
VBA Code:
Sub InsertZeroesv2()
    On Error GoTo errHandler
    Selection.SpecialCells(xlCellTypeBlanks).Value = 0
    Exit Sub
errHandler:
    Selection.Value = 0
End Sub
 
Upvote 0
@Cubist, you sure you don't have the posters mixed up ?

I have tried this approach before suggesting the loop however it doesn't seem to work on a completely blank range. There must be at least 1 non-blank value in the range.
My testing was with totally blank ranges and there was no issues.
What loop ?
 
Upvote 0
When I run I get this. I was referring to the For loop in post#2. And yes I got the names swapped.
Screen Shot 2024-07-06 at 11.42.25 AM.png
 
Upvote 0
@Cubist, you're definately right for what you show.

I suspect those selected cells are not within the sheets usedrange which I believe is what SpecialCells is restricted to.
Expermintation shows me if part of the selection was within the sheets usedrange only they would have been zero'd
and (this one could be dangerous) if only a single cell within the usedrange is selected all blanks within the usedrange are zero'd
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top