Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Rob
 
Posts: n/a
Default Copy & paste in multiple areas using VBA

I'm trying to select a series of ranges to
1. paste a named formula I've created and then
2. remove the formula & leave the value

What I have so far is shown below, but I'm sure there must be a better way.
The problem seems to be that I can't use the copy function in a multiple
range. I guess I can't paste xlValues to multiple ranges either, so I'm
being forced to handle each range, one at a time, which seems rather
cumbersome.

Sub Macro1()
Range("G8:R45,G50:R59,G63:R110,G114:R121,G114:R122 ,G126:R134").Select
Selection = "=ITNBudgetFormula"
Range("G8:R45").Select
Range("G8:R45").Copy
Selection.PasteSpecial Paste:=xlValues
Range("G50:R59").Select
Range("G50:R59").Copy
Selection.PasteSpecial Paste:=xlValues
'etc for the rest of the range
End Sub


  #2   Report Post  
Duke Carey
 
Posts: n/a
Default

Rob -

One quick way around this is to give you collection of cells a name, say
FormulaRange. Then use code like this instead of wht you've got

Dim rng as Range
Application.Screenupdating = false
For Each rng in Range("FormulaRange")
rng.select
rng.formula = "=ITNBudgetFormula"
rng.Copy
rng.PasteSpecial xlvalues
Next rng
Application.Screenupdating = true

Duke

"Rob" wrote:

I'm trying to select a series of ranges to
1. paste a named formula I've created and then
2. remove the formula & leave the value

What I have so far is shown below, but I'm sure there must be a better way.
The problem seems to be that I can't use the copy function in a multiple
range. I guess I can't paste xlValues to multiple ranges either, so I'm
being forced to handle each range, one at a time, which seems rather
cumbersome.

Sub Macro1()
Range("G8:R45,G50:R59,G63:R110,G114:R121,G114:R122 ,G126:R134").Select
Selection = "=ITNBudgetFormula"
Range("G8:R45").Select
Range("G8:R45").Copy
Selection.PasteSpecial Paste:=xlValues
Range("G50:R59").Select
Range("G50:R59").Copy
Selection.PasteSpecial Paste:=xlValues
'etc for the rest of the range
End Sub



  #4   Report Post  
Duke Carey
 
Posts: n/a
Default

Rob -

While my earlier post contained code for selecting each cell in the group,
VBA code works much faster if you do not select cells. And the fact is that
rarely do you need to select a cell to accomplish your goal.

In this case, you may simply need to create your formula in VBA, using R1C1
references, assign the formula to the range of cells, then convert each cell
to a value like so, which doesn't select ANY cells and work very, very fast

With range("FormulaRange")
.FormulaR1C1 = "=rc[-1]"
For Each cc In range("FormulaRange")
cc.Formula = cc.Value
Next
End With



"Rob" wrote:

I'm trying to select a series of ranges to
1. paste a named formula I've created and then
2. remove the formula & leave the value

What I have so far is shown below, but I'm sure there must be a better way.
The problem seems to be that I can't use the copy function in a multiple
range. I guess I can't paste xlValues to multiple ranges either, so I'm
being forced to handle each range, one at a time, which seems rather
cumbersome.

Sub Macro1()
Range("G8:R45,G50:R59,G63:R110,G114:R121,G114:R122 ,G126:R134").Select
Selection = "=ITNBudgetFormula"
Range("G8:R45").Select
Range("G8:R45").Copy
Selection.PasteSpecial Paste:=xlValues
Range("G50:R59").Select
Range("G50:R59").Copy
Selection.PasteSpecial Paste:=xlValues
'etc for the rest of the range
End Sub



  #5   Report Post  
Don Guillett
 
Posts: n/a
Default

and my method should be even quicker

--
Don Guillett
SalesAid Software

"Duke Carey" wrote in message
...
Rob -

While my earlier post contained code for selecting each cell in the group,
VBA code works much faster if you do not select cells. And the fact is

that
rarely do you need to select a cell to accomplish your goal.

