How to substitute blanks with zero, only if the blanks are in between other data

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
578
Office Version
  1. 365
Platform
  1. Windows
Hello,

In my score sheets, there are often blank spaces left in between other cells that need to be zero. I know of the find/replace function that can substitute blanks with zeros, but that will substitute any blank cell, even those that are after (to the right of) the last filled cell in each row (and the lenghts of the rows vary). For instance, in the table below, the cells I marked with "z" should be substituted with zero, but the cells at the end that I marked with "x" shouldn't.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]92[/TD]
[TD]88[/TD]
[TD]95[/TD]
[TD]z[/TD]
[TD]83[/TD]
[TD]89[/TD]
[TD]91[/TD]
[TD]x[/TD]
[TD]x[/TD]
[TD]x[/TD]
[/TR]
[TR]
[TD]86[/TD]
[TD]89[/TD]
[TD]85[/TD]
[TD]91[/TD]
[TD]93[/TD]
[TD]98[/TD]
[TD]89[/TD]
[TD]90[/TD]
[TD]87[/TD]
[TD]96[/TD]
[/TR]
[TR]
[TD]93[/TD]
[TD]91[/TD]
[TD]z[/TD]
[TD]z[/TD]
[TD]88[/TD]
[TD]85[/TD]
[TD]90[/TD]
[TD]87[/TD]
[TD]90[/TD]
[TD]x[/TD]
[/TR]
</tbody>[/TABLE]


Any suggestions would be appreciated.

Thanks!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Here is a VBA solution for you
Code:
Option Explicit


Sub findBlank()
    Dim lr As Long, lc As Long, i As Long, j As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To lr
        lc = Cells(i, Columns.Count).End(xlToLeft).Column
        For j = 1 To lc
            If Cells(i, j) = "" Then Cells(i, j) = 0
        Next j
    Next i


End Sub

How to install your new code
Copy the Excel VBA code
Select the workbook in which you want to store the Excel VBA code
Press Alt+F11 to open the Visual Basic Editor
Choose Insert > Module
Edit > Paste the macro into the module that appeared
Close the VBEditor
Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)


To run the Excel VBA code:
Press Alt-F8 to open the macro list
Select a macro in the list
Click the Run button
 
Last edited:
Upvote 0
Wow, thanks a lot! That worked beautifully! You're number one :)
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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