So I have a feeling I'm probably over complicating this but...
I have some code that imports a certain Worksheet from an Excel file into an Access table. This Excel file is generated from a preset report in another department, and they can potentially change the report preset so that the Worksheet name changes. Hopefully very rarely, but I want to future-proof my code.
So I have a small set number of variables in an array for each name that the sheet could be (currently 2):
and here is my import code:
I know the best way to handle errors is to avoid them, but since it's checking an external file I have some error handling that checks if error 3125 is thrown, which is when it cannot find the sheet. When that error is encountered (i) is increased by 1 and so it moves on to the next item in the array.
I also have an error message and an exit clause when none of the names are found. This works fine if I set the array from, say, 1 to 10 and then check "If i > 2".
However, I also want to print all the items from the array in the error message box, and having 8 blank arrays looks very sloppy in the message box.
Checking anything to do with strSheetName(i) when (i) is out of the defined range (e.g. 3) quite rightly throws up a subscript out of range error.
Here is that code:
Is there a way to perform an action when the integer is out of range?
I want it to be dynamic so that when a new item is added to the list in VBA the message box will not have to be updated too (as it sits right down the bottom of the code).
Thanks for any ideas! (I'm sure I'll have some grumbles about error handling haha)
I have some code that imports a certain Worksheet from an Excel file into an Access table. This Excel file is generated from a preset report in another department, and they can potentially change the report preset so that the Worksheet name changes. Hopefully very rarely, but I want to future-proof my code.
So I have a small set number of variables in an array for each name that the sheet could be (currently 2):
Code:
'Set variables to check for each sheet name
Dim i As Integer
i = 1
Dim strSheetName_final As String
Dim strSheetName(1 To 2) As String
strSheetName(1) = "After Provision"
strSheetName(2) = "Sheet1"
Code:
ImportExcel:
'Import file using file path, sheet name, and import table
On Error GoTo Err_Import
strSheetName_final = strSheetName(i)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, strTable, strFilePath, True, strSheetName_final & "$"
I also have an error message and an exit clause when none of the names are found. This works fine if I set the array from, say, 1 to 10 and then check "If i > 2".
However, I also want to print all the items from the array in the error message box, and having 8 blank arrays looks very sloppy in the message box.
Checking anything to do with strSheetName(i) when (i) is out of the defined range (e.g. 3) quite rightly throws up a subscript out of range error.
Here is that code:
Code:
Err_Import:
If Err.Number = 3125 Then
i = i + 1
If VarType(strSheetName(i)) = 0 Then '<<<<< here I cannot figure out
MsgBox "The following sheet(s) were not found in the target file:" & vbNewLine & _
"""" & Join(strSheetName(), ", ") & """" & vbNewLine & _
"Please check the format of the target file and try again.", vbOKOnly, "Error " & Err.Number
Resume Exit_Func
End If
Resume ImportExcel
End If
I want it to be dynamic so that when a new item is added to the list in VBA the message box will not have to be updated too (as it sits right down the bottom of the code).
Thanks for any ideas! (I'm sure I'll have some grumbles about error handling haha)