View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
[email protected] santudam@gmail.com is offline
external usenet poster
 
Posts: 1
Default 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