XLA Routines: EE_ReplaceSheet
EE_ReplaceSheet is a useful routine that replaces a sheet, and does not error if the sheet does not exist meaning you can use the routine to create a new sheet.
Function EE_ReplaceSheet(strSheet As String) As Worksheet Dim wksNew As Worksheet Dim wbk As Workbook Dim blnDisplayAlerts As Boolean Set wbk = ActiveWorkbook On Error Resume Next Set wksNew = wbk.Worksheets.Add(after:=wbk.Worksheets(strSheet)) Err.Clear: On Error GoTo 0: On Error GoTo -1 If wksNew Is Nothing Then Set wksNew = wbk.Worksheets.Add(after:=wbk.Worksheets(ThisWorkbook.Worksheets.Count)) wksNew.Name = strSheet Set wksNew = Nothing Exit Function End If blnDisplayAlerts = Application.DisplayAlerts Application.DisplayAlerts = False On Error Resume Next wbk.Worksheets(strSheet).Delete wksNew.Name = strSheet Err.Clear: On Error GoTo 0: On Error GoTo -1 Application.DisplayAlerts = blnDisplayAlerts Set EE_ReplaceSheet = wksNew Set wksNew = Nothing Set wbk = Nothing End Function
»
- Nick's blog
- Login or register to post comments
- 2650 reads
Recent comments
5 years 36 weeks ago
6 years 22 weeks ago
6 years 34 weeks ago
6 years 37 weeks ago
6 years 38 weeks ago
6 years 43 weeks ago
6 years 52 weeks ago
7 years 2 days ago
7 years 3 days ago
7 years 3 days ago