+ Reply to Thread
Results 1 to 4 of 4

For loop exiting prematurely

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    180

    For loop exiting prematurely

    A “for loop” is exiting prematurely.

    You can find the loop that is giving me troubles at the line(about half way down):
    “For Row = DataRow To Ptr2FDc.count + DataRow”

    The debugger gets as far as just before executing the line:
    .Cells(Row, ElevationIDColumn).value = Ptr2FDc(Index).ID

    The code exits immediately (without executing the above line of code) to the end of the workbook.

    I checked the “for” variables. DataRow IS 2 and Ptr2FDc.count IS 20
    Index has a valid number of 1

    In the debugger I can see the PTR2FDc collection object and it does have an object in the collection at index 1. It is the correct object.

    There is no error code

    Public Sub PrintFDcollection(ByVal Wrkbk As Workbook, ByVal WrkSht4Printout As Worksheet, ByVal FieldDataCollection As MeasurementCollection)
        Dim Ptr2FDc As MeasurementCollection
        Dim LabelRow As Long
        Dim DataRow As Long
        Dim Row As Long
        Dim Index As Long
        Dim EndRowNum As Long
        Const ElevationIDColumn = 1
        Const X_CoordinateColumn = 2
        Const Y_CoordinateColumn = 3
        Const ElevationColumn = 4
        Const ElevationTypeColumn = 5
        
        With Wrkbk
            With WrkSht4Printout
    '           Instantiate FieldDataCollection (copy of original) as a pointer
                Set Ptr2FDc = FieldDataCollection
            
    '           store column labels in FieldDataCollection object
                   LabelRow = 1
                   .Cells(LabelRow, ElevationIDColumn).value = Ptr2FDc.IDColumnLabel
                   .Cells(LabelRow, X_CoordinateColumn).value = Ptr2FDc.X_CoordinateColumnLabel
                   .Cells(LabelRow, Y_CoordinateColumn).value = Ptr2FDc.Y_CoordinateColumnLabel
                   .Cells(LabelRow, ElevationColumn).value = Ptr2FDc.ElevationColumnLabel
                   .Cells(LabelRow, ElevationTypeColumn).value = Ptr2FDc.ElevationTypeColumnLabel
    '
    '           for as many Measurements exist in FieldData
                DataRow = 2
                EndRowNum = Ptr2FDc.count + DataRow
                For Row = DataRow To Ptr2FDc.count + DataRow
    '              read MeasurementObj data into worksheet
                    Index = Row - 1
                   .Cells(Row, ElevationIDColumn).value = Ptr2FDc(Index).ID
                   
                   .Cells(Row, X_CoordinateColumn).value = Ptr2FDc(Index).Coordinates.X
                   .Cells(Row, Y_CoordinateColumn).value = Ptr2FDc(Index).Coordinates.Y
                   
                   
                   .Cells(Row, ElevationColumn).value = Ptr2FDc(Index).Elevation
                   .Cells(Row, ElevationTypeColumn).value = Ptr2FDc(Index).ElevationType
                 Next
            End With
        End With
    End Sub
    Last edited by JBeaucaire; 05-30-2017 at 02:05 AM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,229

    Re: a "for loop" is exiting prematurely

    Hi,

    Edit your first message and Select the code. Then click on the "#" Icon above the message area. This will put code tags around what you've selected. After you do that, read my below suggestion.

    I think this line is the problem.
    For Row = DataRow To Ptr2FDc.count + DataRow
    DataRow starts at 2 and then goes to 3 + Ptr2FDc.count... This doesn't make sense to me.

    Try
    For Row = DataRow To Ptr2FDc.count
    Last edited by MarvinP; 05-29-2017 at 04:01 PM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    180

    Re: a "for loop" is exiting prematurely

    Thank you for responding Marvin.

    my understanding of For Row = DataRow to Ptr2FDc.count is not the same as yours.

    My understanding is that Row starts at DataRow and indexes by 1 (default index) until Row reaches Ptr2FDc.count

    Perhaps I have the wrong understanding?

  4. #4
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    180

    Re: a "for loop" is exiting prematurely

    I tried an experiment.
    I replaced "Ptr2FDc(Index).ID with a simple number
    the "for loop" did not bail.

    so there is something wrong with Ptr2FDc(Index).ID (and all its other properties)

    I did use "set" to instantiate the collection. Maybe I have to use "set" to instantiate the objects the collection object
    contains.

    I thought that I could avoid that by using an index in the collection object.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Having difficulty with exiting a Do loop
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-17-2014, 10:03 PM
  2. How do i loop prematurely?
    By djblois1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-16-2012, 12:15 PM
  3. Problem with do loop - does not appear to be exiting
    By jbd99 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2012, 05:17 AM
  4. Macro terminates prematurely
    By aljanga in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-26-2009, 12:21 PM
  5. Not exiting For loop
    By gshock in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-13-2008, 03:50 PM
  6. New question about exiting a loop!
    By Canuckbulldog in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2008, 08:36 AM
  7. Function exists prematurely
    By brain in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-31-2006, 11:20 AM
  8. [SOLVED] Exiting a Do Loop on error
    By Cloudfall in forum Excel General
    Replies: 7
    Last Post: 11-02-2005, 06:17 PM

Tags for this Thread

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