VBA to copy a worksheet to a new workbook using same format for .xls files

WStockel

New Member
Joined
May 30, 2020
Messages
30
Office Version
  1. 365
Platform
  1. Windows
I need a VBA code to copy a worksheet to a new workbook using the same format. I only want to copy values and not formulas. Since this file will be emailed to a number of members I need it done in .xls format.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You do realize that .xls format is pre 2007 version of Excel, don't you? It seems odd that running 365 one would suggest saving in a retrograde format.
 
Upvote 0
It seems odd that running 365 one would suggest saving in a retrograde format.
... not if the "number of members" the OP is talking about have retrogade Office versions ?

Change both file path and worksheet name to suit your needs:
VBA Code:
Public Sub CopySheet()
    
    Const cFullName As String = "C:\Users\Wstockel\Documents\NewBook.xls"   '  <<< change accordingly

    Dim oSht    As Worksheet
    With Application
        .ScreenUpdating = False
        ThisWorkbook.Worksheets("Sheet1").Copy                              '  <<< change accordingly
        Set oSht = ActiveSheet
        oSht.Cells.Copy
        oSht.Cells(1, 1).PasteSpecial Paste:=xlPasteValues
        .CutCopyMode = False
        oSht.Cells(1, 1).Select
        oSht.Parent.SaveAs Filename:=cFullName, FileFormat:=xlWorkbookNormal
        .ScreenUpdating = True
    End With
End Sub
 
Upvote 0
... not if the "number of members" the OP is talking about have retrogade Office versions ?

That is true, but I like to clear the air on the issue first, then work on the code.
regards, JLG
 
Upvote 0
I'm writing the program for an older person who is running an old version of excel. They will be the one generating the new file that they will email to others that have old versions of excel.
 
Upvote 0
@GWteB So the code you sent is a macro in the target worksheet? If so I need it the other way. I need the source file to write the to new worksheet with just values and same formatting.
 
Upvote 0
it appears
... not if the "number of members" the OP is talking about have retrogade Office versions ?

Change both file path and worksheet name to suit your needs:
VBA Code:
Public Sub CopySheet()
   
    Const cFullName As String = "C:\Users\Wstockel\Documents\NewBook.xls"   '  <<< change accordingly

    Dim oSht    As Worksheet
    With Application
        .ScreenUpdating = False
        ThisWorkbook.Worksheets("Sheet1").Copy                              '  <<< change accordingly
        Set oSht = ActiveSheet
        oSht.Cells.Copy
        oSht.Cells(1, 1).PasteSpecial Paste:=xlPasteValues
        .CutCopyMode = False
        oSht.Cells(1, 1).Select
        oSht.Parent.SaveAs Filename:=cFullName, FileFormat:=xlWorkbookNormal
        .ScreenUpdating = True
    End With
End Sub
I got this code to work. But how do I modify this line of code so it creates the new workbook on the same path as the source worksheet if I don't know the path?
 
Upvote 0
Well, you could assume that they will be saving to the same directory that the host workbook is in, so use the path for that workbook.
VBA Code:
Dim fPath As String
fPath = ThisWorkbook.Path & "\"
Then use fPath wherever the path is needed in the code.
Otherwise, you will have to contact whoever you are doing it for and ask them for the path to use.
 
Upvote 0
Well, you could assume that they will be saving to the same directory that the host workbook is in, so use the path for that workbook.
VBA Code:
Dim fPath As String
fPath = ThisWorkbook.Path & "\"
Then use fPath wherever the path is needed in the code.
Otherwise, you will have to contact whoever you are doing it for and ask them for the path to use.

So the whole line would be:
fPath = ThisWorkbook.Path & "\" & NewFileName.xls
 
Upvote 0
VBA Code:
Public Sub CopySheet()
    Dim oSht        As Worksheet
    Dim sFullName   As String
    
    sFullName = ThisWorkbook.Path & "\NewBook.xls"   '  <<<<<
    
    With Application
        .ScreenUpdating = False
        ThisWorkbook.Worksheets("Sheet1").Copy       '  <<< change accordingly
        Set oSht = ActiveSheet
        oSht.Cells.Copy
        oSht.Cells(1, 1).PasteSpecial Paste:=xlPasteValues
        .CutCopyMode = False
        oSht.Cells(1, 1).Select
        oSht.Parent.SaveAs Filename:=sFullName, FileFormat:=xlWorkbookNormal
        .ScreenUpdating = True
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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