Code breaks after executing copy statement in VBA

0 votes

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
Mar 31, 2023 in Others by narikkadan
• 63,600 points
540 views

1 answer to this question.

0 votes

Qualify the sheets with a workbook reference or use ws_MCAT.

    Dim i As Long
    If Not ws_McatPrepareExists Then
        With ws_MCAT
            i = .Index
            .Copy After:=.Parent.Sheets(i)   'duplicate the worksheet after the original
            .Parent.Sheets(i + 1).Name = "Mcat_Prepare" 'rename the new worksheet
        End With
    End If
answered Mar 31, 2023 by Kithuzzz
• 38,000 points

Related Questions In Others

0 votes
1 answer

How do I copy a specific range of cells after I use AutoFilter in VBA?

Set the range of filtered data Set Rng ...READ MORE

answered Feb 2, 2023 in Others by narikkadan
• 63,600 points
861 views
0 votes
1 answer

How can increase the speed of if statement in VBA Code?

Use a Dictionary Object. Option Explicit Sub PreencherO() ...READ MORE

answered Feb 7, 2023 in Others by narikkadan
• 63,600 points
532 views
0 votes
1 answer

How to find a value in an excel column by vba code Cells.Find

Just use: Dim Cell As Range Columns("B:B").Select Set cell = ...READ MORE

answered Nov 17, 2022 in Others by narikkadan
• 63,600 points
3,768 views
0 votes
1 answer

How to programmatically code an 'undo' function in Excel-Vba?

Add the command button to the worksheet ...READ MORE

answered Nov 25, 2022 in Others by narikkadan
• 63,600 points
1,034 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,212 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,662 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
908 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
1,053 views
0 votes
1 answer

What VBA code would I use to concatenate cell A2 & B2 in cell C2 and then have it Autofill down the column?

Solution Find the last row. Write a formula to ...READ MORE

answered Feb 14, 2023 in Others by Kithuzzz
• 38,000 points
1,016 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP