VBA to copy worksheets and keep merged cells merged

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
683
Office Version
  1. 2016
Platform
  1. Windows
I have the following VBA which creates separate files for each worksheet in a workbook and it works fine, however, if any cells are merged in the worksheets the merging is removed when the new file is produced. Is there some code I can put in to keep the merging in tact when the new files are produced? Many thanks.

VBA Code:
MyPath = ThisWorkbook.Path
    For Each sht In ThisWorkbook.Sheets
    sht.Copy
    ActiveSheet.Cells.Copy
    ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
    ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
    ActiveWorkbook.SaveAs _
    Filename:=MyPath & "\" & "Project - " & sht.Name & ".xlsx"
    ActiveWorkbook.Close savechanges:=False
    Next sht
  
    Range("F3").Select
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You might consider the following...

VBA Code:
MyPath = ThisWorkbook.Path
For Each sht In ThisWorkbook.Sheets
    sht.Copy
    ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
    ActiveWorkbook.SaveAs Filename:=MyPath & "\" & "Project - " & sht.Name & ".xlsx"
    ActiveWorkbook.Close savechanges:=False
Next sht

Cheers,

Tony
 
Upvote 0
Solution
You might consider the following...

VBA Code:
MyPath = ThisWorkbook.Path
For Each sht In ThisWorkbook.Sheets
    sht.Copy
    ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
    ActiveWorkbook.SaveAs Filename:=MyPath & "\" & "Project - " & sht.Name & ".xlsx"
    ActiveWorkbook.Close savechanges:=False
Next sht

Cheers,

Tony
Many thanks for this solution :)
 
Upvote 0
You're very welcome.
Please remember to checkmark the question as Solved. Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

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