Avatar of upobDaPlaya
upobDaPlaya
 asked on

Why would a IF THEN result jump up to a prior line in the VBA code

Without providing an example.  I can get it later if needed.  If I have a For Next loop and within the For Next Loop I have an IF THEN statement.  For some reason when the IF Statement runs it jumps back to a prior line of code above the Loop to another IF THEN statement.  What would be a reason for this to occur. Note if I Debug-Compile no errors are found.   Realize this question may be difficult to evaluate without an example but I am not home yet...

I was hoping the question was obvious enough without an example to offer some suggestions to troubleshoot.
VBAMicrosoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Martin Liss

8/22/2022 - Mon
Martin Liss

Your Sub may be being called again (likely through a chain of other subs) by whatever is in the If/Then statement.
Martin Liss

For example if you run Subc in this code, it will behave as you describe when x becomes 5.

Sub Suba()
    Call Subb
End Sub

Sub Subb()
    Call Subc
End Sub

Sub Subc()
    Dim X As Integer
    
    MsgBox "I'm here"
    For X = 1 To 10
        If X = 5 Then Call Suba
    Next
    
End Sub

Open in new window

Note that I added the "Call"s for the purposes of this example even though they aren't necessary.

When you get in front of your computer again, put a breakpoint on the If/Then line (or one of the lines) by clicking in the left-hand margin of one of those lines and then repeatedly press F8 to step through the code. For more help with debugging please see this article of mine.
upobDaPlaya

ASKER
Here is the partial code

***** FIRST BLOCK OF CODE *********
Sheets("My Sales").Select
For intR = 2 to Range ("A2").End(xlDown).Row
If Range ("J" & intR).Value = "Bad" THEN
 With Sheets ("My Sales")
  If .Range("C" & intR) = '' Then
     Exit For
  Else
  Range("A" & intR).EntireRow.Delete
  On Error GoTo NextBadLine
  Int R = int R-1
  End If
End With
NextBadLine

End If    'Jump line -  When the code runs and it gets to the code below (See "Problem Line" below) it jumps back up to this line
Next intR
IntR = 0

**** SECOND BLOCK OF CODE *******
With ActiveWorkbook
Set ws = .Worksheets ("People Data")
For intR = 2 to Range ("A2").End(xlDown).Row
   Set ws1 = .Worksheets ("Sales Data")
    For intZ = 2 to Range ("a2").End(xlDown).Row
    If Range ("F" & intR) & Range ("I" & intR) & Abs(Range("K" & intR)) = .Range("a"& intZ) & .Range ("E" & intZ) & .Range("F" & intZ) Then  'PROBLEM LINE - When IT GETS HERE it jumps up to the line I call above the "Jump line"
    .Range("A" & intZ).Value = "REVIEW"
   End IF
   Next intZ
Next intR
End With
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Norie

What error(s) are you trying to handle with this?
On Error GoTo NextBadLine

Open in new window

upobDaPlaya

ASKER
If there is some type of error when I attempt to delete I wanted to continue
SOLUTION
Martin Liss

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
upobDaPlaya

ASKER
Ok...i will fix that error..but the reason for the issue was I did not explicitly name the ranges.  I needed to ws.range and ws1.range.  Any other suggestions please let m,e know..thx
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Martin Liss

If one or more of us have answered this question then please don't forget to close it. Thanks.
Martin Liss

I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2016