Replacing/deleting values if doesn't satisfy condition

excel newb

New Member
Joined
Mar 29, 2009
Messages
15
Hi,

I have a column with "1" and "0". I'm looking for something like, if the value in a cell within the column is "0", then delete the 4 cells previous to the cell that contained "0" within the row. Instead of deleting, the values can also be replaced with anything e.g., zero or n/a etc. Whichever way it would work, would be great.

Thank you!

 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hello and welcome to MrExcel.

Does this work as expected? Press ALT + F11 to open the Visual Basic Editor, Insert > Module and paste in:

Code:
Sub Deal0()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 5 To LR
    If Range("A" & i).Value = 0 Then
        Range("A" & i - 4).Resize(4).Value = [#N/A]
    End If
Next i
End Sub

Press ALT + F11 to return to your sheet, Tools > Macro > Macros, highlight Deal0 and click the Run button.
 
Upvote 0
Thank you.

I'm using excel 2007, so I'm not sure how to apply the last part to 2007:
Press ALT + F11 to return to your sheet, Tools > Macro > Macros, highlight Deal0 and click the Run button.



Also, when you specified "A", how would I specify A and more columns. Do I put & or , or something else?

Thank you :)
 
Upvote 0
Click the Office button (top left), select Excel Options, tick Show Developer tab in the ribbon the click OK. Click Macros, highlight Deal 0 then click the Run button.

What is the range to which this macro should apply?
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,957
Latest member
Hat4Life

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