Copy sheet to new workbook with formating

KreshBell

New Member
Joined
Jan 13, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have VB, which copies the sheet to the new workbook in the same folder, the name of the new file is the same as the source and an extension has been added, which is the name of the sheet I am copying. Everything works ok but I would like it to copy the format as well, and it doesn't work.

Can anyone help me?

VBA Code:
Sub SheetCopyRename()


    Dim mySourceWB As Workbook
    Dim mySourceSheet As Worksheet
    Dim myDestWB As Workbook
    Dim myNewFileName As String
    
'   First capture current workbook and worksheet
    Set mySourceWB = ActiveWorkbook
    Set mySourceSheet = ActiveSheet


'   Build new file name based
    Filename = ActiveWorkbook.Name
    If InStr(Filename, ".") > 0 Then
    Filename = Left(Filename, InStr(Filename, ".") - 1)
    End If
    myNewFileName = mySourceWB.Path & "\" & Filename & "_" & mySourceSheet.Name & ".xlsx"


'   Add new workbook and save with name of sheet from other file
    Workbooks.Add
    ActiveWorkbook.SaveAs Filename:=myNewFileName
    Set myDestWB = ActiveWorkbook
    
'   Copy over sheet from previous file
    mySourceWB.Activate
    Cells.Copy
    myDestWB.Activate
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    
'   Resave new workbook
    ActiveWorkbook.Save


End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try below

VBA Code:
Sub SheetCopyRename()


    Dim mySourceWB As Workbook
    Dim mySourceSheet As Worksheet
    Dim myDestWB As Workbook
    Dim myNewFileName As String
   
'   First capture current workbook and worksheet
    Set mySourceWB = ActiveWorkbook
    Set mySourceSheet = ActiveSheet


'   Build new file name based
    Filename = ActiveWorkbook.Name
    If InStr(Filename, ".") > 0 Then
    Filename = Left(Filename, InStr(Filename, ".") - 1)
    End If
    myNewFileName = mySourceWB.Path & "\" & Filename & "_" & mySourceSheet.Name & ".xlsx"


'   Add new workbook and save with name of sheet from other file
    Workbooks.Add
    ActiveWorkbook.SaveAs Filename:=myNewFileName
    Set myDestWB = ActiveWorkbook
   
'   Copy over sheet from previous file
    mySourceWB.Activate
    Cells.Copy
    myDestWB.Activate
    Range("A1").Select
   Selection.PasteSpecial Paste:=xlPasteValues  
    Selection.PasteSpecial Paste:=xlPasteFormats
    Application.CutCopyMode = False
   
'   Resave new workbook
    ActiveWorkbook.Save


End Sub
 
Upvote 0
to the left is the source, to the right is the copy. not copied formatting

2022-01-13_08-29-42 cp.jpg
 
Upvote 0
Row height is different, thumbnails are missing

You then need to specify that particular row . Secondly thumbnails are the object . May be someone else will be able to help you out in this.
 
Upvote 0
*KreshBell,

Your original code worked for me, ie, it included colors, row heights and thumbnails. Clearly though it's not working for you. You might consider the following approach...

VBA Code:
Sub SheetCopyRename()

Dim mySourceWB As Workbook
Dim mySourceSheet As Worksheet
Dim myNewFileName As String
Dim fName As String

'   First capture current workbook and worksheet
Set mySourceWB = ActiveWorkbook
Set mySourceSheet = ActiveSheet

'   Build new file name based
fName = ActiveWorkbook.Name
If InStr(fName, ".") > 0 Then fName = Left(fName, InStr(fName, ".") - 1)
myNewFileName = mySourceWB.Path & "\" & fName & "_" & mySourceSheet.Name & ".xlsx"

'   Copy over sheet from previous file
mySourceSheet.Copy
ActiveWorkbook.SaveAs FileName:=myNewFileName
End Sub

Cheers,

Tony
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,219
Members
453,024
Latest member
Wingit77

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