VBA - Create sheets based on unique values in a column

ORoxo

Board Regular
Joined
Oct 30, 2016
Messages
149
Hey, guys,
Let's say I have vendors' names on column A and I wish to create a sheet for each vendor. However, the macro must also be able to check if there's a sheet with that name already.

Right now, I'm struggling to name the new worksheet:

Code:
Sub test()
Dim r As Integer, lr As Integer, ws As Worksheet


Application.ScreenUpdating = False


lr = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row


For r = 2 To lr
  
    Worksheets.Add
    ActiveSheet.Name = Sheet1.Cells(r, 1)
    
Next


Application.ScreenUpdating = True


End Sub

I wouldn't like you to provide me a full macro. Instead, I would appreciate hits so I can get there by myself.

Kind regards,
ORoxo

EDIT: Ok, I overcame the problem with the name of the sheets. Now I need an If structure to ignore if there's already a sheet with that word.
 
Last edited:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
What do you want to do if a sheet name already exists? Delete it and create a new sheet with that name or skip to the next name?
 
Upvote 0
At this stage, I would just like it to skip to the next name, Joe.
 
Last edited:
Upvote 0
Then you can try using the function below in your code.
Code:
Sub test()
Dim r As Integer, lr As Integer, ws As Worksheet
Application.ScreenUpdating = False
lr = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To lr
    If Not SheetExists(Sheet1.Cells(r, 1)) Then
        Worksheets.Add
        ActiveSheet.Name = Sheet1.Cells(r, 1)
    End If
Next
Application.ScreenUpdating = True
End Sub
Function SheetExists(shName As String) As Boolean
SheetExists = False
    For Each sh In .Sheets
        If sh.Name = shName Then
            SheetExists = True
            Exit For
        End If
     Next sh
End Function
 
Upvote 0

Forum statistics

Threads
1,214,544
Messages
6,120,126
Members
448,947
Latest member
test111

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