VBA to create worksheets

chaddres

Board Regular
Joined
Jun 14, 2014
Messages
143
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet with data in columns A to J. I need a macro that I can paste the data into that takes the data and puts all data with the same Home Cost Number (column E) on a separate tab. There are some blank cost numbers at the top of the list that need to go on one tab, then the next cost number and associated data on a second tab, and so on. I have an undefined number of cost numbers and an undefined number of rows of data.

I am at a 100% loss on how/where to even begin. Even the list of cost numbers can change. I can rearrange the columns of my report output, if that helps. Anything that can be done is truly appreciated. I would even pay for help if someone could direct me to a resource.

Thank you in advance...
Chad
 

Attachments

  • Screenshot 2022-11-01 at 5.35.13 PM.png
    Screenshot 2022-11-01 at 5.35.13 PM.png
    192.3 KB · Views: 20

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hello Chaddres,

I'm assuming that you'd like a separate sheet created for each individual Home Cost Number code. However, in relation to the blanks, what is the intention here? To automatically create new worksheets there needs to be a value in the blank cells otherwise nothing will happen for these blank cells. Do you intend to give them a temporary value or name?

Cheerio,
vcoolio.
 
Upvote 0
Vcoolio:

We could fill them in with "0", right? That would work for me.

Chad
 
Upvote 0
Hello Chad,

The next step is for you to upload a sample of your workbook. You can use this site's XL2BB tool (or upload your sample to a file sharing site such as Drop Box or WeTransfer then post the link to your file back here). Make sure that the sample is an exact replica of your actual workbook and if your data is sensitive then please use dummy data. Doing this will enable us to test any code in your supplied sample.

Cheerio,
vcoolio.
 
Upvote 0
Allocations.xlsx
ABCDEFGHI
1Position IDPayroll NameLocation DescriptionBusiness Unit DescriptionHome Cost Number CodeIs Home Cost NumberCost NumberPercentageAllocations Status
2UZK045426Perez, AnaPrince of PeaceOperations2005-11002-FS166-301No2005-11007-FS195-30117.00Active
3UZK045426Perez, AnaPrince of PeaceOperations2005-11002-FS166-301Yes2005-11002-FS166-30183.00Active
4UZK045472Fechter, TimothyCampOperations1400-12001-FS358-301Yes1400-12001-FS358-301100.00Active
5UZK045694Knutson, David LMardak AdministrationGovernment Affairs1005-10001-FS358-301Yes1005-10001-FS358-301100.00Active
6UZK045703Wills, JamarNorth DivisionOperations1725-11002-FS124-301Yes1725-11002-FS124-301100.00Active
7UZK045831Roberts, JoelCampOperations1400-12001-FS358-301Yes1400-12001-FS358-301100.00Active
8UZK046100Ragland, Veronica SMary RyanOperations1503-11001-FS358-301No1503-17007-FS175-30110.00Active
9UZK046100Ragland, Veronica SMary RyanOperations1503-11001-FS358-301No1503-17008-FS183-30125.00Active
10UZK046100Ragland, Veronica SMary RyanOperations1503-11001-FS358-301No1503-17014-FS204-30135.00Active
11UZK046100Ragland, Veronica SMary RyanOperations1503-11001-FS358-301No1701-11002-FS100-3015.00Active
12UZK046100Ragland, Veronica SMary RyanOperations1503-11001-FS358-301No1708-11002-FS107-3015.00Active
13UZK046100Ragland, Veronica SMary RyanOperations1503-11001-FS358-301No1713-11002-FS112-3015.00Active
14UZK046100Ragland, Veronica SMary RyanOperations1503-11001-FS358-301No1716-11002-FS115-3015.00Active
1
 
