Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 64
Default Issue with Do Loop

Hi - I'm having trouble understanding why the loop below is failing -- when I
execute the code I get the following error: "Compile error: Loop without
Do".

Any help is appreciated!! The code is below:

Sub thirdlevel()

Dim thirdvar 'variable for "3rd Level Groundwater" sheet
Dim copyvar 'variable for "Copy of Data for Graphs" sheet

'Checking "3rd Level Groundwater" sheet to see how many previous entries
there were
Sheets("3rd Level Groundwater (2)").Select
Range("E5").Select

thirdvar = 0

Do
If Len(ActiveCell.Offset(4 * thirdvar, 0)) = 0 Then
Exit Do
Else
thirdvar = thirdvar + 1
Loop
End If


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Issue with Do Loop

Hi Pivot,

You need to Place the loop statement after End If:

Sub thirdlevel()

Dim thirdvar 'variable for "3rd Level
'Groundwater" sheet

Dim copyvar 'variable for "Copy of Data
'for Graphs" sheet

' Checking "3rd Level Groundwater" sheet to see
' how many previous entries there were

Sheets("3rd Level Groundwater (2)").Select
Range("E5").Select

thirdvar = 0

Do
If Len(ActiveCell.Offset(4 * thirdvar, 0)) = 0 Then
Exit Do
Else
thirdvar = thirdvar + 1

End If
Loop

End Sub


---
Regards,
Norman



"Linking to specific cells in pivot table"
crosoft.com wrote in
message ...
Hi - I'm having trouble understanding why the loop below is failing --
when I
execute the code I get the following error: "Compile error: Loop without
Do".

Any help is appreciated!! The code is below:

Sub thirdlevel()

Dim thirdvar 'variable for "3rd Level Groundwater" sheet
Dim copyvar 'variable for "Copy of Data for Graphs" sheet

'Checking "3rd Level Groundwater" sheet to see how many previous entries
there were
Sheets("3rd Level Groundwater (2)").Select
Range("E5").Select

thirdvar = 0

Do
If Len(ActiveCell.Offset(4 * thirdvar, 0)) = 0 Then
Exit Do
Else
thirdvar = thirdvar + 1
Loop
End If




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Issue with Do Loop

Control structures like Do...Loop and If Then...Else...End If must be
nested - completely enclosed by other control structures. So you could
use:

Do
If Len(ActiveCell.Offset(4 * thirdvar, 0)) = 0 Then
Exit Do
Else
thirdvar = thirdvar + 1
End If
Loop

But there's really no reason to use the Exit Do. Try:

Do Until Len(ActiveCell.Offset(4 * thirdvar, 0)) = 0
thirdvar = thirdvar + 1
Loop


In article ,
"Linking to specific cells in pivot table"
crosoft.com wrote:

Hi - I'm having trouble understanding why the loop below is failing -- when I
execute the code I get the following error: "Compile error: Loop without
Do".

Any help is appreciated!! The code is below:

Sub thirdlevel()

Dim thirdvar 'variable for "3rd Level Groundwater" sheet
Dim copyvar 'variable for "Copy of Data for Graphs" sheet

'Checking "3rd Level Groundwater" sheet to see how many previous entries
there were
Sheets("3rd Level Groundwater (2)").Select
Range("E5").Select

thirdvar = 0

Do
If Len(ActiveCell.Offset(4 * thirdvar, 0)) = 0 Then
Exit Do
Else
thirdvar = thirdvar + 1
Loop
End If


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 64
Default Issue with Do Loop

Thanks Norman and JE!! This is what I needed.

"JE McGimpsey" wrote:

Control structures like Do...Loop and If Then...Else...End If must be
nested - completely enclosed by other control structures. So you could
use:

Do
If Len(ActiveCell.Offset(4 * thirdvar, 0)) = 0 Then
Exit Do
Else
thirdvar = thirdvar + 1
End If
Loop

But there's really no reason to use the Exit Do. Try:

Do Until Len(ActiveCell.Offset(4 * thirdvar, 0)) = 0
thirdvar = thirdvar + 1
Loop


In article ,
"Linking to specific cells in pivot table"
crosoft.com wrote:

Hi - I'm having trouble understanding why the loop below is failing -- when I
execute the code I get the following error: "Compile error: Loop without
Do".

Any help is appreciated!! The code is below:

Sub thirdlevel()

Dim thirdvar 'variable for "3rd Level Groundwater" sheet
Dim copyvar 'variable for "Copy of Data for Graphs" sheet

'Checking "3rd Level Groundwater" sheet to see how many previous entries
there were
Sheets("3rd Level Groundwater (2)").Select
Range("E5").Select

thirdvar = 0

Do
If Len(ActiveCell.Offset(4 * thirdvar, 0)) = 0 Then
Exit Do
Else
thirdvar = thirdvar + 1
Loop
End If



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find loop doesn't loop JSnow Excel Discussion (Misc queries) 2 June 24th 09 08:28 PM
For next do loop issue Christian Falde Excel Discussion (Misc queries) 4 November 11th 08 07:01 PM
Problem adding charts using Do-Loop Until loop Chris Bromley[_2_] Excel Programming 2 May 23rd 05 01:31 PM
Worksheet_Change - loop within a loop bgm Excel Programming 1 January 19th 04 01:27 PM
HELP!!!! Can't stop a loop (NOT an infinite loop) TBA[_2_] Excel Programming 3 December 14th 03 03:33 PM


All times are GMT +1. The time now is 09:40 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"