PDA

View Full Version : Create Sheet if Workbook name contain some text



steve87bg
03-11-2014, 04:28 AM
Hi,

i want to write a macro that will create sheet "Check" if workbook name contain "Mobile" in his name. Can someone help me please :)

Kenneth Hobs
03-11-2014, 06:01 AM
Function Test()
If InStr(ThisWorkbook.Name, "Check") <> 0 Then
If Not WorkSheetExists("Mobile") Then
Worksheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = "Mobile"
End If
End If
End Function

'WorkSheetExists in a workbook:
Function WorkSheetExists(sWorkSheet As String, Optional sWorkbook As String = "") As Boolean
Dim ws As Worksheet, wb As Workbook
On Error GoTo notExists
If sWorkbook = "" Then
Set wb = ActiveWorkbook
Else
Set wb = Workbooks(sWorkbook)
End If
Set ws = wb.Worksheets(sWorkSheet)
WorkSheetExists = True
Exit Function
notExists:
WorkSheetExists = False
End Function