jmenche
11-08-2006, 01:51 PM
Howdy,
I slapped together a couple of routines from the web and came up with this sub to import multiple worksheets from. It works, but does not work as fast as when I just do the File/BetExternalData/Import thing and import each sheet individually.
Can anyone see why my routine is so slow?
Thanks :beerchug:
ub MultiWStoAccess()
Dim objXL As Object
Dim objXLWB As Object
Dim objXLWS As Object
Dim objXLRNG As Object
Dim strRange As String
Dim strPath As String
Dim strFile As String
Dim MyBook As String
Dim fd As FileDialog
Dim vrtSelectedItem As Variant
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
MyBook = vrtSelectedItem
Next
Else: GoTo ExitSub
End If
End With
Set objXL = CreateObject("Excel.Application")
Set objXLWB = objXL.Workbooks.Open(MyBook)
For Each objXLWS In objXLWB.Worksheets
On Error GoTo ExitSub
strRange = objXLWS.Name & "!"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "MyData", MyBook, True, strRange
Next
ExitSub:
objXLWB.Close
objXL.Quit
Set objXLWB = Nothing
Set objXL = Nothing
Set fd = Nothing
End Sub
I slapped together a couple of routines from the web and came up with this sub to import multiple worksheets from. It works, but does not work as fast as when I just do the File/BetExternalData/Import thing and import each sheet individually.
Can anyone see why my routine is so slow?
Thanks :beerchug:
ub MultiWStoAccess()
Dim objXL As Object
Dim objXLWB As Object
Dim objXLWS As Object
Dim objXLRNG As Object
Dim strRange As String
Dim strPath As String
Dim strFile As String
Dim MyBook As String
Dim fd As FileDialog
Dim vrtSelectedItem As Variant
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
MyBook = vrtSelectedItem
Next
Else: GoTo ExitSub
End If
End With
Set objXL = CreateObject("Excel.Application")
Set objXLWB = objXL.Workbooks.Open(MyBook)
For Each objXLWS In objXLWB.Worksheets
On Error GoTo ExitSub
strRange = objXLWS.Name & "!"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "MyData", MyBook, True, strRange
Next
ExitSub:
objXLWB.Close
objXL.Quit
Set objXLWB = Nothing
Set objXL = Nothing
Set fd = Nothing
End Sub