Split selected column into sheets Macro

doriannjeshi

Active Member
Joined
Apr 5, 2015
Messages
301
Office Version
  1. 365
Platform
  1. Windows
Hi,
Is it possible to split the content of a selected column into seperate new sheets named after them. But the column is not fixed , but the selected one. In this case B

Book2
ABCDEF
1ABCDEF
2a-bori3-31
3111111
4131111
5111111
613111
7a-bori1111
8131111
9111-61
101311
11a-bori11111
1212111
13121111
1412111
sheet1


1730986651910.png
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
In your example, if you select column D, then you must have 2 sheets: "1" and "-6" ?

Should the macro always create the sheets? That is, if sheet "1" already exists, should it delete the content of sheet "1" and put in the new data?
 
Upvote 0
In your example, if you select column D, then you must have 2 sheets: "1" and "-6" ?

Should the macro always create the sheets? That is, if sheet "1" already exists, should it delete the content of sheet "1" and put in the new data?
Hi Dante,
Yes the the macro should create the sheets, if possible to delete the content of the sheet if exists first and put the new data would be a nice option
 
Upvote 0
Try this:

VBA Code:
Sub split_column()
  Dim sh1 As Worksheet, sh As Worksheet
  Dim c As Range
  Dim col As Long, lr As Long, i As Long
  Dim dic As Object
  Dim ky As Variant
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  Set sh1 = Sheets("Sheet1")
  Set dic = CreateObject("Scripting.Dictionary")
  
  col = ActiveCell.Column
  If sh1.AutoFilterMode Then sh1.AutoFilterMode = False
  lr = sh1.Cells(Rows.Count, col).End(3).Row
  
  For Each c In sh1.Range(sh1.Cells(2, col), sh1.Cells(Rows.Count, col).End(3))
    If c.Value <> "" Then dic(c.Value) = Empty
  Next
  
  For Each ky In dic.Keys
    On Error Resume Next: Sheets(Trim(Str(ky))).Delete: On Error GoTo 0
    sh1.Range("A1").AutoFilter col, ky
    Sheets.Add(, Sheets(Sheets.Count)).Name = ky
    sh1.AutoFilter.Range.EntireRow.Copy Range("A1")
  Next

  sh1.Select
  sh1.AutoFilterMode = False
  Application.ScreenUpdating = False
End Sub

🤗
 
Upvote 0
Try this:

VBA Code:
Sub split_column()
  Dim sh1 As Worksheet, sh As Worksheet
  Dim c As Range
  Dim col As Long, lr As Long, i As Long
  Dim dic As Object
  Dim ky As Variant
  Dim sName As String
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  Set sh1 = Sheets("Sheet1")
  Set dic = CreateObject("Scripting.Dictionary")
  
  col = ActiveCell.Column
  If sh1.AutoFilterMode Then sh1.AutoFilterMode = False
  lr = sh1.Cells(Rows.Count, col).End(3).Row
  
  For Each c In sh1.Range(sh1.Cells(2, col), sh1.Cells(Rows.Count, col).End(3))
    If c.Value <> "" Then dic(c.Value) = Empty
  Next
  
  For Each ky In dic.Keys
    sName = Trim(Str(ky))
    On Error Resume Next: Sheets(sName).Delete: On Error GoTo 0
    sh1.Range("A1").AutoFilter col, ky
    Sheets.Add(, Sheets(Sheets.Count)).Name = ky
    sh1.AutoFilter.Range.EntireRow.Copy Range("A1")
  Next

  sh1.Select
  sh1.AutoFilterMode = False
  Application.ScreenUpdating = False
End Sub
 
Upvote 0
:unsure:I think I didn't consider that sheets, in addition to being called numbers, can also be called letters.

Try this:
VBA Code:
Sub split_column()
  Dim sh1 As Worksheet, sh As Worksheet
  Dim c As Range
  Dim col As Long, lr As Long, i As Long
  Dim dic As Object
  Dim ky As Variant
  Dim sName As String
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  Set sh1 = Sheets("Sheet1")
  Set dic = CreateObject("Scripting.Dictionary")
  
  col = ActiveCell.Column
  If sh1.AutoFilterMode Then sh1.AutoFilterMode = False
  lr = sh1.Cells(Rows.Count, col).End(3).Row
  
  For Each c In sh1.Range(sh1.Cells(2, col), sh1.Cells(Rows.Count, col).End(3))
    If c.Value <> "" Then dic(c.Text) = Empty
  Next
  
  For Each ky In dic.Keys
    sName = ky
    On Error Resume Next: Sheets(sName).Delete: On Error GoTo 0
    sh1.Range("A1").AutoFilter col, ky
    Sheets.Add(, Sheets(Sheets.Count)).Name = ky
    sh1.AutoFilter.Range.EntireRow.Copy Range("A1")
  Next

  sh1.Select
  sh1.AutoFilterMode = False
  Application.ScreenUpdating = False
End Sub

🧙‍♂️
 
Upvote 0
Solution

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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