+ Reply to Thread
Results 1 to 4 of 4

Issue with Do Loop

Hybrid View

  1. #1
    Linking to specific cells in pivot table
    Guest

    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. #2
    Norman Jones
    Guest

    Re: 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"
    <Linkingtospecificcellsinpivottable@discussions.microsoft.com> wrote in
    message news:4605BE53-E53D-40AB-9FDC-EE05B9CA150D@microsoft.com...
    > 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. #3
    JE McGimpsey
    Guest

    Re: 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 <4605BE53-E53D-40AB-9FDC-EE05B9CA150D@microsoft.com>,
    "Linking to specific cells in pivot table"
    <Linkingtospecificcellsinpivottable@discussions.microsoft.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. #4
    Linking to specific cells in pivot table
    Guest

    Re: 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 <4605BE53-E53D-40AB-9FDC-EE05B9CA150D@microsoft.com>,
    > "Linking to specific cells in pivot table"
    > <Linkingtospecificcellsinpivottable@discussions.microsoft.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 to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1