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?

AD
0XX
0XX
0XX
0XX
AC
0XX
0XX
DE
0XX

<tbody>
</tbody>

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

ADXX
ADXX
ADXX
ADXX
ACXX
ACXX
DEXX

<tbody>
</tbody>

I would be so grateful for your help.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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,215,140
Messages
6,123,266
Members
449,093
Latest member
Vincent Khandagale

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