Why does delete a sheet cause my code to stop - vba

I have some code here (Below) I need to clear the data within the WorkSheet "Data Entry" there may be other, quicker, ways to do this, but i went with delete and remake. Please feel free to say if so.
Private Sub CommandButton1_Click()
Dim DataEntryWs As Worksheet
For i = Worksheets.Count To 1 Step -1
If Worksheets(i).Name = "Data Entry" Then
Application.DisplayAlerts = False
Worksheets("Data Entry").Delete
MsgBox ("Sheet Deleted")
Set DataEntryWs = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
DataEntryWs.Name = "Data Entry"
Call Data_Entry_Calcs
Else
If i = Worksheets.Count Then
MsgBox ("Adding new sheets now")
Set DataEntryWs = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
DataEntryWs.Name = "Data Entry"
Call Data_Entry_Calcs
Else
End If
End If
Next i
Call Data_Entry_Calcs
End Sub
Whenever I run the code, if the Sheet named "Data Entry" is present then when the code gets to this line Worksheets("Data Entry").Delete the code breaks and dosen't contiue. Why is this the case? Been annoying me for a while now.
I have tried running the For loop both forwards and backwards to see if this has made any difference, but had no success with it.

How about clearing the contents of that Sheet instead of deleting it, such as:
Private Sub CommandButton1_Click()
Dim DataEntryWs As Worksheet
For i = Worksheets.Count To 1 Step -1
If Worksheets(i).Name = "Data Entry" Then
Application.DisplayAlerts = False
Worksheets("Data Entry").Rows("2:" & Rows.Count).ClearContents 'clear the contents from Row 2 to last
Call Data_Entry_Calcs
Else
If i = Worksheets.Count Then
MsgBox ("Adding new sheets now")
Set DataEntryWs = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
DataEntryWs.Name = "Data Entry"
Call Data_Entry_Calcs
End If
End If
Next i
Call Data_Entry_Calcs
End Sub

Can you try this instead. You don't need the loop to do this if I understand what you're trying to do correctly
Private Sub CommandButton1_Click()
Dim DataEntryWs As Worksheet
' Set Sheet want to test to variable
' We use error handling in case it doesn't exist. If it doesn't exists DataEntryWs = nothing
On Error Resume Next
Set DataEntryWs = ThisWorkbook.Worksheets("Data Entry")
On Error GoTo 0
' Test if sheet exists. If does Delete
If Not DataEntryWs Is Nothing Then
Application.DisplayAlerts = False
DataEntryWs.Delete
Application.DisplayAlerts = True
MsgBox "Sheet Deleted"
End If
' Add new sheet
MsgBox "Adding new sheets now"
With ThisWorkbook
Set DataEntryWs = .Sheets.Add(after:=.Sheets(.Sheets.Count))
End With
DataEntryWs.Name = "Data Entry"
Call Data_Entry_Calcs
End Sub

Can you try this code and let me know what happens, and yes, I do get the messagebox when I run it...
Sub Test2()
Dim i As Integer
On Error GoTo err_handler
For i = Worksheets.Count To 1 Step -1
If Worksheets(i).Name = "Data Entry" Then
Application.DisplayAlerts = False
Worksheets("Data Entry").Delete
Application.DisplayAlerts = True
MsgBox ("Sheet Deleted")
End If
Next i
Exit Sub
err_handler:
MsgBox Err.Description
End Sub

Why not try just this?
Private Sub CommandButton1_Click()
Dim DataEntryWs As Worksheet
On Error Resume Next
Set DataEntryWs = Sheets("Data Entry")
On Error GoTo 0
If Not DataEntryWs Is Nothing Then
DataEntryWs.Cells.Clear
MsgBox "Sheet Data Entry cleared.", vbInformation
Else
MsgBox "Adding new sheet now.", vbInformation
With ThisWorkbook
Set DataEntryWs = .Sheets.Add(after:=.Sheets(.Sheets.Count))
DataEntryWs.Name = "Data Entry"
End With
End If
DataEntryWs.Activate
Call Data_Entry_Calcs
End Sub

Related

How do I apply a macro to multiple excel files when the macro contains many subs?

