Basic Fixed Loop Macro

Will85

Active Member
Joined
Apr 26, 2012
Messages
254
Office Version
  1. 365
Platform
  1. Windows
I am trying to learn loops.

My vba code is:

Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Selection.End(xlDown).Select
Selection.End(xlDown).Select

I would like to repeat this 5x

Or

Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Selection.End(xlDown).Select
Activecelloffset(0, -1) <<<<if this="" equals="" "x"="" then="" stop="" the="" loop,="" if="" not="" then
IF THIS CELL = "X" THEN STOP THE LOOP, IF NOT THEN . . .
Activecelloffset(0, 1) <<<< TO GET BACK TO WHERE WE LEFT OFF BEFORE TESTING
Selection.End(xlDown).Select

Then continue the loop until we find an "X"</if>
 
Last edited:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
To be clearer, I really need:

Selection.End(xlDown).Select
Range(Selection, Selection.End(xlToRight)).Select

Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Selection.End(xlDown).Select
Activecelloffset(0, -1) <<<<if this="" equals="" "x"="" then="" stop="" the="" loop,="" if="" not="" then
IF THIS CELL = "X" THEN STOP THE LOOP, IF NOT THEN . . .
Activecelloffset(0, 1) <<<< TO GET BACK TO WHERE WE LEFT OFF BEFORE TESTING


Then continue the loop until we find an "X"</if>
 
Last edited:
Upvote 0
Im now also realizing that my xlToRight repeated four times doesnt work, it only works once. Need to find a workaround for that.
 
Upvote 0
Have you looked at .CurrentRegion or .UsedRange instead of your current approach ??

Code:
Activesheet.currentregion.clear
OR
Code:
Activesheet.usedrange.clear

You don't need to use Select to clear data !!
 
Last edited:
Upvote 0
Not sure I follow. My variable range is five columns accross, I need to select all five columns, except they are seperated by a single blank column between each.
 
Upvote 0
Current region will clear the area within the "blank rows / coloumns
Usedrange will clear all within the usedrange regardless of blank rows / columns
 
Upvote 0
I'm still not quite clear what you are trying to achieve, but this might help

It assumes the entire row can be cleared...
It also assumes that the "x" will be in column "A".
It starts at row 2 and works down the page

Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lr
    If Cells(r, 1) <> "x" Then
        Rows(r).Clear
        Else
        Exit Sub
    End If
Next r
end sub

Rather than posting code that doesn't work and is meaningless to others.....it might be easier to simply explain in words what you are trying to achieve !
 
Last edited:
Upvote 0

Forum statistics

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