VBA to create variable sheets and names

hblbs

Board Regular
Joined
Mar 18, 2009
Messages
184
Hi all, I am wondering if anyone has a VBA code to create sheets and copy rows of information over to the corresponding sheet. After Ma Base there is a blank column and the subject grades are listed, for each subject I need to create a sheet including all columns up to Ma Base and the subject and after the next break the subject class group. So in the example below there would be 3 sheets created (AR, BL and CP). Within each sheet the would be 10 records copied over for AR and CP and 3 for BL based on the results(4C).

StuIdStudentClass gpSexGTSub(s)Sd(s)ELligibilityLACDate of entry1st YrAtt %En BaseMa BaseAR Aut2: Year_9LBL Aut2: Year_9LCP Aut2: Year_9LARBLCP
1A9MSM04/09/2012974b4c5c4b9xAX49xCs4
2B9ISMY04/09/2012924b4a4a5c9yAX29yCs2
3C9SMMY04/09/2012884b4a4a4c5c9xAX19xBX9xCs1
4D9ISFEL30/04/20137993b3b5c5b3a9yAX49yBX9yCs4
5E9qNFGGg,It,Ma,Re04/09/2012975a5a5b5a9yAX39yCs3
6F9qNMKMLD,SLCNY04/09/2012953b4c5c4c9yAX59yCs5
7G9RuMY04/09/2012935c4b4a5b9xAX29xCs2
8H9KEM04/09/2012944a4b4a5c4a9yAX29yBX9yCs2
9I9ISMY04/09/20124984b4b5c4b9yAX59yCs5
10J9qzM04/09/2013984b4c4b4a9xAX29xCs2

<tbody>
</tbody>

I could of course copy the sheets 3 times and the filter and delete the necessary columns and rows, however I have to to this with over 20 subjects of various class sizes and it would be useful if this could be automated as it is needs to be done fairly regularly for 5 year groups and 20 subjects(5*20=100 sheets to create and amend).

Thanks in advance.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I have tried amending the following which creates a list based on rows in a sheet, but was wondering if anyone knows how to amend so that it can be used to work across columns like my scenario above.

Code:
Sub createShts()
Dim rData As Range
    Dim rCl As Range
    Dim sNm As String
    
    Set ws = Sheet1
    'extract a list of unique names
    'first clear existing list
    With ws
        Set rData = .Range(.Cells(1, 1), .Cells(Rows.Count, 3).End(xlUp))
        .Columns(256).Clear
        .Range(Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Cells(1, 256), Unique:=True
    
        For Each rCl In .Range(.Cells(1, 256), .Cells(.Rows.Count, 256).End(xlUp))
            sNm = rCl.Text
            'add new sheet (only if required-NB uses UDF)
            If WksExists(sNm) Then
                'so clear contents
                Sheets(sNm).Cells.Clear
            Else
                'new sheet required
                Set wsNew = Sheets.Add
                wsNew.Move After:=Worksheets(Worksheets.Count) 'move to end
                wsNew.Name = sNm
            End If
            'AutoFilter & copy to relevant sheet
            rData.AutoFilter Field:=1, Criteria1:=sNm
            rData.Copy Destination:=Worksheets(sNm).Cells(1, 1)
        Next rCl
        End With
        ws.Columns(256).ClearContents 'remove temporary list
        rData.AutoFilter 'switch off AutoFilter
    End Sub
Function WksExists(wksName As String) As Boolean
    On Error Resume Next
    WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
End Function
 
Upvote 0

Forum statistics

Threads
1,216,104
Messages
6,128,856
Members
449,472
Latest member
ebc9

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top