I have a few workbooks that I have developed for sizing of equipment. I export the details of the design via VBA to a CSV file to make it easier to load back in. Attached below is the code. The script works most of the time but if I try try load a CSV file shortly after saving one, excel sometimes crashes and then restarts etc. I have 4 different sizing workbooks for different equipment, each one based on the same VBA code and they all have the same issue.
One item of note, when I save the CSV file, I add text to the end of the file names to identify the type of equipment that is sized as well as keep it from being identified as an excel file. This is done by an export script. for example "Filename.csv" gets saved as "Filename.csv.WHRU"
'''''''''''''''''''''''''''''''''''''''''''
Worksheets(12).Activate
Dim fStr As String With Application.FileDialog(msoFileDialogFilePicker) .Show
If .SelectedItems.Count = 0 Then
MsgBox "Cancel Selected"
Exit Sub
End If
'fStr is the file path and name of the file
fStr = .SelectedItems(1)
End With
With ThisWorkbook.Sheets(12).QueryTables.Add(Connection:= _
"TEXT;" & fStr, Destination:=ThisWorkbook.Sheets(12).Cells(3, 7))
.Name = "CAPTURE"
.FieldNames = True
.RefreshStyle = xlOverwriteCells
.TextFileCommaDelimiter = True
.Refresh BackgroundQuery:=False
End With
For Each Cn In ThisWorkbook.Connections
Cn.Delete
Next Cn
For Each Cn In Sheets(12).QueryTables
Cn.Delete
Next Cn
'''''''''''''''''''''''''''''''''''''''''''
Option Explicit
at the top of each Form/Module/Class. You can default this setting by selectingTools->Options->Require Variable Declaration
. Now, keep doingDebug->Compile VBAProject
until all errors are gone. I'm fairly certain that you're reusing variables, likeCn
, without declaring/setting them properly. Where is the code where you save the file? – ProfexWorksheets(12)
is not always the same asSheets(12)
.Sheets(3)
could be a chart and mess everything up afterwards. – Profex