Macro to clear contents based on cell value

Schroetke

New Member
Joined
Dec 15, 2010
Messages
17
I've searched the boards, and know the answer is right in front of me, but am stumped. Need VBA code for the following:

Column J contains a list of 1's or 0's based on other cells data. The range begins in row 6 and continues to the end of the sheet (which can be variable in length - but never more than 200 rows).

Beginning in row 6, I would like to clear the contents of O6 through Y6 if cell J6 =1. Then, clear the contents of O7 through Y7 if cell J7 =1, and so on to the end of the sheet.

Any help is much appreciated.
 
Because both of your lines are within the IF THEN block, they are both subject to the check to see if column A is equal to 1.
So either, both lines will run (if the condition is met), or neither one will be run (if the condition is not met).

Thank you.

Yes, I wanted both lines to be run (only clear rows when A=1). However, with the code, only the first line run, the second line does not run, i.e. no matter A is equal to 1 or not, all rows for col G to I are cleared....
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
If you have the code written exactly as shown, columns B-D and G-I will be cleared. If they are not, then something else is going on, like:
- some of the cells are protected and cannot be edited (confirm all these columns can be edited)
- the rows you are checking are not between row 6 and myLastRow (confirm the value of myLastRow)
- there is another macro running to re-populate the cells or deleting rows afterwards (so rows move up)

I would recommend stepping through the code line-by-line by using F8 where you can watch what each line is doing.
 
Upvote 0
If you have the code written exactly as shown, columns B-D and G-I will be cleared. If they are not, then something else is going on, like:
- some of the cells are protected and cannot be edited (confirm all these columns can be edited)
- the rows you are checking are not between row 6 and myLastRow (confirm the value of myLastRow)
- there is another macro running to re-populate the cells or deleting rows afterwards (so rows move up)

I would recommend stepping through the code line-by-line by using F8 where you can watch what each line is doing.

Hi Joe4,

Thanks a lot for the valuable and helpful hints. I re-wrote the code to

'Then Range("L" & i & ":Q" & i & ",C" & i & ":F" & i).ClearContents

It works now! Thank you.
 
Upvote 0
Thanks a lot for the valuable and helpful hints. I re-wrote the code to

'Then Range("L" & i & ":Q" & i & ",C" & i & ":F" & i).ClearContents
You can write that more compactly and, in my opinion, in a more readable format this way...

Then Intersect(Rows(i), Range("L:Q,C:F")).ClearContents
 
Upvote 0
You can write that more compactly and, in my opinion, in a more readable format this way...

Then Intersect(Rows(i), Range("L:Q,C:F")).ClearContents
That's indeed much nicer! Thank you :)
Actually, it just occurred to me that this can be written even more compactly using Evaluate's square bracket notation like this...

Then Intersect(Rows(i), [L:Q,C:F]).ClearContents
 
Upvote 0

Forum statistics

Threads
1,217,388
Messages
6,136,298
Members
450,002
Latest member
bybynhoc

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