refactor
refactor

Reputation: 15044

copy width and height in addition to data in VBA

Below code copies data(as well as formatting) from source range to target range.

Set RngTemplate = ActiveWorkbook.Worksheets("Template").Range("A1:AD1")
Set RngTarget = ActiveWorkbook.Worksheets("File1").Range("A1:AD1")   
RngTemplate.Copy RngTarget

But I also want the cell widths to be copied to target range, any idea how to implement it.

Upvotes: 1

Views: 573

Answers (1)

SierraOscar
SierraOscar

Reputation: 17637

Use the .PasteSpecial() method

Set RngTemplate = ActiveWorkbook.Worksheets("Template").Range("A1:AD1")
Set RngTarget = ActiveWorkbook.Worksheets("File1").Range("A1:AD1")   
RngTemplate.Copy
RngTarget.PasteSpecial
RngTarget.PasteSpeical xlPasteColumnWidths
Application.CutCopyMode = False

Upvotes: 1

Related Questions