PC Review


Reply
Thread Tools Rate Thread

How to Copy formats and formulas in vba?

 
 
DK
Guest
Posts: n/a
 
      1st Apr 2009
The following statement copies the formulas in one range to another range of
cells.
How can I also copy the formats of "ExpRow"?
Thank you.

....
r = Range("MtrCounter").Value
c = Range("ExpRow").Columns.Count
With Sheet2
.Range(.Cells(1, 1), .Cells(r, c)) = Range("ExpRow").Formula
End With
....


 
Reply With Quote
 
 
 
 
Rick Rothstein
Guest
Posts: n/a
 
      1st Apr 2009
This will probably work...

r = Range("MtrCounter").Value
c = Range("ExpRow").Columns.Count
With Sheet2
.Range("ExpRow").Copy .Range(.Cells(1, 1), .Cells(r, c))
.Range(.Cells(1, 1), .Cells(r, c)) = Range("ExpRow").Formula
End With

--
Rick (MVP - Excel)


"DK" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> The following statement copies the formulas in one range to another range
> of cells.
> How can I also copy the formats of "ExpRow"?
> Thank you.
>
> ...
> r = Range("MtrCounter").Value
> c = Range("ExpRow").Columns.Count
> With Sheet2
> .Range(.Cells(1, 1), .Cells(r, c)) = Range("ExpRow").Formula
> End With
> ...
>


 
Reply With Quote
 
DK
Guest
Posts: n/a
 
      1st Apr 2009
I found a solution. However, if you can see a better way I'd be glad to see
it.
You guys are so good the answers arrive before you send them.
Thanks again for all you do.
DK

With Sheet2
.Range(.Cells(1, 1), .Cells(r, c)) = Range("ExpRow").Formula
.Range("ExpRow").EntireRow.Copy
.Range(.Cells(1, 1), .Cells(r, c)).PasteSpecial Paste:=xlPasteFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With


"DK" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> The following statement copies the formulas in one range to another range
> of cells.
> How can I also copy the formats of "ExpRow"?
> Thank you.
>
> ...
> r = Range("MtrCounter").Value
> c = Range("ExpRow").Columns.Count
> With Sheet2
> .Range(.Cells(1, 1), .Cells(r, c)) = Range("ExpRow").Formula
> End With
> ...
>



 
Reply With Quote
 
DK
Guest
Posts: n/a
 
      1st Apr 2009
Hey! I didn't know that one could copy from > to in that manner.
Just the one line within the With statment seems to do the entire job.
> .Range("ExpRow").Copy .Range(.Cells(1, 1), .Cells(r, c))

Thanks very much Rick.
DK


"Rick Rothstein" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> This will probably work...
>
> r = Range("MtrCounter").Value
> c = Range("ExpRow").Columns.Count
> With Sheet2
> .Range("ExpRow").Copy .Range(.Cells(1, 1), .Cells(r, c))
> .Range(.Cells(1, 1), .Cells(r, c)) = Range("ExpRow").Formula
> End With
>
> --
> Rick (MVP - Excel)
>
>
> "DK" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> The following statement copies the formulas in one range to another range
>> of cells.
>> How can I also copy the formats of "ExpRow"?
>> Thank you.
>>
>> ...
>> r = Range("MtrCounter").Value
>> c = Range("ExpRow").Columns.Count
>> With Sheet2
>> .Range(.Cells(1, 1), .Cells(r, c)) = Range("ExpRow").Formula
>> End With
>> ...
>>

>



 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      1st Apr 2009
That single line copies the formula, but it puts the same formula in each
cell (all cell references are the same, they don't change in the same way as
when you "copy a formula down")... you need the second line I posted to do
that (it overwrites the "static" formulas with the "copy down" type of
formulas)... I only used the Copy statement to quickly move the format into
the cells where you wanted the formulas.

--
Rick (MVP - Excel)


"DK" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hey! I didn't know that one could copy from > to in that manner.
> Just the one line within the With statment seems to do the entire job.
>> .Range("ExpRow").Copy .Range(.Cells(1, 1), .Cells(r, c))

> Thanks very much Rick.
> DK
>
>
> "Rick Rothstein" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> This will probably work...
>>
>> r = Range("MtrCounter").Value
>> c = Range("ExpRow").Columns.Count
>> With Sheet2
>> .Range("ExpRow").Copy .Range(.Cells(1, 1), .Cells(r, c))
>> .Range(.Cells(1, 1), .Cells(r, c)) = Range("ExpRow").Formula
>> End With
>>
>> --
>> Rick (MVP - Excel)
>>
>>
>> "DK" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> The following statement copies the formulas in one range to another
>>> range of cells.
>>> How can I also copy the formats of "ExpRow"?
>>> Thank you.
>>>
>>> ...
>>> r = Range("MtrCounter").Value
>>> c = Range("ExpRow").Columns.Count
>>> With Sheet2
>>> .Range(.Cells(1, 1), .Cells(r, c)) = Range("ExpRow").Formula
>>> End With
>>> ...
>>>

>>

>
>


 
Reply With Quote
 
DK
Guest
Posts: n/a
 
      1st Apr 2009
Rick,
I don't know if it makes any difference that I'm using xl-2007.
I checked the resulting formulas using just the one line of code and it
seems to be ok.
However I may keep the .Formula line just to make sure.
Here is a small sample using only .Range("ExpRow").Copy .Range(.Cells(1, 1),
..Cells(r, c)).
1st row is part of "ExpRow" next rows follow below.
The first formula is common to all records, the relative formulas refer to
the list of records.
= testby =Input!D16 =Input!E16

