Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inconsistent execution, step vs. run

Status
Not open for further replies.

segmentationfault

Programmer
Jun 21, 2001
160
US
This is swiftly making me insane. Any suggestions are appreciated.

I have a sub that I trigger with the BeforeSave event. This code reads certain cells in ThisWorkbook, then opens a second workbook, records these values, saves workbook 2, then ends, allowing ThisWorkbook to save. In effect, ThisWorkbook is a document and workbook 2 is a log book which automatically tracks the document's number, author, and various dates.

When I step through the code, it functions perfectly. When the code is run, it fails to read any values from ThisWorkbook. I have interrupted it and watched all the values to specify the problem as far as possible. These blank values are then written to the logbook.

If I interrupt the code at the end of the sub and drag the execution back to the beginning and step through it, it works perfectly, in my mind eliminating the possibility of poor assumptions about the environment.

Here is a simplified version of the very mundane, run of the mill sub. I'm left thinking that there is some bizarre race condition that is preventing it from reading the proper workbook, unless there is some well known fault with VBA or these methods that is unknown to me.

I left out some arithmetic and context from this macro, but I cannot stress enough that it works flawlessly when stepped through, but fails completely when run.

Code:
Public Sub UpdateLogbook()
    Dim From As String
    Dim Desc As String
    Dim FromDate As Date
    Dim ThruDate As Date
    
    Application.ScreenUpdating = False
    ThisWorkbook.Activate
    Sheets(1).Activate
    
    From = Cells(Constants.FromRow, Constants.FieldsCol)
    Desc = Cells(Constants.BodyRow, Constants.BodyCol)
    FromDate = CDate(Cells(Constants.IssuedDateRow, Constants.DatesCol))
    ThruDate = CDate(Cells(Constants.ValidThruRow, Constants.DatesCol))
    
    Workbooks.Open Constants.LogbookFilepath & Constants.LogbookFilename
    Set Constants.LogBook = ActiveWorkbook
    Constants.LogBook.Sheets(1).Activate
    Columns(Constants.LBNumberCol).Select
    
    Dim aRange As Range
    ' Selects a predefined row based on a predetermined log book number
    Set aRange = Selection.Find(what:=Constants.LogBookNumber, lookat:=xlWhole)
    If Not aRange Is Nothing Then
        ' Inserts values into the log book.  If this macro is run,
        ' all values to the right of the assignment operator are
        ' uninitialized.  If the macro is stepped, they contain
        ' the appropriate data.
        Cells(aRange.Row, Constants.LBDescCol) = Desc
        Cells(aRange.Row, Constants.LBNameCol) = From
        Cells(aRange.Row, Constants.LBIssueCol) = FromDate
        Cells(aRange.Row, Constants.LBExpireCol) = ThruDate
    End If

    Workbooks(Constants.LogBook.Name).Save
    Workbooks(Constants.LogBook.Name).Close

    ThisWorkbook.Activate
    Application.ScreenUpdating = True
End Sub
 
Stupid mistake, no surprise there.

It turns out that the workbook had some very hidden pages preceding the one I was using, hence Sheets(1) refers to the incorrect sheet.

I know for an absolute fact that I stepped the macro through the Sheets(1).Activate line and it read the correct cells, but I'm not very eager to pursue a reason for that. I might guess that it has something to do with the fact that screen updating does not turn off when one steps through a macro, therefore Sheets(1) was not being properly activated when stepped, and all the cell references were then reading the appropriate sheet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top