How to end For loop If condition is met.

Cummins

Board Regular
Joined
Jul 26, 2011
Messages
58
Seems like there should be a way to goto Next if the empty cell condition is met but I can't find it.


For x = 1 To lastrow
Data = Str(F1.Cells(x, 1))
ActiveSheet.Cells(x, 1).Select
If IsEmpty(ActiveCell.Value) Then ????
If Data <> "0" Then Ary1(x, 1) = Data: g = g + 1
Next
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Then a label is what you really want?

Code:
For x = 1 To lastrow
Data = Str(F1.Cells(x, 1))
ActiveSheet.Cells(x, 1).Select
If IsEmpty(ActiveCell.Value) Then Goto SkipMe
If Data <> "0" Then Ary1(x, 1) = Data: g = g + 1

SkipMe:

Next
 
Upvote 0
But that exits the loop. I only want to skip to the next test.

Use Block If's instead of single liners...

Instead of
If expression Then Do Something

Try
Rich (BB code):
If expression Then
    Do Something
    'What to do if expression is TRUE
Else
    Do something else, or nothing at all
    'What to do if expression is FALSE
End if

Hope that helps...


Please avoid spaghetti code caused by using goto statements.
 
Last edited:
Upvote 0
Just only do your code if the condition is met and then let the code flow automatically to the Next line. There is also no need to Select.

Code:
For X = 1 To lastrow
Data = Str(F1.Cells(X, 1))
If Cells(X, 1).Value <> "" Then
    If Data <> "0" Then
        Ary1(X, 1) = Data
        g = g + 1
    End If
End If
Next
 
Upvote 0
Thanks to all for the assistance. Just trying to learn the flexibility of the code outside of the "Text".
Much appreciated.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,753
Members
452,940
Latest member
rootytrip

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