Column width in VBA

Wongjim

New Member
Joined
Jul 30, 2018
Messages
3
Hi
I have a VBA script were a range of cells are saved in a new xlsx file. The VBA script works fine on only one thing.
How do I save also the column width and row height from the original file to the saved file?

At this moment I have this script:

Option Explicit


Sub SaveXLSX()
Dim Filename As Variant
Dim Wb As Workbook
Dim Source As Range, Dest As Range

With Blad1
'Refer to the data cells
Set Source = .Range("A1:K10")
'Build the file name (same result as your code, just to show another way)
'Filename = Join(Array(.Range("P29"), .Range("P30"), .Range("P31")))
Filename = Range("A2")
End With
'Ask the user
Filename = Application.GetSaveAsFilename(Filename, "Excelfile (*.xlsx), *.xlsx")
'Aborted?
If VarType(Filename) = vbBoolean Then Exit Sub
'Create a new file
Set Wb = Workbooks.Add(xlWBATWorksheet)
'Refer to the destination cell
Set Dest = Wb.Sheets(1).Range("A1")

'Copy the cells
Source.Copy Dest
'Alternatively to remove the formulas if any:
'Source.Copy
'Dest.PasteSpecial xlPasteValuesAndNumberFormats

'Save the file
Wb.SaveAs Filename
'Application.ScreenUpdating = True
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi & welcome to MrExcel.
Are the column widths & row heights all the same, or can they vary?
 
Upvote 0
Hi Fluff,
They vary
Columns were the width and height reply to are "A1" tot "G71"

Thanks for helping out...
 
Upvote 0
Try
Code:
Sub SaveXLSX()
Dim Filename As Variant
Dim Wb As Workbook
Dim Source As Range, Dest As Range
[COLOR=#0000ff]Dim Col As Range, Rw As Range[/COLOR]

With Sheet3
'Refer to the data cells
Set Source = .Range("A1:K10")
'Build the file name (same result as your code, just to show another way)
'Filename = Join(Array(.Range("P29"), .Range("P30"), .Range("P31")))
Filename = Range("A2")
End With
'Ask the user
'Filename = Application.GetSaveAsFilename(Filename, "Excelfile (*.xlsx), *.xlsx")
'Aborted?
If VarType(Filename) = vbBoolean Then Exit Sub
'Create a new file
Set Wb = Workbooks.Add(xlWBATWorksheet)
'Refer to the destination cell
Set Dest = Wb.Sheets(1).Range("A1")

'Copy the cells
Source.Copy Dest
[COLOR=#0000ff]For Each Col In Source.Columns
   Dest.Offset(, Col.Column - 1).ColumnWidth = Col.ColumnWidth
Next Col
For Each Rw In Source.Rows
   Dest.Offset(Rw.Row - 1).RowHeight = Rw.Height
Next Rw[/COLOR]
'Alternatively to remove the formulas if any:
'Source.Copy
'Dest.PasteSpecial xlPasteValuesAndNumberFormats

'Save the file
Wb.SaveAs Filename
'Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks Fluff
Worked like a charm!

how the heck did you find the answer that quick? I search the forums before registering for days!!!

BR
Jim
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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