vba to copy certain sheet and make copy of it as a stand alone workbook

ansvk1

Board Regular
Joined
Oct 6, 2017
Messages
82
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi there,
i am trying to create a workbook out of a specific sheet (the name and location changes every time- based on need) from a different workbook. And this is how far i got with my code that gets stuck at line 16

Code:
Sub Button1_Click()


    Dim ws As Worksheet
    Dim wbNew As Workbook, wbCurrent As Workbook
    Dim strFileName As String


    'Assign object variables
    Set wbCurrent = ActiveWorkbook
    Set ws = wbCurrent.ActiveSheet
    
    'Get desired file path from user
    strFileName = InputBox("Enter File Name: ", "Creating New File...")
    If strFileName <> "" Then
        'copy whole current sheet
        ws.Copy
        Set wbNew = ws
        wbNew.SaveAs wbCurrent.Path & "\" & strFileName
    End If
End Sub

any help is greatly appreciated!
thanks
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Nearly there.

Code:
Sub Button1_Click()

    Dim ws As Worksheet
    Dim wbNew As Workbook, wbCurrent As Workbook
    Dim strFileName As String




    'Assign object variables
    Set wbCurrent = ActiveWorkbook
    Set ws = wbCurrent.ActiveSheet
    
    'Get desired file path from user
    strFileName = InputBox("Enter File Name: ", "Creating New File...")
    If strFileName <> "" Then
        'copy whole current sheet
        ws.Copy
        myFileName = wbCurrent.Path & "\" & strFileName & ".xlsx"
        ActiveWorkbook.SaveAs Filename:=myFileName, FileFormat:=xlWorkbookNormal
        
    End If
    


End Sub
 
Upvote 0
thank, it works great!
could not figure out myfilename part....so close but so far :)

And just now i have realised this macro cannot be run in my workbook as it is a .xlsx file. any suggestions on how to be able to run this procedure from a different macro enabled workbook and specify the destination of the to be copied workbook and its specific sheet name to be copied from there?
 
Upvote 0
You can just save it to your personal workbook and run it anytime you like on the current book/sheet you have active.

and specify the destination of the to be copied workbook and its specific sheet name to be copied from there

Not too sure what you mean here.
 
Upvote 0
uhm, this is for my whole team (5people), who will be needing this on a daily basis. Do i have to have all of them setup their personal workbook? or is there any easier way to have this worked out easily, i cannot change current workbook to .xlsm format?
 
Last edited:
Upvote 0
If it's just out of the one book then for sure, just save as .xlsm.

currently the macro saves the new file as xlsx only
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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