I have used a macro to track changes in a workbook, but I would now like to run this macro in over a 100 excel files within a particular folder using a Do While Loop.
I am very new to VBA and will appreciate all the help I can get.
I have come across some code that should enable me to loop through excel files in a folder and run the macro in each one.
However it requires me to get rid of the 'sub' and 'end sub' from the macro when I copy and paste it into the do while loop, but I have 3 of them within the macro; some variables will be undefined if I delete all 3.
Therefore I tried 'Call Tracker' within the loop ('Tracker' being the macro name) and hoped it would run in each excel file.
Sub LoopThroughFiles()
Dim xFd As FileDialog
Dim xFdItem As Variant
Dim xFileName As String
Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
If xFd.Show = -1 Then
xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
xFileName = Dir(xFdItem & "*,xls*")
Do While xFileName <> ""
With Workbooks.Open(xFdItem & xFileName)
'Your code here
Call Tracker
End With
xFileName = Dir
Loop
End If
End Sub
Below is the code inside 'Tracker'
Option Explicit
Dim sOldAddress As String
Dim vOldValue As Variant
Public Sub Workbook_TrackChange(Cancel As Boolean)
Dim Sh As Worksheet
For Each Sh In ActiveWorkbook.Worksheets
Sh.PageSetup.LeftFooter = "&06" & ActiveWorkbook.FullName & vbLf & "&A"
Next Sh
End Sub
Public Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim wSheet As Worksheet
Dim wActSheet As Worksheet
Dim iCol As Integer
Set wActSheet = ActiveSheet
'Precursor Exits
'Other conditions that you do not want to track could be added here
If vOldValue = "" Then Exit Sub 'If you comment out this line *every* entry will be recorded
'Continue
On Error Resume Next ' This Error-Resume-Next is only to allow the creation of the tracker sheet.
Set wSheet = Sheets("Tracker")
'**** Add the tracker Sheet if it does not exist ****
If wSheet Is Nothing Then
Set wActSheet = ActiveSheet
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Tracker"
End If
On Error GoTo 0
'**** End of specific error resume next
On Error GoTo ErrorHandler
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
With Sheets("Tracker")
'******** This bit of code moves the tracker over a column when the first columns are full**'
If .Cells(1, 1) = "" Then '
iCol = 1 '
Else '
iCol = .Cells(1, 256).End(xlToLeft).Column - 7 '
If Not .Cells(65536, iCol) = "" Then '
iCol = .Cells(1, 256).End(xlToLeft).Column + 1 '
End If '
End If '
'********* END *****************************************************************************'
.Unprotect Password:="Secret"
'******** Sets the Column Headers **********************************************************
If LenB(.Cells(1, iCol).Value) = 0 Then
.Range(.Cells(1, iCol), .Cells(1, iCol + 7)) = Array("Cell Changed", "SAP ID", "Field Name", "Old Field Value", _
"New Field Value", "Time of Change", "Date Stamp", "User")
.Cells.Columns.AutoFit
End If
With .Cells(.Rows.Count, iCol).End(xlUp).Offset(1)
If Target.Count = 1 Then
.Offset(0, 1) = Cells(Target.Row, 2) 'SAPID
End If
'.Offset(0, 1) = Cells(Target.Row, 2) 'SAPID
If Target.Count = 1 Then
.Offset(0, 2) = Cells(Target.Column) 'Field name
End If
'.Offset(0, 2) = Cells(Target.Column) 'Field name
.Value = sOldAddress
.Offset(0, 3).Value = vOldValue
If Target.Count = 1 Then
.Offset(0, 4).Value = Target.Value
End If
.Offset(0, 5) = Time
.Offset(0, 6) = Date
.Offset(0, 7) = Application.UserName
.Offset(0, 7).Borders(xlEdgeRight).LineStyle = xlContinuous
End With
.Protect Password:="Secret" 'comment to protect the "tracker tab"
End With
ErrorExit:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
wActSheet.Activate
Exit Sub
ErrorHandler:
'any error handling you want
'Debug.Print "We have an error"
Resume ErrorExit
End Sub
Public Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
With Target
sOldAddress = .Address(external:=True)
If .Count > 1 Then
vOldValue = "Multiple Cell Select"
Else
vOldValue = .Value
End If
End With
End Sub
'Call Tracker' in the loop does not produce an error. In fact the code seems to execute and loops through all the files but it does not run the macro in each one it opens.

