segmentationfault
Programmer
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.
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