Seeking method to replace zeros with whatever number is populated in cell above

auto.pilot

Well-known Member
Joined
Sep 27, 2007
Messages
734
Office Version
  1. 365
Platform
  1. Windows
I searched and also tried to solve this problem but I'm stumped.

I have several columns populated with positive numbers, but some are zero. I'd like a VBA method to change the zero's to whatever number is in the cell directly above. I suppose this needs to be some sort of a loop, since a column may have several numbers, then more than one cell with zero, then more positive numbers. There are about 50 columns, and 30 rows of data. This would be quite simple with a formula, but I'd like to do it with VBA. How can I do this?

One column looks something like this. As an example, the first and second instance of zero should be changed to 16.4 & 16.4, then the zeros at the bottom should be changed to 12.2 & 12.2

16.4
0
0
12.2
0
0
17.85

Thanks in advance.

Jim
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
It would help if you told us the columns involved or a way to identify them.
 
Upvote 0
I searched and also tried to solve this problem but I'm stumped.

I have several columns populated with positive numbers, but some are zero. I'd like a VBA method to change the zero's to whatever number is in the cell directly above. I suppose this needs to be some sort of a loop, since a column may have several numbers, then more than one cell with zero, then more positive numbers. There are about 50 columns, and 30 rows of data. This would be quite simple with a formula, but I'd like to do it with VBA. How can I do this?

One column looks something like this. As an example, the first and second instance of zero should be changed to 16.4 & 16.4, then the zeros at the bottom should be changed to 12.2 & 12.2

16.4
0
0
12.2
0
0
17.85

Thanks in advance.

Jim
I found this, which almost works... Zeros are correctly replaced with the value of the cell above, but it fails when the column has more than two zeros next to each other.
Is it possible to adjust this code, or do I need a different solution?

VBA Code:
Sub ReplaceZeros()
Application.Calculation = xlCalculationManual
Dim cell As Range
Set cell = Cells(Rows.Count, "A").End(xlUp)

While cell.Row > 1

If cell = 0 Then
    cell = cell(0)
End If

Set cell = cell(0)

Wend

Application.Calculation = xlCalculationAutomatic

End Sub
 
Upvote 0
See if this macro does what you want...
VBA Code:
Sub ReplaceZerosWithValueAbove()
  Dim LastRow As Long, Ar As Range
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  For Each Ar In Range("AC7:AS" & LastRow & "," & "AV8:BG" & LastRow).Areas
    Ar.Replace 0, "#N/A", xlWhole, , , , False, False
    Ar.SpecialCells(xlConstants, xlErrors).FormulaR1C1 = "=R[-1]C"
    Ar.Value = Ar.Value
  Next
End Sub
 
Upvote 0
Solution
Excellent solution. Thanks for sharing your time and knowledge!
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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