Need solution to: "Sorry an error occurred object doesn't support this property or method"

I have code that uses information from a set of text boxes in a userform to find and edit values in two workbooks. The code that I am using to edit the values in the second workbook gives me the following error, "Sorry an Error Occurred Object doesn't support this property or method". Can anyone help me with this? Aside from what is causing the error I think my code is correct but if anyone sees any errors in my code by all means please feel free to correct me or offer suggestions. Thanks in advance!
Private Sub Submit_Click()
Dim WS As Worksheet
Dim lastrow As Long
Dim r As Long
Dim password As String
Application.ScreenUpdating = False
If Not IsNumeric(TextBox1.Text) Then
On Error GoTo ErrorHandler
password = TextBox1.Text
Set WS = ActiveWorkbook.Worksheets("Accounts")
lastrow = WS.Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lastrow
If WS.Cells(r, 2) = Label5.Caption Then
WS.Cells(r, 2).Value = TextBox1.Text
WS.Cells(r, 3).Value = TextBox2.Text
WS.Cells(r, 4).Value = TextBox3.Text
MsgBox "Update Successful", vbInformation
TextBox1.Text = ""
Call Edit_Login
Application.ScreenUpdating = True
Exit Sub
End If
Next
MsgBox "Data not Found!!", vbCritical
TextBox1.Text = ""
Unload Me
Application.ScreenUpdating = True
Exit Sub
ErrorHandler: MsgBox "Sorry an Error occured. " & vbCrLf & Err.Description
Exit Sub
End If
MsgBox "Please Enter Correct Information", vbCritical
Application.ScreenUpdating = True
End Sub
Private Sub Edit_Login()
Dim Wkbk As Workbook
Dim txt As String
Dim txt2 As String
Dim txt3 As String
Dim lastrow As Long
Dim r As Long
Dim Account As String
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
If Not IsNumeric(TextBox1.Text) Then
On Error GoTo ErrorHandler
Account = TextBox1.Text
Set Wkbk = Workbooks.Open("C:\Users\kameron\Desktop\Quality Improvement
Software\Log In.xlsm")
lastrow = Wkbk.Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lastrow
If Wkbk.Sheets("Tables").Cells(r, 1) = Label5.Caption Then
Wkbk.Sheets("Tables").Cells(r, 1).Value = TextBox1.Text
Wkbk.Sheets("Tables").Cells(r, 2).Value = TextBox2.Text
Wkbk.Sheets("Tables").Cells(r, 3).Value = TextBox3.Text
MsgBox "Update Successful", vbInformation
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
Unload EditAccount
Application.ScreenUpdating = True
Exit Sub
End If
Next
MsgBox "Data not Found!!", vbCritical
TextBox1.Text = ""
Unload Me
Application.ScreenUpdating = True
Exit Sub
ErrorHandler: MsgBox "Sorry an Error occured. " & vbCrLf & Err.Description
Exit Sub
End If
MsgBox "Please Enter Correct Information", vbCritical
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
The problem is the line
lastrow = Wkbk.Cells(Rows.Count, "A").End(xlUp).Row
Workbook objects don't have a Cells property.
In context, you seem to want
lastrow = Wkbk.Sheets("Tables").Cells(Rows.Count, "A").End(xlUp).Row
In order to track down this error, you could have done one of two things:
1) Stepped through the code using F8 and see what line it fails on.
2) Temporarily commented out the line On Error GoTo ErrorHandler and run the code.
Either approach would have quickly led to that line.

Excel VBA Clearing range after copy and pasting range to another sheet

