Notes:
- Check to see if there is already a sheet name that is using the desired String
- If there is, clear the cells & reset the UsedRange
- If there isn't, Insert a new Tab, Rename it and move it to the end of the Book
- If there is, clear the cells & reset the UsedRange
InsertTabOrClear (String)
Public Function InsertTabOrClear(myNewName As String)
Debug.Print "InsertTabOrClear"
'----------------------------------------------------------------------------------------------------------
'Defines current tab - returns to it after working with new tab
'Checks for sheet with [String]: If found, CLEAR data
' If NOT found, ADD sheet
'----------------------------------------------------------------------------------------------------------
Dim iCurrentWS As Long, iTabLoop As Long, sCurrentWs As String, bExists As Boolean, iMatchWs As Long
bExists = False
If ActiveSheet.Name = myNewName Then Worksheets(1).Select
iCurrentWS = ActiveSheet.Index: sCurrentWs = ActiveSheet.Name
For iTabLoop = 1 To ActiveWorkbook.Worksheets.Count
If Worksheets(iTabLoop).Name = myNewName Then
bExists = True
iMatchWs = iTabLoop
Exit For
End If
Next iTabLoop
If bExists = True Then
udfReset99 (iTabLoop)
Else
'-------------------------------------------------------------------------
'add new sheet: rename
Sheets.Add after:=Worksheets(ActiveWorkbook.Worksheets.Count)
ActiveSheet.Name = myNewName
'-------------------------------------------------------------------------
End If
Worksheets(sCurrentWs).Select
End Function
Debug.Print "InsertTabOrClear"
'----------------------------------------------------------------------------------------------------------
'Defines current tab - returns to it after working with new tab
'Checks for sheet with [String]: If found, CLEAR data
' If NOT found, ADD sheet
'----------------------------------------------------------------------------------------------------------
Dim iCurrentWS As Long, iTabLoop As Long, sCurrentWs As String, bExists As Boolean, iMatchWs As Long
bExists = False
If ActiveSheet.Name = myNewName Then Worksheets(1).Select
iCurrentWS = ActiveSheet.Index: sCurrentWs = ActiveSheet.Name
For iTabLoop = 1 To ActiveWorkbook.Worksheets.Count
If Worksheets(iTabLoop).Name = myNewName Then
bExists = True
iMatchWs = iTabLoop
Exit For
End If
Next iTabLoop
If bExists = True Then
udfReset99 (iTabLoop)
Else
'-------------------------------------------------------------------------
'add new sheet: rename
Sheets.Add after:=Worksheets(ActiveWorkbook.Worksheets.Count)
ActiveSheet.Name = myNewName
'-------------------------------------------------------------------------
End If
Worksheets(sCurrentWs).Select
End Function
udfReset99 (Sheet.Index)
Function udfReset99(myTempWs As Long)
Debug.Print "udfReset99"
Dim iLastWs As Long
iLastWs = ActiveSheet.Index
'Make sure the current Ws is not the same as the temp
If myTempWs = iLastWs Then
If iLastWs = 1 Then
iLastWs = 2
Else
iLastWs = iLastWs - 1
End If
End If
'Select the temp WS and clear the data
Worksheets(myTempWs).Select
Cells.Clear
'Delete the Used Range
Worksheets(myTempWs).UsedRange.Delete
Worksheets(myTempWs).UsedRange
'Go back to the original sheet
Worksheets(iLastWs).Select
End Function
Debug.Print "udfReset99"
Dim iLastWs As Long
iLastWs = ActiveSheet.Index
'Make sure the current Ws is not the same as the temp
If myTempWs = iLastWs Then
If iLastWs = 1 Then
iLastWs = 2
Else
iLastWs = iLastWs - 1
End If
End If
'Select the temp WS and clear the data
Worksheets(myTempWs).Select
Cells.Clear
'Delete the Used Range
Worksheets(myTempWs).UsedRange.Delete
Worksheets(myTempWs).UsedRange
'Go back to the original sheet
Worksheets(iLastWs).Select
End Function