In this case, you may simply need to create your formula in VBA, using

R1C1
references, assign the formula to the range of cells, then convert each

cell
to a value like so, which doesn't select ANY cells and work very, very

fast

With range("FormulaRange")
.FormulaR1C1 = "=rc[-1]"
For Each cc In range("FormulaRange")
cc.Formula = cc.Value
Next
End With



"Rob" wrote:

I'm trying to select a series of ranges to
1. paste a named formula I've created and then
2. remove the formula & leave the value

What I have so far is shown below, but I'm sure there must be a better

way.
The problem seems to be that I can't use the copy function in a multiple
range. I guess I can't paste xlValues to multiple ranges either, so I'm
being forced to handle each range, one at a time, which seems rather
cumbersome.

Sub Macro1()

Range("G8:R45,G50:R59,G63:R110,G114:R121,G114:R122 ,G126:R134").Select
Selection = "=ITNBudgetFormula"
Range("G8:R45").Select
Range("G8:R45").Copy
Selection.PasteSpecial Paste:=xlValues
Range("G50:R59").Select
Range("G50:R59").Copy
Selection.PasteSpecial Paste:=xlValues
'etc for the rest of the range
End Sub







  #6   Report Post  
Duke Carey
 
Posts: n/a
Default

True

"Don Guillett" wrote:

and my method should be even quicker

--
Don Guillett
SalesAid Software

"Duke Carey" wrote in message
...
Rob -

While my earlier post contained code for selecting each cell in the group,
VBA code works much faster if you do not select cells. And the fact is

that
rarely do you need to select a cell to accomplish your goal.

In this case, you may simply need to create your formula in VBA, using

R1C1
references, assign the formula to the range of cells, then convert each

cell
to a value like so, which doesn't select ANY cells and work very, very

fast

With range("FormulaRange")
.FormulaR1C1 = "=rc[-1]"
For Each cc In range("FormulaRange")
cc.Formula = cc.Value
Next
End With



"Rob" wrote:

I'm trying to select a series of ranges to
1. paste a named formula I've created and then
2. remove the formula & leave the value

What I have so far is shown below, but I'm sure there must be a better

way.
The problem seems to be that I can't use the copy function in a multiple
range. I guess I can't paste xlValues to multiple ranges either, so I'm
being forced to handle each range, one at a time, which seems rather
cumbersome.

Sub Macro1()

Range("G8:R45,G50:R59,G63:R110,G114:R121,G114:R122 ,G126:R134").Select
Selection = "=ITNBudgetFormula"
Range("G8:R45").Select
Range("G8:R45").Copy
Selection.PasteSpecial Paste:=xlValues
Range("G50:R59").Select
Range("G50:R59").Copy
Selection.PasteSpecial Paste:=xlValues
'etc for the rest of the range
End Sub






  #7   Report Post  
Rob
 
Posts: n/a
Default

Thanks Don and Duke. And the winner is......

I really appreciate your input and will trial both to see what works best in
my situation. Thanks for spending time to provide the best solution!

Rob

"Duke Carey" wrote in message
...
True

"Don Guillett" wrote:

and my method should be even quicker

--
Don Guillett
SalesAid Software

"Duke Carey" wrote in message
...
Rob -

While my earlier post contained code for selecting each cell in the
group,
VBA code works much faster if you do not select cells. And the fact is

that
rarely do you need to select a cell to accomplish your goal.

In this case, you may simply need to create your formula in VBA, using

R1C1
references, assign the formula to the range of cells, then convert each

cell
to a value like so, which doesn't select ANY cells and work very, very

fast

With range("FormulaRange")
.FormulaR1C1 = "=rc[-1]"
For Each cc In range("FormulaRange")
cc.Formula = cc.Value
Next
End With



"Rob" wrote:

I'm trying to select a series of ranges to
1. paste a named formula I've created and then
2. remove the formula & leave the value

What I have so far is shown below, but I'm sure there must be a
better

