I have a VBA function in Excel which looks up data from another workbook, which is normally closed. The function is used several times in the master worksheet.
Sometimes, the function works perfectly OK, but Excel frequently crashes and reboots.
I am running Excel 2010 on a Windows 7 machine, but have also tried copying the files to a PC with XP running Excel 2007, and this also crashes. Any ideas what the problem is? The code for the function is copied below.
Function Cost(SheetIn, TableIn, ColID1, ColID2, RowID)
Dim RRow As Integer 'Row containing the Table Name
Dim RowNo As Integer 'Row with the required data
Dim ColNo As Integer 'Column with the required data
Dim StopRow As Integer 'Number of data rows in the table
Dim CostFile As Workbook 'Workbook containing the Cost Functions
Dim HelpText As String 'Formula output in the event of unsuccessful lookup
Set CostFile = GetObject("D:\Work\Technology\Overall Model\Cost Functions.xlsm")
'------------------------------------------------------------------------------------------------------------------------------------------
'Look for the Worksheet, and output the relevant Help Text if the right sheet doesn't exist
'------------------------------------------------------------------------------------------------------------------------------------------
Check = False
For I = 1 To CostFile.Sheets.Count
If CostFile.Sheets(I).Name = SheetIn Then
Check = True
I = CostFile.Sheets.Count
End If
Next I
If Check = False Then
HelpText = "SheetIn should be one of following: "
For I = 1 To CostFile.Sheets.Count
HelpText = HelpText + CostFile.Sheets(I).Name + ", "
Next I
Cost = HelpText
GoTo 100
End If
'------------------------------------------------------------------------------------------------------------------------------------------
'Look for the Data Table, and output the relevant Help Text if the right table doesn't exist
'Count the number of Rows in the Data Table
'------------------------------------------------------------------------------------------------------------------------------------------
Check = False
For I = 1 To 1000
If CostFile.Sheets(CStr(SheetIn)).Cells(I, 1) = TableIn Then
RRow = I
Check = True
I = 1000
End If
Next I
If Check = False Then
HelpText = "TableIn should be one of following: "
For I = 1 To 1000
If CostFile.Sheets(CStr(SheetIn)).Cells(I, 1) <> "" Then
HelpText = HelpText + CostFile.Sheets(CStr(SheetIn)).Cells(I, 1) + ", "
End If
Next I
Cost = HelpText
GoTo 100
End If
For I = 4 To 104
If CostFile.Sheets(CStr(SheetIn)).Cells(RRow + I, 2) = "" Then
StopRow = I
I = 104
End If
Next I
'------------------------------------------------------------------------------------------------------------------------------------------
'Look for the Columm Headings in Data Table, and output the relevant Help Text if the Column Headings doesn't exist
'------------------------------------------------------------------------------------------------------------------------------------------
Check = False
For J = 3 To 50
If CostFile.Sheets(CStr(SheetIn)).Cells(RRow, J) = ColID1 And CostFile.Sheets(CStr(SheetIn)).Cells(RRow + 1, J) = ColID2 Then
ColNo = J
Check = True
J = 50
End If
Next J
If Check = False Then
For J = 3 To 50
If CostFile.Sheets(CStr(SheetIn)).Cells(RRow, J) = ColID1 Then
Check = True
End If
Next J
If Check = False Then
HelpText = "ColID1 should be one of following: "
For J = 3 To 50
If CostFile.Sheets(CStr(SheetIn)).Cells(RRow, J) <> "" Then
HelpText = HelpText + CostFile.Sheets(CStr(SheetIn)).Cells(RRow, J) + ", "
End If
Next J
Cost = HelpText
GoTo 100
Else
HelpText = "ColID2 should be one of following: "
For J = 3 To 50
If CostFile.Sheets(CStr(SheetIn)).Cells(RRow, J) = ColID1 Then
HelpText = HelpText + CostFile.Sheets(CStr(SheetIn)).Cells(RRow + 1, J) + ", "
End If
Next J
Cost = HelpText
GoTo 100
End If
End If
'------------------------------------------------------------------------------------------------------------------------------------------
'Look for the Row Item in Data Table, and output the relevant Help Text if the Row Item doesn't exist
'------------------------------------------------------------------------------------------------------------------------------------------
Check = False
For I = 3 To StopRow - 1
If CostFile.Sheets(CStr(SheetIn)).Cells(RRow + I, 2) = RowID Then
RowNo = I
Check = True
I = StopRow - 1
End If
Next I
If Check = False Then
HelpText = "RowID should be one of following: "
For I = 3 To StopRow - 1
HelpText = HelpText + CStr(CostFile.Sheets(CStr(SheetIn)).Cells(RRow + I, 2)) + ", "
Next I
Cost = HelpText
GoTo 100
End If
'------------------------------------------------------------------------------------------------------------------------------------------
'Output Result
'------------------------------------------------------------------------------------------------------------------------------------------
Cost = CostFile.Sheets(CStr(SheetIn)).Cells(RRow + RowNo, ColNo)
100 End Function
Sometimes, the function works perfectly OK, but Excel frequently crashes and reboots.
I am running Excel 2010 on a Windows 7 machine, but have also tried copying the files to a PC with XP running Excel 2007, and this also crashes. Any ideas what the problem is? The code for the function is copied below.
Function Cost(SheetIn, TableIn, ColID1, ColID2, RowID)
Dim RRow As Integer 'Row containing the Table Name
Dim RowNo As Integer 'Row with the required data
Dim ColNo As Integer 'Column with the required data
Dim StopRow As Integer 'Number of data rows in the table
Dim CostFile As Workbook 'Workbook containing the Cost Functions
Dim HelpText As String 'Formula output in the event of unsuccessful lookup
Set CostFile = GetObject("D:\Work\Technology\Overall Model\Cost Functions.xlsm")
'------------------------------------------------------------------------------------------------------------------------------------------
'Look for the Worksheet, and output the relevant Help Text if the right sheet doesn't exist
'------------------------------------------------------------------------------------------------------------------------------------------
Check = False
For I = 1 To CostFile.Sheets.Count
If CostFile.Sheets(I).Name = SheetIn Then
Check = True
I = CostFile.Sheets.Count
End If
Next I
If Check = False Then
HelpText = "SheetIn should be one of following: "
For I = 1 To CostFile.Sheets.Count
HelpText = HelpText + CostFile.Sheets(I).Name + ", "
Next I
Cost = HelpText
GoTo 100
End If
'------------------------------------------------------------------------------------------------------------------------------------------
'Look for the Data Table, and output the relevant Help Text if the right table doesn't exist
'Count the number of Rows in the Data Table
'------------------------------------------------------------------------------------------------------------------------------------------
Check = False
For I = 1 To 1000
If CostFile.Sheets(CStr(SheetIn)).Cells(I, 1) = TableIn Then
RRow = I
Check = True
I = 1000
End If
Next I
If Check = False Then
HelpText = "TableIn should be one of following: "
For I = 1 To 1000
If CostFile.Sheets(CStr(SheetIn)).Cells(I, 1) <> "" Then
HelpText = HelpText + CostFile.Sheets(CStr(SheetIn)).Cells(I, 1) + ", "
End If
Next I
Cost = HelpText
GoTo 100
End If
For I = 4 To 104
If CostFile.Sheets(CStr(SheetIn)).Cells(RRow + I, 2) = "" Then
StopRow = I
I = 104
End If
Next I
'------------------------------------------------------------------------------------------------------------------------------------------
'Look for the Columm Headings in Data Table, and output the relevant Help Text if the Column Headings doesn't exist
'------------------------------------------------------------------------------------------------------------------------------------------
Check = False
For J = 3 To 50
If CostFile.Sheets(CStr(SheetIn)).Cells(RRow, J) = ColID1 And CostFile.Sheets(CStr(SheetIn)).Cells(RRow + 1, J) = ColID2 Then
ColNo = J
Check = True
J = 50
End If
Next J
If Check = False Then
For J = 3 To 50
If CostFile.Sheets(CStr(SheetIn)).Cells(RRow, J) = ColID1 Then
Check = True
End If
Next J
If Check = False Then
HelpText = "ColID1 should be one of following: "
For J = 3 To 50
If CostFile.Sheets(CStr(SheetIn)).Cells(RRow, J) <> "" Then
HelpText = HelpText + CostFile.Sheets(CStr(SheetIn)).Cells(RRow, J) + ", "
End If
Next J
Cost = HelpText
GoTo 100
Else
HelpText = "ColID2 should be one of following: "
For J = 3 To 50
If CostFile.Sheets(CStr(SheetIn)).Cells(RRow, J) = ColID1 Then
HelpText = HelpText + CostFile.Sheets(CStr(SheetIn)).Cells(RRow + 1, J) + ", "
End If
Next J
Cost = HelpText
GoTo 100
End If
End If
'------------------------------------------------------------------------------------------------------------------------------------------
'Look for the Row Item in Data Table, and output the relevant Help Text if the Row Item doesn't exist
'------------------------------------------------------------------------------------------------------------------------------------------
Check = False
For I = 3 To StopRow - 1
If CostFile.Sheets(CStr(SheetIn)).Cells(RRow + I, 2) = RowID Then
RowNo = I
Check = True
I = StopRow - 1
End If
Next I
If Check = False Then
HelpText = "RowID should be one of following: "
For I = 3 To StopRow - 1
HelpText = HelpText + CStr(CostFile.Sheets(CStr(SheetIn)).Cells(RRow + I, 2)) + ", "
Next I
Cost = HelpText
GoTo 100
End If
'------------------------------------------------------------------------------------------------------------------------------------------
'Output Result
'------------------------------------------------------------------------------------------------------------------------------------------
Cost = CostFile.Sheets(CStr(SheetIn)).Cells(RRow + RowNo, ColNo)
100 End Function