ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How can I copy range's style to another in Excel? (https://www.excelbanter.com/excel-programming/367748-how-can-i-copy-ranges-style-another-excel.html)

ZeroCool127

How can I copy range's style to another in Excel?
 
Hi everybody,

I have a problum that I selected a range on Excel worksheet, and inserted
another range on the same worksheet, now I wanna copy the selected range's
style to the inserted one, I use

insertedRange.Style = selectedRange.Style;

or

insertedRange.Cells.Style = selectedRange.Cells.Style;

both of the two works not correctly, what is the mistake?


--
If you enter this world knowing you are loved and leave this world knowing
the same, then everything that happens in between can be dealt with.

NickHK

How can I copy range's style to another in Excel?
 
Not used .Style before, but upon doing some research:

..Style is an Object, so at the least you would need:
Set insertedRange.Style = selectedRange.Style
However, if you read the Help, you see .Style is read-only.

If you look at the example in Excel VBA Help it show:
<Quote
Worksheets("Sheet1").Range("A1").Style.Name = "Normal"
</Quote
But this errors which #450, indicating it is read-only.

However, if you record a macro of a style change you get something like
Worksheets("Sheet1").Range("A1").Style = "Normal"

Confused ??

What is the value of
Debug.Print insertedRange.Style.Name, selectedRange.Style.Name

And what do you mean by "works not correctly" ?

NickHK

"ZeroCool127" wrote in message
...
Hi everybody,

I have a problum that I selected a range on Excel worksheet, and inserted
another range on the same worksheet, now I wanna copy the selected range's
style to the inserted one, I use

insertedRange.Style = selectedRange.Style;

or

insertedRange.Cells.Style = selectedRange.Cells.Style;

both of the two works not correctly, what is the mistake?


--
If you enter this world knowing you are loved and leave this world knowing
the same, then everything that happens in between can be dealt with.




DoctorG

How can I copy range's style to another in Excel?
 
I don't know what is wrong eith your code but I thought of two options :

1) If you want an exact copy of the range you need the following

Range("MyRange").Copy
[navigate to the new position]
ActiveSheet.Paste
Application.CutCopyMode = False

2) If you don't want the contents but only want the formatting add the next
line to the end of the previous code

Selection.ClearContents

HTH

"ZeroCool127" wrote:

Hi everybody,

I have a problum that I selected a range on Excel worksheet, and inserted
another range on the same worksheet, now I wanna copy the selected range's
style to the inserted one, I use

insertedRange.Style = selectedRange.Style;

or

insertedRange.Cells.Style = selectedRange.Cells.Style;

both of the two works not correctly, what is the mistake?


--
If you enter this world knowing you are loved and leave this world knowing
the same, then everything that happens in between can be dealt with.


DoctorG

How can I copy range's style to another in Excel?
 
Or if the target range already exists copy/paste only the formatting

Range("MyRange").Select
Selection.Copy
Range("MyTarget").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

"ZeroCool127" wrote:

Hi everybody,

I have a problum that I selected a range on Excel worksheet, and inserted
another range on the same worksheet, now I wanna copy the selected range's
style to the inserted one, I use

insertedRange.Style = selectedRange.Style;

or

insertedRange.Cells.Style = selectedRange.Cells.Style;

both of the two works not correctly, what is the mistake?


--
If you enter this world knowing you are loved and leave this world knowing
the same, then everything that happens in between can be dealt with.


NickHK

How can I copy range's style to another in Excel?
 
Is this code in .Net or something ?
Because if it is supposed to be VBA, you do not want the ";" at the end of
the lines.

NickHK

"ZeroCool127" wrote in message
...
Hi everybody,

I have a problum that I selected a range on Excel worksheet, and inserted
another range on the same worksheet, now I wanna copy the selected range's
style to the inserted one, I use

insertedRange.Style = selectedRange.Style;

or

insertedRange.Cells.Style = selectedRange.Cells.Style;

both of the two works not correctly, what is the mistake?


--
If you enter this world knowing you are loved and leave this world knowing
the same, then everything that happens in between can be dealt with.





All times are GMT +1. The time now is 06:42 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com