I have this code,i want to short sheets in excel by name cane anyone improve this code.
Sub Select_Sheet()
Dim sh As Worksheet, shNam As String
If IsEmpty(Sheets("Sum").Range("P9")) Then Exit Sub
shNam = Sheets("Sum").Range("P9").Value
If SheetExists(ActiveWorkbook.Name, shNam) Then
Sheets(shNam).Select
Else
Sheets("MBC").Copy after:=Worksheets(Sheets.Count)
ActiveSheet.Name = shNam
End If
End Sub
Function SheetExists(wbName As String, shName As String) As Boolean
SheetExists = False
With Workbooks(wbName)
For Each sh In .Sheets
If sh.Name = shName Then
SheetExists = True
Exit For
End If
Next sh
End With
End Function
Thanks
Sub Select_Sheet()
Dim sh As Worksheet, shNam As String
If IsEmpty(Sheets("Sum").Range("P9")) Then Exit Sub
shNam = Sheets("Sum").Range("P9").Value
If SheetExists(ActiveWorkbook.Name, shNam) Then
Sheets(shNam).Select
Else
Sheets("MBC").Copy after:=Worksheets(Sheets.Count)
ActiveSheet.Name = shNam
End If
End Sub
Function SheetExists(wbName As String, shName As String) As Boolean
SheetExists = False
With Workbooks(wbName)
For Each sh In .Sheets
If sh.Name = shName Then
SheetExists = True
Exit For
End If
Next sh
End With
End Function
Thanks