Split Data based on Value in Row

MR_786

New Member
Joined
Feb 18, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi,

This is my post on this forum, I have data in the following format:

Data.png


1. I want to split the data from the master worksheet into multiple worksheets based on the value in row 6 (move entire columns to new worksheet).
2. I also want the first column to be recurring on every worksheet.
3. I would like for each new worksheet created to use the value in row 6 that it was split by as the worksheet name.

Any help will be much appreciated!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi and welcome to MrExcel.

Try the following macro, fit your master sheet name on this line:
Set sh1 = Sheets("Master") 'Fit to your sheet name

VBA Code:
Sub SplitData()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim lr As Long, lc As Long
  Dim c As Range
  Dim ky As Variant
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
 
  Set sh1 = Sheets("Master")  'Fit to your sheet name
  lr = sh1.Range("A" & Rows.Count).End(3).Row
  lc = sh1.Cells.Find("*", , , , xlByColumns, xlPrevious).Column
  Set sh2 = Sheets.Add
  sh1.Range("A1", sh1.Cells(lr, lc)).Copy
  sh2.Range("A1").PasteSpecial xlPasteValues, , , True
 
  With CreateObject("scripting.dictionary")
    For Each c In sh2.Range("F2", sh2.Range("F" & Rows.Count).End(xlUp))
      If c.Value <> "" Then .Item(c.Value) = Empty
    Next c
    For Each ky In .Keys
      sh2.Range("A1", sh2.Cells(lc, lr)).AutoFilter 6, ky
      Sheets.Add(, Sheets(Sheets.Count)).Name = ky
      sh2.AutoFilter.Range.Copy
      Range("A1").PasteSpecial xlPasteValues, , , True
    Next ky
  End With
  sh1.Select
  sh2.Delete
End Sub
 
Upvote 0
Hi and welcome to MrExcel.

Try the following macro, fit your master sheet name on this line:
Set sh1 = Sheets("Master") 'Fit to your sheet name

VBA Code:
Sub SplitData()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim lr As Long, lc As Long
  Dim c As Range
  Dim ky As Variant
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
 
  Set sh1 = Sheets("Master")  'Fit to your sheet name
  lr = sh1.Range("A" & Rows.Count).End(3).Row
  lc = sh1.Cells.Find("*", , , , xlByColumns, xlPrevious).Column
  Set sh2 = Sheets.Add
  sh1.Range("A1", sh1.Cells(lr, lc)).Copy
  sh2.Range("A1").PasteSpecial xlPasteValues, , , True
 
  With CreateObject("scripting.dictionary")
    For Each c In sh2.Range("F2", sh2.Range("F" & Rows.Count).End(xlUp))
      If c.Value <> "" Then .Item(c.Value) = Empty
    Next c
    For Each ky In .Keys
      sh2.Range("A1", sh2.Cells(lc, lr)).AutoFilter 6, ky
      Sheets.Add(, Sheets(Sheets.Count)).Name = ky
      sh2.AutoFilter.Range.Copy
      Range("A1").PasteSpecial xlPasteValues, , , True
    Next ky
  End With
  sh1.Select
  sh2.Delete
End Sub

Hi, many thanks for your response, the code works perfectly!
If it isn't too much to ask can you please annotate each line of your code with some descriptive text so I can enhance my knowledge.
 
Upvote 0
If it isn't too much to ask can you please annotate each line of your code with some descriptive text so I can enhance my knowledge

VBA Code:
Sub SplitData()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim lr As Long, lc As Long
  Dim c As Range
  Dim ky As Variant
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
 
  'Set the sheet to the sh1 variable
  Set sh1 = Sheets("Master")  'Fit to your sheet name
  'last row of column A
  lr = sh1.Range("A" & Rows.Count).End(3).Row
  'last column of the sheet
  lc = sh1.Cells.Find("*", , , , xlByColumns, xlPrevious).Column
  
  'Set the new sheet to the sh2 variable
  Set sh2 = Sheets.Add
  
  'Copy the data from sheet1 to the new TEMPORAL sheet
  sh1.Range("A1", sh1.Cells(lr, lc)).Copy
  sh2.Range("A1").PasteSpecial xlPasteValues, , , True
 
 'Create a dictionary, it's like an index of unique data
  With CreateObject("scripting.dictionary")
  
    'Stores in the dictionary the unique data of column F
    For Each c In sh2.Range("F2", sh2.Range("F" & Rows.Count).End(xlUp))
      If c.Value <> "" Then .Item(c.Value) = Empty
    Next c
    
    'For each key stored in the dictionary
    For Each ky In .Keys
      'Filter the data in column 6 (F)
      sh2.Range("A1", sh2.Cells(lc, lr)).AutoFilter 6, ky
      'Create the new sheet and name it the data of the key
      Sheets.Add(, Sheets(Sheets.Count)).Name = ky
      'Copy the data and paste it on the new sheet
      sh2.AutoFilter.Range.Copy
      Range("A1").PasteSpecial xlPasteValues, , , True
    Next ky
  End With
  sh1.Select
  'delete TEMPORAL sheet
  sh2.Delete
End Sub
 
Upvote 0
VBA Code:
Sub SplitData()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim lr As Long, lc As Long
  Dim c As Range
  Dim ky As Variant
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
 
  'Set the sheet to the sh1 variable
  Set sh1 = Sheets("Master")  'Fit to your sheet name
  'last row of column A
  lr = sh1.Range("A" & Rows.Count).End(3).Row
  'last column of the sheet
  lc = sh1.Cells.Find("*", , , , xlByColumns, xlPrevious).Column
 
  'Set the new sheet to the sh2 variable
  Set sh2 = Sheets.Add
 
  'Copy the data from sheet1 to the new TEMPORAL sheet
  sh1.Range("A1", sh1.Cells(lr, lc)).Copy
  sh2.Range("A1").PasteSpecial xlPasteValues, , , True
 
 'Create a dictionary, it's like an index of unique data
  With CreateObject("scripting.dictionary")
 
    'Stores in the dictionary the unique data of column F
    For Each c In sh2.Range("F2", sh2.Range("F" & Rows.Count).End(xlUp))
      If c.Value <> "" Then .Item(c.Value) = Empty
    Next c
   
    'For each key stored in the dictionary
    For Each ky In .Keys
      'Filter the data in column 6 (F)
      sh2.Range("A1", sh2.Cells(lc, lr)).AutoFilter 6, ky
      'Create the new sheet and name it the data of the key
      Sheets.Add(, Sheets(Sheets.Count)).Name = ky
      'Copy the data and paste it on the new sheet
      sh2.AutoFilter.Range.Copy
      Range("A1").PasteSpecial xlPasteValues, , , True
    Next ky
  End With
  sh1.Select
  'delete TEMPORAL sheet
  sh2.Delete
End Sub

Thank you! I really appreciate it.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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