excel vba : How to replace cell that contains specific text

huiyin9218

Board Regular
Joined
Aug 7, 2018
Messages
53
Hi,

How do I write a code to replace cells that contains "0" in column A with the word at the top row of each 0 group?

[TABLE="class: grid, width: 300"]
<tbody>[TR]
[TD]AD[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]XX[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]XX[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]XX[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]XX[/TD]
[/TR]
[TR]
[TD]AC[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]XX[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]XX[/TD]
[/TR]
[TR]
[TD]DE[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]XX[/TD]
[/TR]
</tbody>[/TABLE]

Above is sample table and below is the output that i wish to obtain.

[TABLE="class: grid, width: 300"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]AD[/TD]
[TD]XX[/TD]
[/TR]
[TR]
[TD]AD[/TD]
[TD]XX[/TD]
[/TR]
[TR]
[TD]AD[/TD]
[TD]XX[/TD]
[/TR]
[TR]
[TD]AD[/TD]
[TD]XX[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]AC[/TD]
[TD]XX[/TD]
[/TR]
[TR]
[TD]AC[/TD]
[TD]XX[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]DE[/TD]
[TD]XX[/TD]
[/TR]
</tbody>[/TABLE]

I would be so grateful for your help.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Assuming those zero values are numerical values and not text and they are the only numerical values in the column & that they are not the result of formulas then try this with a copy of your workbook.
Code:
Sub Replace_Zero()
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .SpecialCells(xlConstants, xlNumbers).FormulaR1C1 = "=R[-1]C"
    .Value = .Value
  End With
End Sub

If any of my assumptions are incorrect, then please clarify.
 
Upvote 0
Assuming those zero values are numerical values and not text and they are the only numerical values in the column & that they are not the result of formulas then try this with a copy of your workbook.
Code:
Sub Replace_Zero()
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .SpecialCells(xlConstants, xlNumbers).FormulaR1C1 = "=R[-1]C"
    .Value = .Value
  End With
End Sub

If any of my assumptions are incorrect, then please clarify.

It works! Thank you!
 
Upvote 0
Code:
Sub Replacezero()


j = 3

lastRowA = Range("A" & Rows.Count).End(xlUp).Row

For Each Cell In Range("A2:A" & lastRowA)
If Cells(j, 1) = 0# Then
Cells(j, 1) = Cells(j - 1, 1)

End If
Next Cell


End Sub

This the code I tried to write just now, it worked for a cell. Can you teach me how to make it to continue until the end of the cell that contains data? Is it something like j+1
Sorry as I'm still a beginner in vba, hope you can teach me.
 
Upvote 0
If you wanted to do it one cell at a time instead of in bulk ..
Highly recommend to always declare all your variables. You can force yourself to do that: In the vba window Tools -> Options... -> Editor tab -> Require Variable Declaration -> OK
Code:
Sub Replacezero()
  Dim lastRowA As Long
  Dim Cell As Range
  
  lastRowA = Range("A" & Rows.Count).End(xlUp).Row

  For Each Cell In Range("A2:A" & lastRowA)
    If Cell.Value = 0 Then
      Cell.Value = Cell.Offset(-1).Value
    End If
  Next Cell
End Sub
 
Upvote 0
If you wanted to do it one cell at a time instead of in bulk ..
Highly recommend to always declare all your variables. You can force yourself to do that: In the vba window Tools -> Options... -> Editor tab -> Require Variable Declaration -> OK
Code:
Sub Replacezero()
  Dim lastRowA As Long
  Dim Cell As Range
  
  lastRowA = Range("A" & Rows.Count).End(xlUp).Row

  For Each Cell In Range("A2:A" & lastRowA)
    If Cell.Value = 0 Then
      Cell.Value = Cell.Offset(-1).Value
    End If
  Next Cell
End Sub

I see. Thank you so much!!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
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