If there isn't already a worksheet with the name Mcat Prepare, I want to copy worksheet MCAT and rename it Mcat Prepare. The code jerks out and terminates without giving me any errors after this line, which is my issue.
Worksheets("MCAT").Copy After:=Worksheets("MCAT") 'duplicate the worksheet after the original
The line is being executed which produces MCAT(2), but code stops after. This is the entire code:
Dim wb_MCAT As Workbook
Dim ws_MCAT As Worksheet
'Open the excel specified
filepath = Application.GetOpenFilename("Mcat Master (*.xls), *.xls", , "Select a file", , False)
If filepath = False Then
MsgBox "No file selected."
End If
Set wb_MCAT = Workbooks.Open(filepath) 'set the workbook where the worksheet is located
Set ws_MCAT = wb_MCAT.Worksheets("MCAT") 'set the worksheet to duplicate
'Check if a worksheet with the name Mcat_Prepare already exists
ws_MCAT.Activate
Dim ws_McatPrepareExists As Boolean
ws_McatPrepareExists = False
For Each existingMcatPrepare In wb_MCAT.Worksheets
If existingMcatPrepare.Name = "Mcat_Prepare" Then
ws_McatPrepareExists = True
Exit For
End If
Next existingMcatPrepare
'If not, duplicate MCAT and rename it as Mcat_Prepare
If Not ws_McatPrepareExists Then
Worksheets("MCAT").Copy After:=Worksheets("MCAT") 'duplicate the worksheet after the original
ActiveSheet.Name = "Mcat_Prepare" 'rename the new worksheet
End If