ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Adding a sheet with a specific name (https://www.excelbanter.com/excel-programming/366827-adding-sheet-specific-name.html)

Kyle

Adding a sheet with a specific name
 
I need a macro that will take the name in cell A2 and see if there is a sheet
that already has that name. If there is not a sheet then I need it to create
a new sheet with the name as the name of the sheet. Is this possible?

my macro right now just searches for a sheet with the same name.

If Cells(1, 2).Value < "" Then
name = Cells(1, 2).Value
Sheets(name).Activate

how can I add the creating new sheet part?

Thanks,
Kyle

Charlie

Adding a sheet with a specific name
 
Pass the value to this sub:

Public Sub CreateSheet(SheetName As String)
'
' if the sheet already exists select it
'
On Error GoTo CreateNewSheet
Sheets(SheetName).Select
Exit Sub
'
' if not create it
'
CreateNewSheet:
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = SheetName
'
End Sub


"kyle" wrote:

I need a macro that will take the name in cell A2 and see if there is a sheet
that already has that name. If there is not a sheet then I need it to create
a new sheet with the name as the name of the sheet. Is this possible?

my macro right now just searches for a sheet with the same name.

If Cells(1, 2).Value < "" Then
name = Cells(1, 2).Value
Sheets(name).Activate

how can I add the creating new sheet part?

Thanks,
Kyle


[email protected]

Adding a sheet with a specific name
 

Charlie wrote:
Pass the value to this sub:

Public Sub CreateSheet(SheetName As String)
'
' if the sheet already exists select it
'
On Error GoTo CreateNewSheet
Sheets(SheetName).Select
Exit Sub
'
' if not create it
'
CreateNewSheet:
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = SheetName
'
End Sub


"kyle" wrote:

I need a macro that will take the name in cell A2 and see if there is a sheet
that already has that name. If there is not a sheet then I need it to create
a new sheet with the name as the name of the sheet. Is this possible?

my macro right now just searches for a sheet with the same name.

If Cells(1, 2).Value < "" Then
name = Cells(1, 2).Value
Sheets(name).Activate

how can I add the creating new sheet part?

Thanks,
Kyle


Hi

Try this code

Sub Create_Sheets()

Dim Name As String
Dim Sheet_Count As Integer
Dim Wk_Sheet As Worksheet
Dim Check As Boolean

Name = ThisWorkbook.Sheets("Sheet1").Range("A2").Value
Sheet_Count = Worksheets.Count

For Each Wk_Sheet In ThisWorkbook.Sheets
If Wk_Sheet.Name = Name Then
Check = True
End If
Next

'if there is no sheet found
If Check = False Then
'Add the sheet at last
ThisWorkbook.Sheets.Add after:=Worksheets(Worksheets.Count)
ThisWorkbook.Sheets(Sheet_Count + 1).Name =
ThisWorkbook.Sheets(1).Range("a2").Value
End If

End Sub



All times are GMT +1. The time now is 01:37 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com