Question to keep Formatting to another WB via VBA.

trixzy

New Member
Joined
Nov 5, 2014
Messages
4
Okay, I've been searching all morning and I'm having trouble getting this code to work:

Code:
Sub CommandButton1_Click()MyBook = ActiveWorkbook.Name ' Retrieves the name of original WB.
Columns("A:A").AutoFit
Columns("B:B").AutoFit
Columns("C:C").AutoFit
Columns("D:D").AutoFit
Range("A5:D500").Select
Selection.Copy
Workbooks.Add ' Opens my New Workbook
Sheets("Sheet1").Select
Sheets("Sheet1").Range("A1").Select
ActiveSheet.PasteSpecial
ActiveWorkbook.SaveAs Filename:="C:\........" ' Save name of new book
Workbooks(MyBook).Activate ' Back to the original WB
Application.CutCopyMode = False
End Sub

Okay so this code works for me as it should opening a new WB pasting the info and prompting me to save the file.

The only issue here is that from the first WB I have AutoFit the columns A-D and this Formatting does not come over to the next workbook.

I've tried;
Code:
[COLOR=#000000][FONT=Segoe UI]Range("A1").PasteSpecial Paste:=xlFormats
[/FONT][/COLOR]ActiveSheet.PasteSpecial Paste:=xlFormats
Worksheets("Sheet1").Range("A1").PasteSpecial Paste:=xlValues

and multiple other codes but I can't get the Column width to transfer over to the new workbook without breaking the paste function itself.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
How about simply adding
Code:
Range("A:D").EntireColumn.AutoFit
to the end of your code?
 
Upvote 0
Helps if I look at your code! It needs to go here
Code:
Sub CommandButton1_Click()
MyBook = ActiveWorkbook.Name ' Retrieves the name of original WB.
Columns("A:A").AutoFit
Columns("B:B").AutoFit
Columns("C:C").AutoFit
Columns("D:D").AutoFit
Range("A5:D500").Select
Selection.Copy
Workbooks.Add ' Opens my New Workbook
Sheets("Sheet1").Select
Sheets("Sheet1").Range("A1").Select
ActiveSheet.PasteSpecial
[COLOR=#ff0000]Range("A:D").EntireColumn.AutoFit[/COLOR]
ActiveWorkbook.SaveAs Filename:="C:\........" ' Save name of new book
Workbooks(MyBook).Activate ' Back to the original WB
Application.CutCopyMode = False
End Sub
 
Upvote 0
Thanks for your quick reply.
I tried that but the original issue still stands. It copies everything but doesn't still AutoFit on the new workbook it saves. The code does AutoFit the original document though.
 
Upvote 0
Apologies, it should be
Code:
ActiveSheet.Range("A:D").EntireColumn.AutoFit
 
Upvote 0
Okay I think I found the error. I had the macro done from ActiveX Control instead of Form Control and when I made a new button from "Form Control" the Form values are copied to the new workbook! Thank you for your help! =)
 
Upvote 0
Glad you got it sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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