Upvote 0
Allocations.xlsx
ABCDEFGHI
1Position IDPayroll NameLocation DescriptionBusiness Unit DescriptionHome Cost Number CodeIs Home Cost NumberCost NumberPercentageAllocations Status
2UZK915611Fine, Alison Elisabeth Yes 100.00Active
3UZK917651Bracey, Carol JeanClarke Yes 100.00Active
4UZK917998Hearn, Reginald MNorth DivisionOperations Yes 100.00Active
5UZK910822Mcclain-Kelly, Dana JClarkePrograms1001-17003-FS358-301No3106-17003-FS174-30160.00Active
6UZK910822Mcclain-Kelly, Dana JClarkePrograms1001-17003-FS358-301Yes1001-17003-FS358-30140.00Active
7UZK913200Lewis, Deonte MMardak AdministrationCommunity Affairs1002-10001-FS358-301Yes1002-10001-FS358-301100.00Active
8UZK916569Crain-Castle, Emily KayGrantosaOperations1002-10001-FS358-301Yes1002-10001-FS358-301100.00Active
9UZK918012Cazares, CarenMitchellCommunity Affairs1002-10001-FS457-311Yes1002-10001-FS457-311100.00Active
10UZK913718Aronson, Marisa EMardak AdministrationFinance1004-10001-FS358-301Yes1004-10001-FS358-301100.00Active
11UZK917965Kwiatkowski, MartaMardak AdministrationFinance1004-10001-FS358-301Yes1004-10001-FS358-301100.00Active
12UZK917983Holentunder, Tyler AMardak AdministrationFinance1004-10001-FS358-301Yes1004-10001-FS358-301100.00Active
13UZK917993Saepharn, Kao FMardak AdministrationFinance1004-10001-FS358-301Yes1004-10001-FS358-301100.00Active
14UZK918053Konieczki, Jordan MMardak AdministrationFinance1004-10001-FS358-301Yes1004-10001-FS358-301100.00Active
15UZK918078Krause, Abigail SMardak AdministrationFinance1004-10001-FS358-301No1301-10001-FS358-30150.00Active
16UZK918078Krause, Abigail SMardak AdministrationFinance1004-10001-FS358-301Yes1004-10001-FS358-30150.00Active
17UZK918105Walls, Shawntay MMardak AdministrationFinance1004-10001-FS358-301Yes1004-10001-FS358-301100.00Active
18UZK045694Knutson, David LMardak AdministrationGovernment Affairs1005-10001-FS358-301Yes1005-10001-FS358-301100.00Active
19UZK912933Schmidlkofer, Joseph FranklinMardak AdministrationGovernment Affairs1005-10001-FS358-301No1400-12001-FS178-30120.00Active
1
 
Upvote 0
Hello Chad,
Thanks for that.

See if the following code does the task for you:-


VBA Code:
Sub Test()

              Dim i As Integer, lr As Long
              Dim sh As Worksheet, ws As Worksheet, ar As Variant
        
              Application.ScreenUpdating = False
              
              Set sh = Sheet1
              lr = sh.Range("A" & Rows.Count).End(xlUp).Row
              sh.Range("E1:E" & lr).AdvancedFilter 2, , sh.[X1], 1  'Unique values moved temporarily to Column X.
              sh.Range("X2", sh.Range("X" & sh.Rows.Count).End(xlUp)).Sort [X2], 1 'Unique values sorted.
              ar = sh.Range("X2", sh.Range("X" & sh.Rows.Count).End(xlUp))
              
              For i = 1 To UBound(ar)
                    If Not Evaluate("ISREF('" & CStr(ar(i, 1)) & "'!A1)") Then
                    Sheets.Add(After:=Sheets(Worksheets.Count)).Name = ar(i, 1)
              End If
                    
              Set ws = Sheets(CStr(ar(i, 1)))
                    
              With sh.[A1].CurrentRegion
                    .AutoFilter 5, ar(i, 1)
                    .Copy ws.[A1]
                    .AutoFilter
              End With
                    ws.Columns.AutoFit
              Next i

             sh.Columns("X").Clear
             Application.Goto Sheet1.[A1]
             Application.ScreenUpdating = True

End Sub

The code temporarily uses Column X as a helper column to extract all unique values from Column E then sorts them and moves them into an array.
It then creates new individual sheets for each Home Cost Number Code without duplication and copies all data to it's individual sheet, including the headings.
Before you run the code, manually enter a text value into the blank cells in Column E, e.g. "A000". This will enable a new sheet to be created for the 'non-code' entries in Column E.
This can be done within the code, but first just see if all this works for you.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Solution
You're welcome Chad. I'm glad to have been able to assist.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

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