VBA - working with data videos | Excel VBA Part 58.31 - SQL for Excel Files - Split a Table into Separate Sheets

Posted by Andrew Gould on 25 July 2021


This video shows you how to split a single Eacel table into multiple worksheets using the values in a column of the table. You'll learn how to create a recordset and populate it with unique values from a column and how to create a SELECT INTO command to create new worksheets. You'll see how to create new worksheets in the same workbook as the source data, in a single different workbook and how to create a separate workbook for each value in the column.

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
Split Table into Separate Sheets.xlsm Excel workbook with macros The code
Movies.xlsx Excel workbook The source data

Click to download a zipped copy of the above files.

There are no exercises for this video.

Making a  video bigger

You can increase the size of your video to make it fill the screen like this:

View full screen

Play your video (the icons shown won't appear until you do), then click on the full screen icon which appears as shown at its bottom right-hand corner.

 

When you've finished viewing a video in full screen mode, just press the Esc key to return to normal view.

Improving the quality of a video

To improve the quality of a video, first click on the Settings icon:

Settings icon

Make sure you're playing your video so that the icons shown appear, then click on this gear icon at the bottom right-hand corner.

 

Choose to change the video quality:

Video quality

Click on Quality as shown to bring up the submenu.

 

The higher the number you choose, the better will be your video quality (but the slower the connection speed):

Connection speed

Don't choose the HD option unless you have a fast enough connection speed 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.

This page has 2 threads Add post
22 Oct 21 at 14:15

I am unable to create files in Path, I am getting runtime error, please see the below code

 

Sub SplitTable()

    Dim MovieFilePath As String
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim cmd As ADODB.Command
    
    
    
    MovieFilePath = ThisWorkbook.Path & "\Movies.xlsx"
    
    Set cn = New ADODB.Connection
    
    cn.ConnectionString = _
        "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & MovieFilePath & ";" & _
        "Extended Properties='Excel 12.0 Xml;HDR=YES';"
    
    cn.Open
    Set rs = New ADODB.Recordset
    rs.ActiveConnection = cn
    rs.Source = " Select DISTINCT [Country]  From  [Film$]   "
    rs.Open
    
    Set cmd = New ADODB.Command
    cmd.ActiveConnection = cn
    cmd.CommandType = adCmdText
    
    Do Until rs.EOF
'           Debug.Print rs.Fields("Country").Value
           cmd.CommandText = _
                                           " Select *  " & _
                                           " Into [" & rs.Fields("Country").Value & "]" & _
                                           " In ' " & ThisWorkbook.Path & " \ Countries Data \  " & rs.Fields("Country").Value & " .xlsx'   'Excel 12.0 Xml; ' " & _
                                           " From [Film$] " & _
                                           " Where [Country]=  ' " & rs.Fields("Country").Value & " ' "
                                           
'     Debug.Print cmd.CommandText
    cmd.Execute
    rs.MoveNext
    Loop
    rs.Close
    cn.Close
    
End Sub

22 Oct 21 at 14:36

I would guess that you've included too many additional spaces in this part of your code

ThisWorkbook.Path & " \ Countries Data \  " & rs.Fields("Country").Value & " .xlsx'   'Excel 12.0 Xml; ' "

Unless your folder names and file names actually have those spaces I would remove them so that it looks more like this

ThisWorkbook.Path & "\Countries Data\" & rs.Fields("Country").Value & ".xlsx' 'Excel 12.0 Xml;'"

I hope it helps!

29 Oct 21 at 09:03

I have received completed code module.

The support you have given is very, very good, I can not say in my own words

 

Love You so much..........

 

26 Oct 21 at 12:12

I remove extra specs Inside the code, after that I am getting runtime error below :

                     RUN TIME Error -2147217900(80040e14)

                    invalid bracketing of name ' i;\fileneeded-31\All\Australia.xlsx'

i have mentioned code below:

    cmd.CommandText = _
                                           " Select * " & _
                                           " Into [" & rs.Fields("Country").Value & "]" & _
                                           " In ' " & ThisWorkbook.Path & "\All\" & rs.Fields("Country").Value & ".xlsx' 'Excel 12.0 Xml;'" & _
                                           " From [Film$] " & _
                                           " Where [Country]=  ' " & rs.Fields("Country").Value & " ' "

Andrew G  
29 Oct 21 at 07:40

Hi there! If you perform a Google search for "clear cached files google chrome" it will explain how to do this. You can then download the file containing the CompletedCode module from this page. I hope it helps!

Chandra  
29 Oct 21 at 07:27

I am unable to Understand, please try to explain, I have two browsers one is Google Chrome and another one Microsoft Edge.

Andrew G  
27 Oct 21 at 14:21

Try clearing your browser's cached files.

Chandra  
27 Oct 21 at 11:30

Again I have downloaded the file , in that file there is no code,

in that file this code is there

 

Sub SplitTable()

    Dim MovieFilePath As String
    Dim cn As ADODB.Connection
    
    MovieFilePath = ThisWorkbook.Path & "\Movies.xlsx"
    
    Set cn = New ADODB.Connection
    
    cn.ConnectionString = _
        "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & MovieFilePath & ";" & _
        "Extended Properties='Excel 12.0 Xml;HDR=YES';"
    
    cn.Open
    
    

    cn.Close
    
End Sub

Andrew G  
27 Oct 21 at 07:57

The example file now contains a module called "Completed code". I hope it helps.

Chandra  
27 Oct 21 at 06:57

Hi

I downloaded the file, in that file there is no code, if You possible please share me the file

Andrew G  
26 Oct 21 at 16:04

Hi, I would recommend downloading the example file from this page and comparing your code very carefully to the working example in that file.

20 Aug 21 at 16:26

Hello Andrew:

 

I was wondering if you will be posting more videos on SQL for Excel. The information has been very useful to me and has saved me a lot of programming that otherwise I would have to do with VBA. If not, do you have any links to websites that specifically deal with this version of SQL for Excel? I note that a lot of features of SQL are not applicable to that version (e.g. unable to do full joins).

22 Aug 21 at 07:45

Hi, I'm happy to hear that you've found the SQL for Excel series of videos useful!

There are a few extra topics on my list to cover, but these are things that are more to do with the ADO side of things than with SQL. I think that the existing 31 videos already cover the broad strokes of what this version of SQL is capable of but for further reading I'd recommend searching for "Access SQL".

I hope that helps and thanks for watching!