Hi guys
I'm really struggling here, hope you can point me in the right direction. I am OK with Excel VBA code but am now looking at Access for the first time.
I pasted my code below and need to combine all sheets in all excel files into one table. Sheets all have different structures i.e. used range is different for each sheet by rows and by columns.
_________________________
CONTEXT
I tried using PowerQuery but it is taking waaaaaayyyyy too long.
Here is the scenario - I have about 150 excel workbooks.
Each workbook has between 40 and 90 sheets.
I want to
1. import all data from each sheet in each workbook into one single database
2. include a column with the filename
3. include a column with the sheetname
Once imported into access, I can then filter the data as needed. I have a separate Excel file listing every single file name, sheet name and a description of that sheet. This will help me to group the sheets together (into 150 group types).
_________________________________
My CODE to get files from one folder. I can't get the loop to work correctly. It is repeatedly bringing back data from the first file
NOTE: I created a dummy excel file with 1 line and 80 columns which gets imported first. All subsequent workbooks will have <80 columns.
I did this so that the Table is created with enough columns to accommodate all subsequent files that are imported.
Any help appreciated.
I'm really struggling here, hope you can point me in the right direction. I am OK with Excel VBA code but am now looking at Access for the first time.
I pasted my code below and need to combine all sheets in all excel files into one table. Sheets all have different structures i.e. used range is different for each sheet by rows and by columns.
_________________________
CONTEXT
I tried using PowerQuery but it is taking waaaaaayyyyy too long.
Here is the scenario - I have about 150 excel workbooks.
Each workbook has between 40 and 90 sheets.
I want to
1. import all data from each sheet in each workbook into one single database
2. include a column with the filename
3. include a column with the sheetname
Once imported into access, I can then filter the data as needed. I have a separate Excel file listing every single file name, sheet name and a description of that sheet. This will help me to group the sheets together (into 150 group types).
_________________________________
My CODE to get files from one folder. I can't get the loop to work correctly. It is repeatedly bringing back data from the first file
VBA Code:
Option Compare Database
Sub CombineAllsheets()
' code works but requires first sheet to have the maximum column used
Dim Starttime As String
Dim EndTime As String
Starttime = Time
Dim strPath As String
Dim strFile As String
strPath = _
"C:\2021.01\AR analysis\5\"
strFile = Dir(strPath & "*.xls*")
Dim blnHasFieldNames As Boolean, blnEXCEL As Boolean, blnReadOnly As Boolean
Dim lngCount As Long
Dim objExcel As Object, objWorkbook As Object
Dim colWorksheets As Collection
Dim strPathFile As String, strTable As String
Dim strPassword As String
'_________________
Do While strFile <> ""
' Establish an EXCEL application object
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set objExcel = CreateObject("Excel.Application")
blnEXCEL = True
End If
Err.Clear
On Error GoTo 0
blnHasFieldNames = False 'no headers in Excelfile
strPathFile = strPath & strFile 'strPath & strFile Excel filename
strTable = "Folder5_2" 'new access tablename
strPassword = vbNullString 'no password
blnReadOnly = False ' open EXCEL file in read-only mode
' Open the EXCEL file and read the worksheet names into a collection
Set colWorksheets = New Collection
Set objWorkbook = objExcel.Workbooks.Open(strPathFile, , blnReadOnly, , strPassword, updatelinks:=False)
For lngCount = 1 To objWorkbook.Worksheets.Count
objWorkbook.Sheets(lngCount).Activate
'objWorkbook.Sheets(lngCount).Visible = xlsheetvisible 'this does not seem to work
colWorksheets.Add objWorkbook.Worksheets(lngCount).Name
Next lngCount
' Close the EXCEL file without saving the file, and clean up the EXCEL objects
objWorkbook.Close False
Set objWorkbook = Nothing
If blnEXCEL = True Then objExcel.Quit
Set objExcel = Nothing
' Import the data from each worksheet into the table
For lngCount = colWorksheets.Count To 1 Step -1
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, _
strTable, strPathFile, blnHasFieldNames, colWorksheets(lngCount) & "$"
Next lngCount
Set colWorksheets = Nothing ' Delete the collection
Loop
' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile
EndTime = Time
MsgBox "Done" & vbCrLf & Starttime & vbCrLf & EndTime
End Sub
NOTE: I created a dummy excel file with 1 line and 80 columns which gets imported first. All subsequent workbooks will have <80 columns.
I did this so that the Table is created with enough columns to accommodate all subsequent files that are imported.
Any help appreciated.
Last edited by a moderator: