end if without block if

pandale

New Member
Joined
Aug 4, 2014
Messages
7
Do While lrow <= last
Debug.Print (CountNumber(1, 1))
Debug.Print (Cells(lrow, 7) / 1000)
​I'M HAVING A BRAIN MELTDOWN, WHY IS THIS IF THEN ELSE NOT WORKING
If lrow = 1 Then
CountNumber(count, 1) = lrow
CountNumber(count, 2) = Cells(count, 7) / 1000
count = 2
Else: For d = 1 To 100
If CountNumber(d, 2) = Cells(lrow, 7) / 1000 Then
CountNumber(d, 1) = CountNumber(d, 1) + 1
CountNumber(d, 2) = Cells(lrow, 7) / 1000
d = 100
End If
lrow = lrow + 1
End If
Next
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Welcome to the forum.

The final two lines of your code need reversing (the If and For-Next blocks overlap) and a Loop needs to be added to the end.

If you write in indented blocks in the VBE it's relatively easy to spot overlapping block errors and you can copy the indented blocks here using the code Tags (the # button) as per below.

Code:
Do While lrow <= last
     Debug.Print (CountNumber(1, 1))
     Debug.Print (Cells(lrow, 7) / 1000)
     'I 'M HAVING A BRAIN MELTDOWN, WHY IS THIS IF THEN ELSE NOT WORKING
     If lrow = 1 Then
        CountNumber(Count, 1) = lrow
        CountNumber(Count, 2) = Cells(Count, 7) / 1000
        Count = 2
     Else
        For d = 1 To 100
            If CountNumber(d, 2) = Cells(lrow, 7) / 1000 Then
                CountNumber(d, 1) = CountNumber(d, 1) + 1
                CountNumber(d, 2) = Cells(lrow, 7) / 1000
                d = 100
            End If
            lrow = lrow + 1
        Next d 'moved up one line and d added to define what is incrementing
     End If
Loop 'Added
 
Upvote 0
Last Line Substitute Loop versus Next

@Jim,

Actually, that is not the problem. Yes, the code is missing the Loop statement to close off the Do loop, but there is a lot more wrong with the code.



@pandale,

Consider this line...

Code:
Else: For d = 1 To 100

You should avoid linking what should be two separate lines together using the colon operator to link them. When you do that, and using indenting to make the code's logic clearer, you get this...

Code:
Do While lrow <= last
  Debug.Print (CountNumber(1, 1))
  Debug.Print (Cells(lrow, 7) / 1000)
  If lrow = 1 Then
    CountNumber(Count, 1) = lrow
    CountNumber(Count, 2) = Cells(Count, 7) / 1000
    Count = 2
  Else
    For d = 1 To 100
      If CountNumber(d, 2) = Cells(lrow, 7) / 1000 Then
        CountNumber(d, 1) = CountNumber(d, 1) + 1
        CountNumber(d, 2) = Cells(lrow, 7) / 1000
        d = 100
      End If
      lrow = lrow + 1
      End If
    Next
There are two End..If statement within the For..Next loop, but only on If statement. On the other hand, you have one If statement at the beginning of your code with no matching End..If statement. I think you may want to reverse the last two statements... and add the Loop statement to closet Do loop off. My guess is your code should look like this...

Code:
Do While lrow <= last
  Debug.Print (CountNumber(1, 1))
  Debug.Print (Cells(lrow, 7) / 1000)
  If lrow = 1 Then
    CountNumber(Count, 1) = lrow
    CountNumber(Count, 2) = Cells(Count, 7) / 1000
    Count = 2
  Else
    For d = 1 To 100
      If CountNumber(d, 2) = Cells(lrow, 7) / 1000 Then
        CountNumber(d, 1) = CountNumber(d, 1) + 1
        CountNumber(d, 2) = Cells(lrow, 7) / 1000
        d = 100
      End If
      lrow = lrow + 1
    Next
  End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,637
Latest member
Ezio2866

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