Copying cell formatting using VBA

petereddy

New Member
Joined
Feb 6, 2014
Messages
43
Hi,

Two questions about copying ranges:

First, the command
VBA Code:
Range(Cells(1, 1), Cells(1, 3)) = Range(Cells(2, 1), Cells(2, 3))
seems to do nothing, but transposing the range twice, as done here
VBA Code:
Range(Cells(1, 1), Cells(1, 3)) = Application.Transpose(Application.Transpose(Range(Cells(2, 1), Cells(2, 3))))
does what I expected the first line to do. Any thoughts how to tighten up the code?

Second, is there a simple way to copy the NumberFormat of each cell, similar to how the second line above copies the values? For example if the NumberFormat in A2 is "0%", in B2 is "#,##0.00" and in C2 is "mmmm d, yyyy", is there a way to apply that to, say, A1:C1? Right now I'm looping through each cell in the range to get its NumberFormat, and applying it to the corresponding cell. Is that my best bet? Thanks.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
The part in red does what you're code is trying to do & the part in blue copies the formats & values
Rich (BB code):
   Range("A1:C1").Value = Range("A2:C2").Value
   
   Range("A2:C2").Copy
   Range("A1:C1").PasteSpecial xlValues
   Range("A1:C1").PasteSpecial xlFormats
 
Upvote 0
Yes,
VBA Code:
  Range("A2:C2").Copy Range("A1")
but that copies everything.
 
Upvote 0
AFAIK it by-passes the clipboard & copies the cells directly.
 
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,307
Members
449,218
Latest member
Excel Master

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