I'm using a code where the workbook detects if the current month has a sheet assigned to it or not and if not then the workbook will create a new sheet with the current month. After creating a new sheet it would copy and paste a certain range from the main sheet onto the new one. My problem is that after doing so I use a Range.Clear to clean the range that I copy pasted however it seems to be clearing it BEFORE copy-pasting.
Private Sub Worksheet_Change(ByVal Target As Range)
nowMonth = Month(Now)
nowYear = Year(Now)
sheetNameStr = nowMonth & "," & nowYear
sheetExists = False
For Each Sheet In Worksheets
If sheetNameStr = Sheet.Name Then
sheetExists = True
End If
Next Sheet
If sheetExists = False Then
Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws.Name = sheetNameStr
MsgBox ("New sheet named " & sheetNameStr & "was created")
End If
Sheets("Main").Activate
Worksheets("Main").Range("A4:D300").Copy Worksheets(sheetNameStr).Range("A1")
Worksheets("Main").Range("A6:D300").Clear
End Sub
Any help would be great thank you.
Here's what happens: the .Clear method causes Worksheet_Change to fire again; the Copy operation is repeated, clearing the destination; then the second Clear doesn't change anything, the source having been cleared already, and both Worksheet_Change procedures exit.
You have to surround your code with:
Application.EnableEvents = False
and
Application.EnableEvents = True
Here's the updated code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nowMonth As Integer
Dim nowYear As Integer
Dim sheetNameStr As String
Dim oSheet As Excel.Worksheet
Dim oNewSheet As Excel.Worksheet
Dim sheetExists As Boolean
On Error GoTo errHandler
Application.EnableEvents = False
nowMonth = Month(Now)
nowYear = Year(Now)
sheetNameStr = nowMonth & "," & nowYear
sheetExists = False
For Each oSheet In ThisWorkbook.Worksheets
If sheetNameStr = oSheet.Name Then
sheetExists = True
Exit For 'Found, can exit the loop.
End If
Next
If Not sheetExists Then
Set oNewSheet = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Sheets.Count))
oNewSheet.Name = sheetNameStr
MsgBox "New sheet named " & sheetNameStr & " was created."
End If
Me.Activate
Me.Range("A4:D300").Copy ThisWorkbook.Worksheets(sheetNameStr).Range("A1")
Me.Range("A6:D300").Clear
Recover:
On Error Resume Next
Set oNewSheet = Nothing
Set oSheet = Nothing
Application.EnableEvents = True
Exit Sub
errHandler:
MsgBox Err.Description, vbExclamation + vbOKOnly, "Error"
Resume Recover
End Sub
Notice that Worksheets is now qualified by ThisWorkbook; otherwise, your code would be referring to whichever workbook is active. Also, Sheets("Main") was replaced by Me as I assume your code is behind the Main worksheet and Me, from there, is the worksheet itself. Finally, whenever you turn EnableEvents off, you must provide adequate error handling to turn it back on in case of issues.
Edit
Here's the original code with just minimal changes to handle EnableEvents:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo errHandler
Application.ScreenUpdating = False
nowMonth = Month(Now)
nowYear = Year(Now)
sheetNameStr = nowMonth & "," & nowYear
sheetExists = False
For Each Sheet In Worksheets
If sheetNameStr = Sheet.Name Then
sheetExists = True
Exit For
End If
Next Sheet
If Not sheetExists Then
Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws.Name = sheetNameStr
MsgBox ("New sheet named " & sheetNameStr & "was created")
End If
Sheets("Main").Activate
Worksheets("Main").Range("A4:D300").Copy Worksheets(sheetNameStr).Range("A1")
Worksheets("Main").Range("A6:D300").Clear
Recover:
On Error Resume Next
Application.ScreenUpdating = True
Exit Sub
errHandler:
MsgBox Err.Description, vbExclamation + vbOKOnly, "Error"
Resume Recover
End Sub

Excel vba username/ password lookup

