Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ENDIF without IF block 4

Status
Not open for further replies.

techie88

Programmer
Aug 30, 2011
8
US

Why do I keep getting Compile Error: End If without if block

Here is my code.

Sub TotalFluid()

Dim i, j, k As Integer
Dim totalFluids As Integer
Dim fID As Integer
i = 2
j = 2
totalFluids = 0

Do While i < 37
Workbooks("Book1.xlsm").Sheets("Sheet1").Activate
dVal = Cells(i, 7)

For j = 2 To 58
Set oBook = Workbooks("Book2.xlsx")
With oBook.Sheets("Sheet2")
d = Cells(j, 1)
For k = 1 To Len(d)
oneChar = VBA.Strings.Mid(d, j, 1)
d1 = d1 & oneChar
If oneChar = "/" And k > 3 Then
d1 = d1 & "2009"
k = Len(d)
End If

Next k

If dVal = d1 Then
Set oBook = Workbooks("Book2.xlsx")
With oBook.Sheets("Sheet2")
If d2 Like d1 Then
totalFluids = ActiveSheet.Cells(j, 4) + totalFluids
Workbooks("Book1.xlsm").Sheets("Sheet1").Activate
ActiveSheet.Cells(i, 9) = totalFluids
totalFluids = 0
d2 = d1
ElseIf d2 <> d1 Then
totalFluids = ActiveSheet.Cells(j, 4) + totalFluids
d2 = d1
End If
End If

Next j
i = i + 1
Loop
'
End Sub
 
You have ane extra end if at the end.... I think...

Fee

"The cure for anything is salt water – sweat, tears, or the sea." Isak Dinesen
 
Do While i < 37

For j = 2 To 58
With oBook.Sheets("Sheet2")
'No end if

For k = 1 To Len(d)

If oneChar = "/" And k > 3 Then
End If

Next k

If dVal = d1 Then

With oBook.Sheets("Sheet2")
'No end if

If d2 Like d1 Then

ElseIf d2 <> d1 Then

End If
End If

Next j

Loop

To check this take out all code except the blocks. All your loop and end ifs are good. With end with are not.
 
You are missing the end with in two places
 
Also, this:

Dim i, j, k As Integer

isn't doing what you think it is doing ...
 
BTW when you get these messages in vb about blocks structures it is often not the structure with the problem, but the structure inside the block that is the problem. So you have to check all block structures not the one mentioned in the error message

Do
if
'no end if
loop

I think you will get the error message "Loop without do", not "if without end if"

Sorry
in my post I put
'no end if
should have said
'no end with
 


TIP:

When I code block structures, includeing IF, With, For, Do, etc. I ALWAYS code the entire structure and then go back and 'fill in the blanks' so to speak.

For instance, for an If...
Code:
If x Then

Else

End if
That's what I begin with. Then I 'fill in the blanks'...
Code:
If var1 < var2 Then
   var2 = var1
Else
   var1 = var2
End if

Also, as strongm indicated
strongm said:
Dim i, j, k As Integer

isn't doing what you think it is doing ...
In your declaration, only k ihas been declared an integer. i & j are both variants by default.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
does this mean, I have to put every single declaration of a variable on a single line?

Dim i As Integer
Dim j As Integer
Dim k As Integer
 

That is my 'preferred' way:
[tt]
Dim i As Integer
Dim j As Integer
Dim k As Integer [/tt]

But you can also do that:
[tt]
Dim i As Integer, j As Integer, k As Integer
[/tt]

Have fun.

---- Andy
 


You could, or...
Code:
Dim i, as integer, j as integer, k As Integer, s as string, a as long
Putting on a separate line enables you to document the variable with a comment
Code:
Dim i As Integer   'loop counter
Dim j As Long      'row counter
Dim k As Integer   'column counter
I perfer to use variables that are more self documenting and self type-identifying
Code:
dim iCol as Integer      'col counter
dim lRow as Long         'row counter
dim sFName as string     'file name
dim wsNew as worksheet   'new worksheet object
dim rgDate as range      'data range object
dim sp as Shape          'shape loop object
dim ws as worksheet      'worksheet loop object


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
techie88,
I also recommend Pretty Code Print it graphically draws a vertical line down the side of the page between the if statements, do loops etc.
I've found it very handy when debugging large pieces of code - I often just use it in preview mode so I don't kill half a dozen trees every time I make a mistake.

It's available for free now:


The Author should be awarded the Victoria Cross for the Wars he has saved me from.

Regards,
Leandra G, Melbourne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top