Split Excel Data into Separate Tabs based on Building Names

ShwetaD

New Member
Joined
Nov 18, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi, I have data in excel that needs to be split in separate tabs based on the building name. can someone please advise how can I create a macro or vb code for this?

AssetIDBuildingLevel1Level2Level3Level4AssetLabelAssetLocationMoreSpecificLocation
1​
Bldg1D ServicesD30 HVACD3040 Heat HVAC Distribution SystemsD3041 Air Distribution SystemsAir Curtain per 1,000 CFMInterior
2​
Bldg2D ServicesD20 PlumbingD2090 Other Plumbing SystemsD2094 Pool Piping and EquipmentPool FiltersMechanical RoomSpa
3​
Bldg3D ServicesD20 PlumbingD2010 Plumbing FixturesD2014 SinksSingle Compartment Stainless SinkRestroomsAmpitheater Concessions
4​
Bldg4D ServicesD20 PlumbingD2010 Plumbing FixturesD2014 SinksSingle Compartment Stainless SinkMechanical RoomPool
5​
Bldg4D ServicesD20 PlumbingD2010 Plumbing FixturesD2014 SinksSingle Compartment Stainless SinkInteriorGrill House
6​
Bldg4D ServicesD20 PlumbingD2010 Plumbing FixturesD2014 SinksSingle Compartment Stainless SinkInteriorRival House
7​
Bldg1D ServicesD20 PlumbingD2010 Plumbing FixturesD2014 SinksSingle Compartment Stainless SinkInterior
8​
Bldg1D ServicesD20 PlumbingD2010 Plumbing FixturesD2014 SinksSingle Compartment Stainless SinkInteriorPrep Storeage
9​
Bldg1D ServicesD20 PlumbingD2010 Plumbing FixturesD2014 SinksSingle Compartment Stainless SinkInterior
10​
Bldg1D ServicesD20 PlumbingD2010 Plumbing FixturesD2014 SinksSink _ Integral with countertopInteriorBanquet Kitchen
11​
Bldg1D ServicesD20 PlumbingD2010 Plumbing FixturesD2014 SinksSink _ Integral with countertopInterior
12​
Bldg2D ServicesD20 PlumbingD2010 Plumbing FixturesD2014 SinksSink _ Integral with countertopInteriorBallroom
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
In the macro, change "Sheet1" to the name of your sheet.
I'm assuming the headers are in row 1 and the data starts in row 2.

Try this:

VBA Code:
Sub create_worksheets()
  Dim c As Range, sh As Worksheet, ky As Variant
  Set sh = Sheets("Sheet1")
  With CreateObject("scripting.dictionary")
    For Each c In sh.Range("B2", sh.Range("B" & Rows.Count).End(xlUp))
      If c.Value <> "" Then .Item(c.Value) = Empty
    Next c
    For Each ky In .Keys
      sh.Range("A1").AutoFilter Columns("B").Column, ky
      Sheets.Add(, Sheets(Sheets.Count)).Name = ky
      sh.AutoFilter.Range.EntireRow.Copy Range("A1")
    Next ky
  End With
  sh.Select
  sh.ShowAllData
End Sub
 
Upvote 0
Hi,

The above code gives an error on the line sh.ShowAllData

Could someone please advise if the above data is to be split into different excel files based on building names then what the code would be?

Thank you
 
Upvote 0
The above code gives an error on the line sh.ShowAllData
Delete that line. But that's not the problem, really the problem is:
Your example is not very complete.
In which column are the buildings?
In which row does your data start?
The macro assumes that the buildings are in column B and start at row 2. It also assumes that the data is on the sheet named "Sheet1"
If not, you should give a more complete example, use the XL2BB tool.
I guess we'll hear from you in 18 days.

f the above data is to be split into different excel files
Your request are tabs not excel files.
 
Upvote 0
Hey,
My example was only to provide a sample. Your assumptions are correct i.e building name is in column B, the headers are in row 1 and the data starts from row 2. Sheet name is Sheet1.

My original question was to split the data in different tabs and thanks for providing the answer.

However, in this scenario what is happening is the macro creates multiple tabs using the above code and then I have to manually move the tab into a new file and save it separately. Sometimes I have 70+ tabs and it is a long manual exercise. So I was wondering if someone could help in writing a code where the data splits into separate excel files rather than into tabs.

Hope that makes sense now.

:)
 
Upvote 0
My original question was to split the data in different tabs and thanks for providing the answer.
You should start by commenting that the response resolved your first request and will not return after 18 days claiming that the macro doesn't work.

Then kindly request that changes be made to the macro.

Try this:
VBA Code:
Sub create_workbooks()
  Dim c As Range, sh As Worksheet, ky As Variant
  Dim wb2 As Workbook
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  Set sh = Sheets("Sheet1")
  With CreateObject("scripting.dictionary")
    For Each c In sh.Range("B2", sh.Range("B" & Rows.Count).End(xlUp))
      If c.Value <> "" Then .Item(c.Value) = Empty
    Next c
    For Each ky In .Keys
      sh.Range("A1").AutoFilter Columns("B").Column, ky
      Set wb2 = Workbooks.Add
      sh.AutoFilter.Range.EntireRow.Copy wb2.Sheets(1).Range("A1")
      wb2.SaveAs ThisWorkbook.Path & "\" & ky & ".xlsx"
      wb2.Close False
    Next ky
  End With
  sh.Select
  sh.ShowAllData
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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