way.
The problem seems to be that I can't use the copy function in a
multiple
range. I guess I can't paste xlValues to multiple ranges either, so
I'm
being forced to handle each range, one at a time, which seems rather
cumbersome.

Sub Macro1()

Range("G8:R45,G50:R59,G63:R110,G114:R121,G114:R122 ,G126:R134").Select
Selection = "=ITNBudgetFormula"
Range("G8:R45").Select
Range("G8:R45").Copy
Selection.PasteSpecial Paste:=xlValues
Range("G50:R59").Select
Range("G50:R59").Copy
Selection.PasteSpecial Paste:=xlValues
'etc for the rest of the range
End Sub








  #8   Report Post  
Rob
 
Posts: n/a
Default

Back again!

I thought it was working OK but the formula I used from Don is doing some
strange things I can't resolve.
The modified formula I'm using is:

Sub SetFormula()
Set frng = Range("G8:R45,G50:R59,G63:R110,G114:R121,G114:R122 ,G126:R134")
Range("G8:R45,G50:R59,G63:R110,G114:R121,G114:R122 ,G126:R134") =
"=ITNBudgetFormula"
With frng
.Formula = .Value
End With
End Sub

However, although most of it works throughout the ranges, there is one
section that, as soon as the .Formula = .Value part executes, returns a
#N/A error (within the range G63:R110, namely G101:R110). Furthermore, some
of the data in other blocks of ranges show blank whereas all the others show
0 when all the data should be 0 because there is none yet. In fact,
immediately prior to executing the .Formula = .Value, all the cells show 0.
But as soon as .Formula = .Value executes these strange things happen.

Any ideas?

Rob






"Rob" <NA wrote in message ...
Thanks Don and Duke. And the winner is......

I really appreciate your input and will trial both to see what works best
in my situation. Thanks for spending time to provide the best solution!

Rob

"Duke Carey" wrote in message
...
True

"Don Guillett" wrote:

and my method should be even quicker

--
Don Guillett
SalesAid Software

"Duke Carey" wrote in message
...
Rob -

While my earlier post contained code for selecting each cell in the
group,
VBA code works much faster if you do not select cells. And the fact
is
that
rarely do you need to select a cell to accomplish your goal.

In this case, you may simply need to create your formula in VBA, using
R1C1
references, assign the formula to the range of cells, then convert
each
cell
to a value like so, which doesn't select ANY cells and work very, very
fast

With range("FormulaRange")
.FormulaR1C1 = "=rc[-1]"
For Each cc In range("FormulaRange")
cc.Formula = cc.Value
Next
End With



"Rob" wrote:

I'm trying to select a series of ranges to
1. paste a named formula I've created and then
2. remove the formula & leave the value

What I have so far is shown below, but I'm sure there must be a
better
way.
The problem seems to be that I can't use the copy function in a
multiple
range. I guess I can't paste xlValues to multiple ranges either, so
I'm
being forced to handle each range, one at a time, which seems rather
cumbersome.

Sub Macro1()

Range("G8:R45,G50:R59,G63:R110,G114:R121,G114:R122 ,G126:R134").Select
Selection = "=ITNBudgetFormula"
Range("G8:R45").Select
Range("G8:R45").Copy
Selection.PasteSpecial Paste:=xlValues
Range("G50:R59").Select
Range("G50:R59").Copy
Selection.PasteSpecial Paste:=xlValues
'etc for the rest of the range
End Sub










Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't Copy and Paste or Paste Special between Excel Workbooks wllee Excel Discussion (Misc queries) 5 April 29th 23 03:43 AM
Can't Copy and Paste between Excel 2003 Workbooks wllee Excel Discussion (Misc queries) 6 March 30th 05 02:59 PM
Copy and Paste and keep format the same Brian Caraher Excel Discussion (Misc queries) 1 March 17th 05 02:05 PM
Copy & Paste Brian Keanie Excel Discussion (Misc queries) 1 February 5th 05 11:56 AM
copy paste cell character limit Fred Excel Discussion (Misc queries) 1 December 2nd 04 08:58 PM


All times are GMT +1. The time now is 02:30 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"