1

I have a code where I'm referencing variable "k" in a named range, and then performing a series of nested "if" loops. However, I can only put "next k" in one spot in the sub. However, I also need the code to loop back to the next k if certain conditions are met. In adding the 2nd "Next k" statement, I get the "Next Without For" error.

Here's the relevant code snippets:

Set SFR = SF.Range("a2", SF.Range("a2").SpecialCells(xlCellTypeLastCell))
Set Sheetparent = Control.Range("b2", Control.Range("b2").End(xlDown))
Set SheetControl = Control.Range("a2", Control.Range("a2").End(xlDown))
Set TypeControl = Control.Range("c2", Control.Range("c2").End(xlDown))
Set BDMControl = Control.Range("E2", Control.Range("e2").End(xlDown))
Set ParentControl = Control.Range("f2", Control.Range("f2").End(xlDown))

        If r.Value = POp.Name Then

        For Each k In SFR

            If SFR(k.Row, 6).Value = r.Offset(0, 1).Value Then

                For Each b In ParentControl

                    If SFR(k.Row, 1).Value = ParentControl(b.Row, 1).Offset(0, -1).Value Then

                    With POp.Range("a2")
                    .Offset(i, 0).Value = SFR(k.Row, 3).Value
                    .Offset(i, 1).Value = SFR(k.Row, 4).Value
                    .Offset(i, 2).Value = SFR(k.Row, 1).Value
                    .Offset(i, 3).Value = SFR(k.Row, 4).Value
                    .Offset(i, 4).Value = SFR(k.Row, 6).Value
                    .Offset(i, 5).Value = SFR(k.Row, 8).Value
                    .Offset(i, 6).Value = SFR(k.Row, 9).Value
                    .Offset(i, 7).Value = SFR(k.Row, 10).Value
                    .Offset(i, 8).Value = SFR(k.Row, 2).Value
                    .Offset(i, 9).Value = SFR(k.Row, 11).Value
                    .Offset(i, 10).Value = SFR(k.Row, 13).Value
                    .Offset(i, 11).Value = SFR(k.Row, 15).Value

                    End With

                    i = i + 1

                    Else

                End If

                Next b

        Else
        End If
        Next k



    Else

    End If

So, the issue is that I get the same values SFR(k.row, [column]) repeated for several rows, before the formula moves on to the next SFR k. I want to be able to tell the macro to go to the next k if

If SFR(k.Row, 1).Value = ParentControl(b.Row, 1).Offset(0, -1).Value

Is true, and the values are copied over. Any suggestions? Thanks in advance.

Community
  • 1
  • 1
LeoPTY
  • 23
  • 1
  • 4
  • Possible duplicate of [VBA - how to conditionally skip a for loop iteration](http://stackoverflow.com/questions/8680640/vba-how-to-conditionally-skip-a-for-loop-iteration) – Automate This Oct 01 '15 at 02:01
  • Looks to me like you just want `Exit For` before the `Else` to exit the `b` loop. `Goto` is unnecessary here as far as I can see. – Rory Oct 01 '15 at 07:17

1 Answers1

0

You have to use a goto statement. Put a label before next k, let's call it nextK, and use goto:

   For k In SFR
      .....
      if someCondition then goto nextK
      ....
nextK:
   next k
A.S.H
  • 29,101
  • 5
  • 23
  • 50