= testby =Input!D17 =Input!E17

= testby =Input!D18 =Input!E18

Thanks again.





"Rick Rothstein" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> That single line copies the formula, but it puts the same formula in each
> cell (all cell references are the same, they don't change in the same way
> as when you "copy a formula down")... you need the second line I posted to
> do that (it overwrites the "static" formulas with the "copy down" type of
> formulas)... I only used the Copy statement to quickly move the format
> into the cells where you wanted the formulas.
>
> --
> Rick (MVP - Excel)
>
>
> "DK" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hey! I didn't know that one could copy from > to in that manner.
>> Just the one line within the With statment seems to do the entire job.
>>> .Range("ExpRow").Copy .Range(.Cells(1, 1), .Cells(r, c))

>> Thanks very much Rick.
>> DK
>>
>>
>> "Rick Rothstein" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> This will probably work...
>>>
>>> r = Range("MtrCounter").Value
>>> c = Range("ExpRow").Columns.Count
>>> With Sheet2
>>> .Range("ExpRow").Copy .Range(.Cells(1, 1), .Cells(r, c))
>>> .Range(.Cells(1, 1), .Cells(r, c)) = Range("ExpRow").Formula
>>> End With
>>>
>>> --
>>> Rick (MVP - Excel)
>>>
>>>
>>> "DK" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> The following statement copies the formulas in one range to another
>>>> range of cells.
>>>> How can I also copy the formats of "ExpRow"?
>>>> Thank you.
>>>>
>>>> ...
>>>> r = Range("MtrCounter").Value
>>>> c = Range("ExpRow").Columns.Count
>>>> With Sheet2
>>>> .Range(.Cells(1, 1), .Cells(r, c)) = Range("ExpRow").Formula
>>>> End With
>>>> ...
>>>>
>>>

>>
>>

>



 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      1st Apr 2009
I'm using XL2003 and what I explained was necessary to make it work on my
system.

--
Rick (MVP - Excel)


"DK" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Rick,
> I don't know if it makes any difference that I'm using xl-2007.
> I checked the resulting formulas using just the one line of code and it
> seems to be ok.
> However I may keep the .Formula line just to make sure.
> Here is a small sample using only .Range("ExpRow").Copy .Range(.Cells(1,
> 1), .Cells(r, c)).
> 1st row is part of "ExpRow" next rows follow below.
> The first formula is common to all records, the relative formulas refer to
> the list of records.
> = testby =Input!D16 =Input!E16
>
> = testby =Input!D17 =Input!E17
>
> = testby =Input!D18 =Input!E18
>
> Thanks again.
>
>
>
>
>
> "Rick Rothstein" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> That single line copies the formula, but it puts the same formula in each
>> cell (all cell references are the same, they don't change in the same way
>> as when you "copy a formula down")... you need the second line I posted
>> to do that (it overwrites the "static" formulas with the "copy down" type
>> of formulas)... I only used the Copy statement to quickly move the format
>> into the cells where you wanted the formulas.
>>
>> --
>> Rick (MVP - Excel)
>>
>>
>> "DK" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Hey! I didn't know that one could copy from > to in that manner.
>>> Just the one line within the With statment seems to do the entire job.
>>>> .Range("ExpRow").Copy .Range(.Cells(1, 1), .Cells(r, c))
>>> Thanks very much Rick.
>>> DK
>>>
>>>
>>> "Rick Rothstein" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> This will probably work...
>>>>
>>>> r = Range("MtrCounter").Value
>>>> c = Range("ExpRow").Columns.Count
>>>> With Sheet2
>>>> .Range("ExpRow").Copy .Range(.Cells(1, 1), .Cells(r, c))
>>>> .Range(.Cells(1, 1), .Cells(r, c)) = Range("ExpRow").Formula
>>>> End With
>>>>
>>>> --
>>>> Rick (MVP - Excel)
>>>>
>>>>
>>>> "DK" <(E-Mail Removed)> wrote in message
>>>> news:(E-Mail Removed)...
>>>>> The following statement copies the formulas in one range to another
>>>>> range of cells.
>>>>> How can I also copy the formats of "ExpRow"?
>>>>> Thank you.
>>>>>
>>>>> ...
>>>>> r = Range("MtrCounter").Value
>>>>> c = Range("ExpRow").Columns.Count
>>>>> With Sheet2
>>>>> .Range(.Cells(1, 1), .Cells(r, c)) = Range("ExpRow").Formula
>>>>> End With
>>>>> ...
>>>>>
>>>>
>>>
>>>

>>

>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy column, paste special formulas & number formats doesn't work Beckey Microsoft Excel Misc 3 30th Jul 2009 07:51 PM
Using VB Copy Entire Row but formulas and formats only no values Kenny Microsoft Excel Misc 5 27th Jul 2008 11:12 PM
What's the best way to add a row and copy formulas and formats? =?Utf-8?B?TWljaGFlbCBhdCBTaWdjb24=?= Microsoft Excel Misc 1 17th Mar 2006 02:43 PM
Looking to copy a worksheet with all formats and formulas without coping inputed data God's Kid Microsoft Excel Misc 3 28th Oct 2005 11:59 PM
How to have formulas and formats auto copy to new inserted rows =?Utf-8?B?RGlwcHlEYXdn?= Microsoft Excel Misc 2 5th Aug 2005 03:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:17 AM.