Discussion Locked

How to enter a test for cell A1 in a do...while loop

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
In my Excel Macro I loop through a spreadsheet and delete cells containing specific text. I would like to end the macro before the loop reaches cell A1 (I begin the macro by making cell A2 active). I don't want the text in A1 deleted. How do I tell the macro to exit the do...while loop when it encounters cell A1?

4 replies

by techsupportspecialist

There are several ways you can get a loop like this to exit. I would recommend the following:

''''''''''''''''''''''''''''''''''
Do Until (use the appropriate syntax for ACTIVE CELL = A2 here)

(Enter all the code here that you want to have executed during the loop)

Loop
''''''''''''''''''''''''''''''''''

This method will check the active cell at the start of the loop, and the loop will terminate if the condition is true.

I hope this helps. Good luck!

by abarnes70

Something a little more reliable would be to use an IF command to control the deletion:

if activecell.address="$A$1" then
else
activecell.clearcontents
end if

The deletion command will depend on what you are deleting, ie cells, their contents, their formats etc.

HTH

This discussion is closed to new replies.