Private Sub cmdLogin_Click()
On Error GoTo ErrorHandler
Dim RowNo As Long
Dim Id As String
Dim pw As String
Dim ws As Worksheets
Application.ScreenUpdating = False
Set ws = Worksheets("User&Pass")
Id = LCase(Me.txtLogin)
RowNo = Application.WorksheetFunction.Match(Id, ws.range("A2:A999"), 0)
CleanExit:
Set ws = Nothing ' free memory
Application.ScreenUpdating = True ' turn on the screen updating
Exit Sub
ErrorHandler:
MsgBox "Unable to match ID, enter valid ID.", vbOKOnly
GoTo CleanExit
End Sub
I've got an excel userform i've been working on and now I need it to look more professional by having a log-in screen. I've started with the code above but I have come to a dead end.
how its set up my aim is to say if id & password matches then load up workbook or unhide the workbook and continue. the username and password are on a sheet called "User&Pass"
Aim is it reads from there in columns a- user / b- pw respectively and if it's a success I will hide that sheet so they cant see other user's information
with what I started above I just need it to say if it matches usercolumn then corresponding pw next door to it continue else go to my errorhandler
i can do the formatting about hiding and unhiding sheets etc just need help with reading username and pw
thanks very much in advance
Z
Editted attempt one;
Private Sub cmdLogin_Click()
On Error GoTo ErrorHandler
Dim RowNo As Long
Dim Id As String
Dim pw As String
Dim ws As Worksheets
Application.ScreenUpdating = False
Set ws = Worksheets("User&Pass")
Id = LCase(Me.txtLogin)
RowNo = Application.WorksheetFunction.Match(Id, ws.range("A2:A999"), 0)
RowNo = RowNo + 1
pw = ws.range("B" & RowNo)
If pw = Me.txtLogin Then
'continue
txt1.Value = "yes"
Else
GoTo ErrorHandler
End If
CleanExit:
Set ws = Nothing ' free memory
Application.ScreenUpdating = True ' turn on the screen updating
Exit Sub
ErrorHandler:
MsgBox "Unable to match ID, enter valid ID.", vbOKOnly
GoTo CleanExit
End Sub
#siddarthRout
Private Sub cmdLogin_Click()
Dim RowNo As Long
Dim Id As String, pw As String
Dim ws As Worksheet
Dim aCell As range
On Error GoTo ErrorHandler
Application.ScreenUpdating = True
Set ws = Worksheets("Details")
Id = LCase(Me.txtLogin)
Set aCell = ws.Columns(1).Find(What:=Id, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'~~> If match found
If Not aCell Is Nothing Then
RowNo = aCell.Row
'~~> Rest of your code. For example if the password is
'~~> Stored in Col B then
Debug.Print aCell.Offset(, 1)
Unload Me
FrmMenu.Show
'~~> You can then use the above aCell.Offset(, 1) to
'~~> match the password which the user entered
Else '<~~ If not found
MsgBox "Unable to match ID, enter valid ID.", vbOKOnly
End If
CleanExit:
Set ws = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrorHandler:
MsgBox Err.Description
Resume CleanExit
End Sub
TESTED AND TRIED
Is this what you are trying?
CODE
Option Explicit
Private Sub cmdLogin_Click()
Dim RowNo As Long
Dim Id As String, pw As String
Dim ws As Worksheet
Dim aCell As Range
On Error GoTo ErrorHandler
If Len(Trim(txtLogin)) = 0 Then
txtLogin.SetFocus
MsgBox "Username cannot be empty"
Exit Sub
End If
If Len(Trim(txtPassword)) = 0 Then
txtPassword.SetFocus
MsgBox "Password cannot be empty"
Exit Sub
End If
Application.ScreenUpdating = False
Set ws = Worksheets("User&Pass")
Id = LCase(Me.txtLogin)
Set aCell = ws.Columns(1).Find(What:=Id, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'~~> If match found
If Not aCell Is Nothing Then
RowNo = aCell.Row
If Me.txtPassword = aCell.Offset(, 1) Then
FrmMenu.Show
Unload Me
Else
MsgBox "Unable to match UserID or PasswordID, Please try again", vbOKOnly
End If
Else '<~~ If not found
MsgBox "Unable to match UserID or PasswordID, Please try again", vbOKOnly
End If
CleanExit:
Set ws = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrorHandler:
MsgBox Err.Description
Resume CleanExit
End Sub
TIP:
Never let your user know (from security perspective) what was incorrect - The username or the password. Always show a generic message like "Unable to match UserID or PasswordID, Please try again" :)
HTH
Sid
Another way
On Error Resume Next
If Me.password <> Application.VLookup(Me.username, Sheet1.Cells(1, 1).CurrentRegion, 2, False) Then
MsgBox ("incorrect")
Exit Sub
Else
MsgBox ("Correct Password Entered")
End If
Also you will need to make sure that all your sheets are xlSheetVeryHidden from the outset to combat having macros disabled and un-hide them as part of your successful log in routine. You'll also want to set a password on your VBA project to prevent people unhiding the sheets. Bear in mind however, Excel is about as secure as a wet paper bag ;)

Add new sheet to existing Excel workbook with VB code

This code creates an Excel file with one sheet. This sheet contains the code of an item like (ASR/Floor/Dept./Item_Name/Item_details/1) which I created and works fine, but I want to add a sheet into this Excel file to create another item code, and then save this file.
Dim xlApp As Excel.Application
Dim wb As Workbook
Dim ws As Worksheet
Dim var As Variant
Dim code As String
Dim i, nocode As Integer
Dim fname, heading As String
code = "ASR/" & Text1.Text & "/" & Text2.Text & "/" & Text3.Text & "/" & Text4.Text
Set xlApp = New Excel.Application
Set wb = xlApp.Workbooks.Add ' Create a new WorkBook
Set ws = wb.Worksheets("Sheet1") 'Specify your worksheet name
nocode = txtnocode.Text
heading = Text6.Text
For i = 2 To nocode + 1
ws.Cells(i, 1).Value = code & "/" & i - 1 '"ORG"
Next i
fname = "c:\" & Text5.Text & ".xls"
wb.SaveAs (fname)
wb.Close
xlApp.Quit
Set ws = Nothing
Set wb = Nothing
Set xlApp = Nothing
The Worksheets.Add method is what you are looking for:
wb.WorkSheets.Add().Name = "SecondSheet"
See MSDN(scroll down and expand Sheets and Worksheets) for the different parameters you can give to .Add including being able to add the sheet before or after a specific one.
Set ws = wb.Sheets("Sheet1")
Set ws = wb.Sheets.Add
ws.Activate
This is some standard code I use for this type of problem
Note: This code is VBA, to run from within the Excel document itself
Option Explicit
Private m_sNameOfOutPutWorkSheet_1 As String
Sub Delete_Recreate_TheWorkSheet()
On Error GoTo ErrorHandler
'=========================
Dim strInFrontOfSheetName As String
m_sNameOfOutPutWorkSheet_1 = "Dashboard_1"
strInFrontOfSheetName = "CONTROL" 'create the new worksheet in front of this sheet
'1] Clean up old data if it is still there
GetRidOf_WorkSheet_IfItExists (m_sNameOfOutPutWorkSheet_1)
CreateNewOutputWorkSheet m_sNameOfOutPutWorkSheet_1, strInFrontOfSheetName
'Color the tab of the new worksheet
ActiveWorkbook.Sheets(m_sNameOfOutPutWorkSheet_1).Tab.ColorIndex = 5
'Select the worksheet that I started with
Worksheets(strInFrontOfSheetName).Select
'=========================
Exit Sub
ErrorHandler:
Select Case Err.Number
Case Else
MsgBox "One_Main - Error: " & Err.Number & " " & Err.Description
End Select
End Sub
Sub GetRidOf_WorkSheet_IfItExists(sWorkSheetName_ForInitalData As String)
On Error GoTo ErrorHandler
'=========================
If fn_WorkSheetExists(sWorkSheetName_ForInitalData) Then
'Sheet Exists
Application.DisplayAlerts = False
Worksheets(sWorkSheetName_ForInitalData).Delete
Application.DisplayAlerts = True
End If
'=========================
Exit Sub
ErrorHandler:
Select Case Err.Number
Case Else
MsgBox "GetRidOf_WorkSheet_IfItExists - Error: " & Err.Number & " " & Err.Description
End Select
End Sub
Function fn_WorkSheetExists(wsName As String) As Boolean
On Error Resume Next
fn_WorkSheetExists = Worksheets(wsName).Name = wsName
End Function
Sub CreateNewOutputWorkSheet(sWorkSheetName_ForOutputData As String, strInFrontOfSheetName As String)
On Error GoTo ErrorHandler
'=========================
If fn_WorkSheetExists(sWorkSheetName_ForOutputData) Then
'Sheet Exists
Application.DisplayAlerts = False
Worksheets(sWorkSheetName_ForOutputData).Delete
Application.DisplayAlerts = True
End If
Dim wsX As Worksheet
Set wsX = Sheets.Add(Before:=Worksheets(strInFrontOfSheetName))
wsX.Name = sWorkSheetName_ForOutputData
'=========================
Exit Sub
ErrorHandler:
Select Case Err.Number
Case Else
MsgBox "CreateNewOutputWorkSheet - Error: " & Err.Number & " " & Err.Description
End Select
End Sub