Embedding Next for iteration within an If Then statement: compiler issues.

Hello,

I am trying to do this thing so I may increase efficiency in my code.
 BG info, there are 4 data sheets, the 5th sheet is called THRESHOLD, and the 6th-9th sheets are where i duplicate row E data. 


The bold is where my code is erroring out and giving me  "Compile Error:  NExt without For".
 Why does it not allow for this? is there a different context/syntax in which i can make this happen?


Sub MyRoutine()
    Dim irow As Long
    Dim ii As Long

For ii = 1 to 4
Sheets(ii).Select

'Checks for # of data rows, if empty, go to next i.
 if empty and at final ii value, exit routine
            irow = 1
            While (Not (IsEmpty(ActiveSheet.Cells(irow, 1))))
                irow = irow + 1
            Wend
'since data will contain 2 rows of non-data, subtract 2 from total
            irow = irow - 2


 'checks if final data sheet has data, if not, end macro
        If irow < 0 and ii = 4 Then
   Worksheets("THRESHOLD").Select
            Range("B2").Select
            Exit Sub
Elseif irow < 0 then
Next ii
Else

Worksheets(ActiveSheet.Index + 4).Select

Range("C2").Select
'start at E2 since row 1 is non-data row
        ActiveCell.Formula = "='" & Sheets(ActiveSheet.Index - 5).Name & "'!E2"
 Selection.AutoFill Destination:=Range("C2:C" & row) 
 Range("C2:C" & row).Select
Next ii

        End If
Worksheets("THRESH").Select
    Range("A2").Select

End sub

Keys to the Problem Embedding Next for iteration within an If Then statement: compiler issues.

Download Error Fixer for Free Now

For ii = 1 to 4
Sheets(ii).Select

'           irow = 1
            While (Not (IsEmpty(ActiveSheet.Cells(irow, 1))))
                irow = irow + 1
            Wend
            irow = irow - 2
 
        If irow < 0 and ii = 4 Then
   Worksheets("THRESHOLD").Select
            Range("B2").Select
            Exit Sub
Elseif irow < 0 then
Next ii
Else

Each 'block' of code has to be self contained, e.g.
If
   While
   WEnd
endif
 
is allowed, but:
 
If
   While
   endif
Wend
 
is not allowed because the code can't figure out how to process that.
 
You have an embedded if statement (highlighted above) which opens a code block, but then the compiler hits the
Next ii which doesn't correspond to a FOR statement within the same code block, it is at a higher level.
 
If you need to increment ii at that point in your code, use ii = ii+1, but whether that is actually your intent is not clear to me.
 
HTH

Check if there are memory problems

Memory management issues can also cause a BSOD in Windows 7. Thankfully, Windows 7 has a Memory Management Diagnostic tool that detects and solves problems related to system memory.

  1. Click on the Start button on the Taskbar.
  2. Type "Windows Memory Diagnostic Tool" or "Memory" (without quotes) in the Search box or,
  3. Click on Windows Memory Diagnostic Tool (in the search results).
  4. Click on "Restart now and check for problems (Recommended)".
  5. The system will restart, and you need to follow the instructions given on the screen.

Another Safe way to Fix the Problem: Embedding Next for iteration within an If Then statement: compiler issues.:

How to Fix Embedding Next for iteration within an If Then statement: compiler issues. with SmartPCFixer?

1. You can Download Error Fixer here. Install it on your system. When you open SmartPCFixer, it will perform a scan.

2. After the scan is done, you can see the errors and problems which need to be fixed.

3. The Fixing part is finished, the speed of your computer will be much higher than before and the errors have been removed.


Related: Download SONY SVE1512H1RB Synaptics PS/2 Port ClickPad driver,Method to Download SONY VGC-LT10E SigmaTel Audio Driver Update v.6.10.5614.0,Way to Download SONY VGN-CS118E/R AV Mode Button Utility v.1.1.01.09140 driver,Method to Download SONY VGN-FW375J/B Alps Pointing Device Driver v.5.7.0.92,Where to Download SONY VGN-NS270J/S Pioneer BD-ROM BDC-TD01 Firmware Update v.1.03,Data Export From Quickbooks To Excel,Date Format In Formula Bar And In The Cell Is Not The Same,Deleted The Snipping Tool,Desktop - Open Program Issues,Desktop Windows Manager Keeps Poping Up & It Reads That There Is A Problem & That Windows Will Check It Out & Send Me An Answer .
Read More: How to Fix Problem - Error 1335 - File corrupt?,Fast Solution to Problem: Enlarge print on emails,Fast Solution to Problem: Encoded subject line shown in default email app in WP8, incompatibility,How to Fix Problem - Enable autoplay for Windows Media Player?,enable cookies on acer windows 7 laptop [Solved],does anyone know what the dialog means "System.Media.FontFamily' threw an exception windows is checking for a solution?,Does Log Me In Ignition for my Android with Windows 7 Home Premium?,Download failure for my Blackberry and the process stops during the install,Display settings keep changing when I come back from sleep mode,Downloaded files are not saved