Excel VBA For loop breaks on execution of commands

mlafrance1

New Member
Joined
Jul 30, 2017
Messages
5
Hi,
I have a Excel 2010 spreadsheet that is frustrating me. The command that I enter inside a For loop executes but instead of going to the next For item (of which there are many) it exits the loop and starts the sub at the beginning again.
Here is the offending loop -
Code:
    Set rng = Range("J17:AA17")
    
    'Evaluate the holes for skins
    For Each myCell In rng
    If myCell.Value = "" Then
        MsgBox myCell.Address & " is not a skin cell."
        'populate the Carry Forward
        Range(myCell.Address).Offset(-16, 0).Value _                          <--
            = Range(myCell.Address).Offset(-16, 0).Value + Range("G18").Value <-- the offending code
    ElseIf myCell.Value = "Skin" Then
        MsgBox myCell.Address & " is a skin cell."
    End If
    Next myCell
    MsgBox "Finished the loop."
Any advice is very much appreciated.
Mike
 
Last edited by a moderator:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
The script works for me and does not just keep stating the loop again.
If the loop never stopped it would run for ever.

You have told it to pop up a message Box every time it enters a new range.
Which means you will see a popup message 18 times.

It's always easier when a script does not do what you want it to do to tell us what you want the script to do and not just say the script is not doing what I wanted.
 
Upvote 0
I suspect this is a worksheet_change event? If so you need to disable events when changing cells in that code. Add

Code:
application.enableevents = false

before the for each line, then

Code:
application.enableevents = true

after the “Next” line.
 
Upvote 0
Thank you for your assistance, My Aswer Is This. iAny good advice is always welcomed.
Mike



The script works for me and does not just keep stating the loop again.
If the loop never stopped it would run for ever.

You have told it to pop up a message Box every time it enters a new range.
Which means you will see a popup message 18 times.

It's always easier when a script does not do what you want it to do to tell us what you want the script to do and not just say the script is not doing what I wanted.
 
Upvote 0
Thanks, Rory.
You were right, it was a worksheet change event. Your response resolved my immediate problem. Although I have other problems in my code your response was greatly appreciated.
Mike
 
Upvote 0

Forum statistics

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