Error in Code

N

N.F

Hello, I am trying to figure out what is wrong with my code but cannot figure
it out
Anyone?? Thank you so much in advance. im sure the solution is simple im
just not seeing it!
I keep getting a compile error: " Do without Loop"


Heres part of my code;

Sub GenerateTextFile()
Dim Coll As Integer ' output column index
Dim HeaderRow As Long
Dim Heading1 As String
Dim Heading2 As String
Dim Heading3 As String

HeaderRow = Range("Header").Row ' get location of header row.
Heading1 = Sheets("Sheet1").Cells(HeaderRow, 2) ' get headings
Heading2 = Sheets("Sheet1").Cells(HeaderRow, 3)
Heading3 = Sheets("Sheet1").Cells(HeaderRow, 4)

Sheets("Output").Cells.ClearContents ' clear output sheet
Roww = HeaderRow + 1 ' first row
i = 1
Do
Coll = 1
Sheets("Output").Cells(i, Coll) = "&D" ' First field

If Sheets("Sheet1").Cells(Roww, 2) <> "" Then
Coll = Coll + 1 ' bump column index
Sheets("Output").Cells(i, Coll) = " " & Heading1 & "=" &
Sheets("Sheet1").Cells(Roww, 2) & "." ' First field
End If
If Sheets("Sheet1").Cells(Roww, 3) <> "" Then
Coll = Coll + 1 ' bump column index
Sheets("Output").Cells(i, Coll) = " " & Heading2 & "=" &
Sheets("Sheet1").Cells(Roww, 3) & "." ' First field
End If
If Sheets("Sheet1").Cells(Roww, 4) <> "" Then
Coll = Coll + 1 ' bump column index
Sheets("Output").Cells(i, Coll) = " " & Heading3 & "=" &
Sheets("Sheet1").Cells(Roww, 4) & "." ' First field
End If

Do
Coll = Coll + 1 ' bump column index
Sheets("Output").Cells(i, Coll) = "/" '
last slash


Roww = Roww + 1 ' next row
i = i + 1
Loop While Sheets("Sheet1").Cells(Roww, 1) <> ""
End If
End Sub
 
F

FSt1

hi
looks like you have 1 too many end if statements at the bottom just before
end sub.
if you don't have an end if for each if when using a do statement, you will
get the do without a loop error message.
yeah, i know. confusing.

regards
FSt1
 
N

N.F

Thanks for your help, but now that I deleted that End if statement
Now I get an error saying "Block If without End If " error
 
F

FSt1

hi,
based on the code you posted, I count 3 ifs and 4 end ifs.
3 ifs and end ifs before the do loop and 1 end if inside the do loop(without
an if).
count all your ifs and end ifs and make sure you have 1 for 1.
post back with results.
regards
FSt1
 
F

FSt1

hi,
excuse me. I did spot an extra do. up near the top after i=1. but i'm still
counting an odd numer of ifs and end ifs.

Regards
FSt1
 
N

N.F

I counted carefully the If's and End If's statements and I have "Do Without
Loop Messege"

I would send you my entire code but its too long.
 
J

Jennifer

You've got 2 Do statements and only 1 Loop. You have an End If
towards the bottom without a beginning If. I've marked them all below
with '==>> so you can figure out which Do you want.

Jennifer


Sub GenerateTextFile()
Dim Coll As Integer ' output column index
Dim HeaderRow As Long
Dim Heading1 As String
Dim Heading2 As String
Dim Heading3 As String

HeaderRow = Range("Header").Row ' get location of header row.
Heading1 = Sheets("Sheet1").Cells(HeaderRow, 2) ' get
headings
Heading2 = Sheets("Sheet1").Cells(HeaderRow, 3)
Heading3 = Sheets("Sheet1").Cells(HeaderRow, 4)


Sheets("Output").Cells.ClearContents ' clear
output sheet
Roww = HeaderRow + 1 ' first row
i = 1
'==>>Do
Coll = 1
Sheets("Output").Cells(i, Coll) = "&D" ' First field


If Sheets("Sheet1").Cells(Roww, 2) <> "" Then
Coll = Coll + 1 ' bump column index
Sheets("Output").Cells(i, Coll) = " " & Heading1 & "=" &
Sheets("Sheet1").Cells(Roww, 2) & "." ' First field
End If

If Sheets("Sheet1").Cells(Roww, 3) <> "" Then
Coll = Coll + 1 ' bump column index
Sheets("Output").Cells(i, Coll) = " " & Heading2 & "=" &
Sheets("Sheet1").Cells(Roww, 3) & "." ' First field
End If

If Sheets("Sheet1").Cells(Roww, 4) <> "" Then
Coll = Coll + 1 ' bump column index
Sheets("Output").Cells(i, Coll) = " " & Heading3 & "=" &
Sheets("Sheet1").Cells(Roww, 4) & "." ' First field
End If


'==>> Do
Coll = Coll + 1 ' bump column index
Sheets("Output").Cells(i, Coll) =
"/" '
last slash

Roww = Roww + 1 ' next row
i = i + 1
'==>> Loop While Sheets("Sheet1").Cells(Roww, 1) <> ""
'==>> End If
End Sub
 
N

N.F

YESSSS!!!! OK FSt1 I think we got it, after looking at it again I fixed the
Ifs and End Ifs and yes, we did have an extra Do. THANK YOU
 
F

FSt1

hi,
did you read my last post.
i did find an extra do. up nead the top after i-1. I missed it first time
through and though that the end if was the problem. I have gotten the do
without loop message before because of ifs and end ifs.

sorry.
regards
FSt1
 
N

N.F

YESSSS!!!! OK FSt1 I think we got it,!!!! after looking at it again I fixed the
Ifs and End Ifs and yes, we did have an extra Do. THANK YOU

Thanks FSt1 and Jennifer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

CODING ERRor 1
Minimizing number of commands 0
Range headings 0
Need Help with a VBA subroutine 0
P 1
Using Collection to also capture range adjacent 10
VBA for Dependents shortcut menu 0
Interpretation 2

Top