Copy worksheet from template

SquareJerBear

New Member
Joined
Jan 27, 2020
Messages
5
Office Version
365
Platform
Windows
Hello, I have played with this and still can't figure it out.

I have connected this macro to a check box:

VBA Code:
Sub copy_Sheet()
    Dim wb As Workbook
    Dim activeWB As Workbook
    Dim FilePath As String
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Set activeWB = Application.ActiveWorkbook
    FilePath = "C:\Users\User\Desktop\Excel\5b108a"

    On Error Resume Next
    Set wb = Application.Workbooks.Open(FilePath)
    wb.Worksheets(1).Copy After:=activeWB.Sheets(activeWB.Sheets.Count)
    activeWB.Activate
    wb.Close False
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
I need it to add the worksheet with the name being the same as the template. Currently it just populates as "Sheet1 (2)," where I would want the sheet named "5b108a."

Also, is there a way for it to add the sheet, and not switch to the added sheet each time it's clicked?

Thanks.
 

Some videos you may like

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

gallen

Well-known Member
Joined
Jun 27, 2011
Messages
1,993
Untested so ensure to use a copy of data:

VBA Code:
Sub copy_Sheet()
    Dim wb As Workbook
    Dim activeWB As Workbook
    Dim activeWS As Worksheet
    Dim FilePath As String
    
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    'Remember active workbook
    Set activeWB = Application.ActiveWorkbook
    
    'remember active sheet
    Set activeWS = Application.ActiveSheet
    
    'set the file path
    FilePath = "C:\Users\User\Desktop\Excel\5b108a"

    On Error Resume Next
    
    'set wb to the newly opened workbook
    Set wb = Application.Workbooks.Open(FilePath)
    
    'copy the first sheet in the newly opened book and paste it at the end of the active workbook
    wb.Worksheets(1).Copy After:=activeWB.Sheets(activeWB.Sheets.Count)
    
    'rename the new sheet
    activeWB.Sheets(activeWB.Sheets.Count).Name = wb.Name
    
    'switch to the active sheet
    activeWS.Activate
    
    'close the newly opened file without saving
    wb.Close False
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
 

SquareJerBear

New Member
Joined
Jan 27, 2020
Messages
5
Office Version
365
Platform
Windows
I think this will work. Thank you so much.

I like the way you commented to say what part does. :)

The only issue is I'm having trouble getting it to work from a network drive.
 

SquareJerBear

New Member
Joined
Jan 27, 2020
Messages
5
Office Version
365
Platform
Windows
So for some reason, for this macro to be happy, you must specify the file-type after the file name, but when the file with the macro is on the main drive (C: for me), it works just fine.

Thanks again!
 

gallen

Well-known Member
Joined
Jun 27, 2011
Messages
1,993
I think this will work. Thank you so much.

I like the way you commented to say what part does. :)

The only issue is I'm having trouble getting it to work from a network drive.
Always good practice. It explains to others (as well as yourself) what you are trying to achieve. Makes debugging simpler and faster.

Hadn't spotted the lack of file extension. I'm surprised it works on the local drive at all. I assumed it was always needed.

At least you have it working
 

Watch MrExcel Video

Forum statistics

Threads
1,095,678
Messages
5,445,923
Members
405,370
Latest member
Miguel_Rojas

This Week's Hot Topics

Top