VBA - for each unique value in column create a new worksheet

kthevbaamateur

New Member
Joined
Jun 21, 2018
Messages
2
Hi

I want to preface this by saying I am a complete vba amateur.

I am trying to make a code that makes a new worksheet for every unique value in column Z. There are a few duplicates. This is what I have so far:

Sub CreateSheets()
'Creates a sheet for each unique Sector name
Dim c As Range, sh As Worksheet
Application.ScreenUpdating = False
With Worksheets("Sheet 2").Activate
For Each c In .Range(.Range("Z2"), .Range("Z" & .Rows.Count).End(xlUp))
Set sh = Sheets.Add(After:=Sheets(Sheets.Count))
sh.Name = c.Value
Set sh = Nothing
Next
End With
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this.
Code:
Sub CreateSectorSheets()
Dim rng As Range
Dim cl As Range
Dim dic As Object
Dim ky As Variant

    Set dic = CreateObject("Scripting.Dictionary")
    
    With Sheets("Sheet2")
        Set rng = .Range(.Range("Z2"), .Range("Z" & .Rows.Count).End(xlUp))
    End With
    
    For Each cl In rng
        If Not dic.exists(cl.Value) Then
            dic.Add cl.Value, cl.Value
        End If
    Next cl
    
    For Each ky In dic.keys
          Sheets.Add(After:=Sheets(Sheets.Count)).Name = dic(ky)
    Next ky
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

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