Results 1 to 4 of 4

Thread: Loop problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    13

    Question Loop problem

    Guys and Galls or if you prefer... Gals & Guys!

    I have created a workbook in excel to compile resultsfrom a questionnaire (cuz my databasing skills in Access aint up to it!).

    I have built code that removes redundant rows on two sheets, then builds charts from the remaining data.

    I have a problem when I run it, it seems to lock into a loop which I can't see.

    This is particularly confusing to a newbie as I copied the code from another project that works fine.

    Can anyone spot the problem?

    Thanks in advance

    Frank

    PHP Code:
    Sub QuestionsChart()

    'Builds Chart for Questions

    Dim DATA
    Dim CURRENT_ROW


    DATA = Sheets("Questions Chart Ranges").Range("a1:b50")
    CURRENT_ROW = 1

    For Each DATALINE In DATA

    Do
        If IsError(Sheets("Questions Chart Ranges").Range("b" & CURRENT_ROW)) Then
        Rows(CURRENT_ROW & ":" & CURRENT_ROW).Select
        Selection.Delete Shift:=xlUp
        End If
    Loop Until IsError(Sheets("Questions Chart Ranges").Range("b" & CURRENT_ROW)) = False  '
    Ends delete redundant rows.

    CURRENT_ROW CURRENT_ROW 1

    Next DATALINE

        ActiveSheet
    .Protect (********)

        
    Application.Goto Reference:="QuestionChartRange"                                   'Selects Chart Range
        Charts.Add                                                                         '
    Adds new chart
        ActiveChart
    .ChartType xlColumnClustered                                          'Specifies chart type
        ActiveChart.SetSourceData Source:=Sheets("Questions Chart Ranges").Range( _
            "A1:B5"), PlotBy:=xlColumns                                                    '
    Builds chart
        ActiveChart
    .Location Where:=xlLocationAsNewSheetName:="Questions Chart"          'Creates as new worksheet
        With ActiveChart                                                                   '
    Sets chart title details
            
    .HasTitle False
            
    .Axes(xlCategoryxlPrimary).HasTitle False
            
    .Axes(xlValuexlPrimary).HasTitle False
        End With
        ActiveChart
    .HasLegend False                                                      'Sets chart legend details
        ActiveChart.ApplyDataLabels Type:=xlDataLabelsShowValue, LegendKey:=False
        ActiveWindow.Zoom = 85                                                              '
    Zooms chart size
        ActiveChart
    .SeriesCollection(1).DataLabels.Select                                   'Sets data labels format and position
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .Position = xlLabelPositionInsideEnd
            .Orientation = xlHorizontal
        End With
        ActiveChart.Deselect
        Sheets("Questions Chart").Select
        Sheets("Questions Chart").Move After:=Sheets(5)
        Call ButtonForGraph

    End Sub 

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Much of this appears spreadsheet specific, but one question for you:

    If you break in the loop, where do you break at.

    Can you step through the code (F8 in VB, but I don't know what in Excel/VBA)? If you can, and the infinite loop is the loop in your code, then you will probably find that the loop control statement is not doing quite what you expect it to.

  3. #3
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    1. Try using VBCODE tags instead of PHP.
    2. See changes to your code.
    VB Code:
    1. For Each DATALINE In Data
    2.     Do
    3.         If IsError(Sheets("Questions Chart Ranges").Range("b" & CURRENT_ROW)) Then
    4.             Rows(CURRENT_ROW & ":" & CURRENT_ROW).Select
    5.             Selection.Delete Shift:=xlUp
    6.             'you may now decide if you want to get out of the loop
    7.             Exit Do
    8.         End If
    9.     Loop Until IsError(Sheets("Questions Chart Ranges").Range("b" & CURRENT_ROW)) = False  'Ends delete redundant rows.
    10.     CURRENT_ROW = CURRENT_ROW + 1
    11. Next DATALINE
    Roy

  4. #4
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    IROY55 is right, you have to leave loop or increase CURRENT_ROW value in 1:
    VB Code:
    1. Do
    2.     If IsError(Sheets("Questions Chart Ranges").Range("b" & CURRENT_ROW)) Then
    3.     Rows(CURRENT_ROW & ":" & CURRENT_ROW).Select
    4.     Selection.Delete Shift:=xlUp
    5.     End If
    6.     'increase CURRENT_ROW value
    7.     CURRENT_ROW=CURRENT_ROW+1
    8. Loop Until IsError(Sheets("Questions Chart Ranges").Range("b" & CURRENT_ROW)) = False

Posting Permissions

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



Click Here to Expand Forum to Full Width