SquareJerBear

New Member
Joined
Jan 27, 2020
Messages
5
Office Version
  1. 365
Platform
  1. 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.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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
 
Upvote 0
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.
 
Upvote 0
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!
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,726
Members
448,294
Latest member
jmjmjmjmjmjm

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top