Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Posted by Andrew Gould on 14 April 2021
Looping through the collection of worksheets in a workbook is a standard technique in Excel VBA and easy to accomplish when the workbook is open. Did you know that you can do the same thing when the workbook is closed? This video explains how to do this using the ActiveX Data Objects library. You'll learn how to loop through the Excel files in a folder, creating a connection to each file using an ActiveX Data Objects Connection object. You'll learn how to query the schema of each workbook to return a list of the worksheets and loop through this list to read the name of each sheet. You'll also learn how to create a recordset object and populate it with data using an SQL Select statement. As a bonus you'll learn about the SQL Union All statment to select data from multiple tables at the same time and how to add criteria to a query using the Where clause.
See our full range of VBA training resources, or test your knowledge of VBA with one of our VBA skills assessment tests.
This video has the following accompanying files:
File name | Type | Description |
---|---|---|
Loop Through Worksheets in Multiple Closed Workbooks.xlsm | Excel workbook with macros | |
Movies 2011.xlsx | Excel workbook | |
Movies 2012.xlsx | Excel workbook | |
Movies 2013.xlsx | Excel workbook | |
Movies 2014.xlsx | Excel workbook | |
Movies 2015.xlsx | Excel workbook | |
Movies 2016.xlsx | Excel workbook |
Click to download a zipped copy of the above files.
There are no exercises for this video.
You can increase the size of your video to make it fill the screen like this:
Play your video (the icons shown won't appear until you do), then click on the full screen icon which appears as shown above.
When you've finished viewing a video in full screen mode, just press the Esc key to return to normal view.
To improve the quality of a video, first click on the Settings icon:
Make sure yoiu're playing your video so that the icons shown above appear, then click on this gear icon.
Choose to change the video quality:
Click as above to change your video quality.
The higher the number you choose, the better will be your video quality (but the slower the connection speed):
Don't choose the HD option shown unless your connection speed is fast enough to support it!
Is your Wise Owl speaking too slowly (or too quickly)? You can also use the Settings menu above to change your playback speed.
From: | Chandra |
When: | 23 Feb 24 at 07:11 |
Hello wiseowl,
Good Day!
inside folder have multiple files if open particular file have multiple sheets but I want to copy particular sheet data past into designation file using this code ,please help me
Option Explicit
Sub GetAllMovies()
Dim MyFilesPath As String
Dim MovieFileName As String
Dim cn As ADODB.Connection
Dim rsSheets As ADODB.Recordset
Dim i As Integer
Dim SheetList As Variant
Dim SQLString As String
Dim rsData As ADODB.Recordset
Sheet1.Range("A1").CurrentRegion.Offset(1, 0).Clear
MyFilesPath = ThisWorkbook.Path & "\My Files\"
MovieFileName = Dir(MyFilesPath & "*.xlsx")
Set cn = New ADODB.Connection
Set rsData = New ADODB.Recordset
Do Until MovieFileName = ""
'Debug.Print MovieFileName
cn.ConnectionString = _
"Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & MyFilesPath & MovieFileName & ";" & _
"Extended Properties='Excel 12.0 Xml;HDR=YES';"
Set rsSheets = cn.OpenSchema(adSchemaTables)
SheetList = rsSheets.GetRows(Fields:="TABLE_NAME")
rsSheets.Close
Set rsSheets = Nothing
For i = 0 To UBound(SheetList, 2)
SQLString = _
SQLString & " UNION ALL SELECT * FROM [" & SheetList(0, i) = "April" & "]" '& _
'" WHERE [Oscar Nominations] > 0 AND [Genre] = 'Animation' AND [Title] LIKE '%2'"
Next i
SQLString = Mid(SQLString, 12)
'Debug.Print SQLString
rsData.Open SQLString, cn
Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Offset(1, 0).CopyFromRecordset rsData
rsData.Close
SQLString = ""
cn.Close
MovieFileName = Dir
Loop
Sheet1.Range("A1").CurrentRegion.EntireColumn.AutoFit
End Sub
From: | Andrew G |
When: | 23 Feb 24 at 09:02 |
Hi! If you need to get data from a single worksheet in a workbook and you know the name of the worksheet, you can just include that sheet name in your select statement:
SELECT * FROM [YourSheetNameHere]
This video shows the full technique https://www.wiseowl.co.uk/vba-macros/videos/vba-ado-net-recordsets/multiple-workbooks-one-query/
I hope it helps!
From: | Chandra |
When: | 29 Feb 24 at 06:56 |
below code i am using,but it is not going to Then block, exit the next loop after that rsData.open SQLString there am getting an error ,please see i have attached the screen shot.
For i = 0 To UBound(SheetList, 2)
If SheetList(0, i) = wrk_shConfig.Range("T3") Or SheetList(0, i) = wrk_shConfig.Range("T4") Or SheetList(0, i) = wrk_shConfig.Range("T5") Then
SQLString = _
SQLString & " UNION ALL SELECT * FROM [" & SheetList(0, i) & "]"
End If
Next i
SQLString = Mid(SQLString, 12)
'Debug.Print SQLString
rsData.Open SQLString, cn
From: | Andrew G |
When: | 04 Mar 24 at 07:31 |
Hi!
It's difficult to know - there could be several reasons for this. I would start by trying some of the suggestions at this link https://stackoverflow.com/questions/25356832/run-time-error-external-table-is-not-in-the-expected-format
Hope you find the solution!
From: | Chandra |
When: | 02 Mar 24 at 06:13 |
really sorry to distub you! again i am getting below error,please let me
From: | Andrew G |
When: | 01 Mar 24 at 07:45 |
Hi! If you look in the Locals window you can see that sheet names which contain a space are enclosed with single quotes. Your code says:
"QTD Summary$"
It should say:
"'QTD Summary$'"
I hope it helps!
From: | Chandra |
When: | 29 Feb 24 at 16:38 |
please see marked blue color item , that is not going to inside then -block ,please help me on this ??????
From: | Andrew G |
When: | 29 Feb 24 at 08:40 |
The most likely reason is that the names of the worksheets you're testing for aren't in the list of values in the SheetList array. Don't forget that sheet names returned by the adSchemaTables constant have a $ symbol added to the end.
From: | Chandra |
When: | 23 Feb 24 at 10:42 |
Hello WiseOwl,
there is no single sheet , there are multiple sheets as a file , i have to find out according to sheetname not only one file , in folder i have multiple files
is it possible closed workbook query?
From: | Andrew G |
When: | 26 Feb 24 at 07:31 |
Hi! I think the easiest thing to do is introduce an IF statement after the beginning of the loop which processes the worksheet names, i.e. this line:
For i = 0 To UBound(SheetList, 2)
You can test for the different worksheet names, for example:
If SheetList(0, i) = "QTD summary" Or SheetList(0, i) = "Rlsr summary" Or SheetList(0, i) = "Disit summary" Then
I hope it helps!
From: | Chandra |
When: | 23 Feb 24 at 14:45 |
What You explained , that one exactly correct.
But my question here you said multiple workbooks has multiple sheets but same sheet name will be there in two times ex: ( QTD summary and Rlsr summary and Disit summary ) sometimes three sheets available and some times two sheets available and some times single sheet available
how can I fetch those. Sheet names and how can I loop it and how can I copy the data and how can I paste data respective destination file ,if you possible share me the code I will get to understand
I sent above one code Please look into that code. You will get to know I hope.
Please it is emergency request as early as possible support me
From: | Andrew G |
When: | 23 Feb 24 at 11:08 |
I'm sorry, I don't understand what you're trying to do. I understand that there are multiple worksheets in multiple workbooks but you said you wanted data from a particular worksheet. If so, and the name of the worksheet is the same in each workbook, you can just state the name of the worksheet in the select statement.
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.