PC Review


Reply
Thread Tools Rate Thread

Copy & paste ranges dependant on results from another calculation

 
 
=?Utf-8?B?QW5keU9E?=
Guest
Posts: n/a
 
      21st Nov 2006
I am trying to set up asheet which will dynamically copy & paste formatted
cells a number of times. However this process will change depending on the
results of a separate calculation;

So far I can calculate the number of iterations and display the result in a
cell, I now need to select the range of cells to be copied e.g. range
("A1:C5") and copy this range 'n' times adjacent to my originating block.

If I look at the code from a recorded macro the code reads as follows:
--
range("A1:C5").Select
Selection.Copy
range("D1").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
range("G1").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

How can I refer to my calculated result in order to set the macro to
automatically PasteSpecial 'n' number of times?
--
Cheers,

Andy
 
Reply With Quote
 
 
 
 
=?Utf-8?B?QWxvaw==?=
Guest
Posts: n/a
 
      21st Nov 2006
Try this.

Sub Test()
Dim n%, i%

'Assuming the number of times the pasting is to be done is in Cell A6
n = Cells(6, 1).Value

If n > 84 Then n = 84

Range("A1:C5").Select
Selection.Copy

For i = 1 To n
Cells(1, 1 + i * 3).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Next i

End Sub


"AndyOD" wrote:

> I am trying to set up asheet which will dynamically copy & paste formatted
> cells a number of times. However this process will change depending on the
> results of a separate calculation;
>
> So far I can calculate the number of iterations and display the result in a
> cell, I now need to select the range of cells to be copied e.g. range
> ("A1:C5") and copy this range 'n' times adjacent to my originating block.
>
> If I look at the code from a recorded macro the code reads as follows:
> --
> range("A1:C5").Select
> Selection.Copy
> range("D1").Select
> Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
> SkipBlanks:=False, Transpose:=False
> Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
> SkipBlanks:=False, Transpose:=False
> range("G1").Select
> Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
> SkipBlanks:=False, Transpose:=False
> Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
> SkipBlanks:=False, Transpose:=False
>
> How can I refer to my calculated result in order to set the macro to
> automatically PasteSpecial 'n' number of times?
> --
> Cheers,
>
> Andy

 
Reply With Quote
 
=?Utf-8?B?QW5keU9E?=
Guest
Posts: n/a
 
      21st Nov 2006
Alok,

Thank you for this. It is exactly what I was looking for. Brilliant!
--
Cheers,

Andy


"Alok" wrote:

> Try this.
>
> Sub Test()
> Dim n%, i%
>
> 'Assuming the number of times the pasting is to be done is in Cell A6
> n = Cells(6, 1).Value
>
> If n > 84 Then n = 84
>
> Range("A1:C5").Select
> Selection.Copy
>
> For i = 1 To n
> Cells(1, 1 + i * 3).Select
> Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
> SkipBlanks:=False, Transpose:=False
> Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
> SkipBlanks:=False, Transpose:=False
> Next i
>
> End Sub
>
>
> "AndyOD" wrote:
>
> > I am trying to set up asheet which will dynamically copy & paste formatted
> > cells a number of times. However this process will change depending on the
> > results of a separate calculation;
> >
> > So far I can calculate the number of iterations and display the result in a
> > cell, I now need to select the range of cells to be copied e.g. range
> > ("A1:C5") and copy this range 'n' times adjacent to my originating block.
> >
> > If I look at the code from a recorded macro the code reads as follows:
> > --
> > range("A1:C5").Select
> > Selection.Copy
> > range("D1").Select
> > Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
> > SkipBlanks:=False, Transpose:=False
> > Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
> > SkipBlanks:=False, Transpose:=False
> > range("G1").Select
> > Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
> > SkipBlanks:=False, Transpose:=False
> > Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
> > SkipBlanks:=False, Transpose:=False
> >
> > How can I refer to my calculated result in order to set the macro to
> > automatically PasteSpecial 'n' number of times?
> > --
> > Cheers,
> >
> > Andy

 
Reply With Quote
 
Doug Glancy
Guest
Posts: n/a
 
      21st Nov 2006
Andy,

Try this:

Sub paste_n_times()
Dim copy_range As Range
Dim n As Long

Set copy_range = Range("A1:C5")
n = 3 'replace with your calc

With copy_range
.Copy
With .Resize(.Rows.Count, .Columns.Count * (n + 1))
.PasteSpecial Paste:=xlPasteFormulas
.PasteSpecial Paste:=xlPasteFormats
End With
End With
End Sub


hth,

Doug


"AndyOD" <(E-Mail Removed)> wrote in message
news:C5013E52-58E0-47EB-B681-(E-Mail Removed)...
>I am trying to set up asheet which will dynamically copy & paste formatted
> cells a number of times. However this process will change depending on the
> results of a separate calculation;
>
> So far I can calculate the number of iterations and display the result in
> a
> cell, I now need to select the range of cells to be copied e.g. range
> ("A1:C5") and copy this range 'n' times adjacent to my originating block.
>
> If I look at the code from a recorded macro the code reads as follows:
> --
> range("A1:C5").Select
> Selection.Copy
> range("D1").Select
> Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
> SkipBlanks:=False, Transpose:=False
> Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
> SkipBlanks:=False, Transpose:=False
> range("G1").Select
> Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
> SkipBlanks:=False, Transpose:=False
> Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
> SkipBlanks:=False, Transpose:=False
>
> How can I refer to my calculated result in order to set the macro to
> automatically PasteSpecial 'n' number of times?
> --
> Cheers,
>
> Andy



 
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
How can I copy big ranges of cells without drag or copy/paste? Ricardo Julio Microsoft Excel Misc 3 23rd Mar 2010 02:38 PM
Copy/Paste dynamic ranges kevin Microsoft Excel Programming 1 28th May 2009 09:06 PM
Copy/Paste dynamic ranges kevin Microsoft Excel Programming 1 28th May 2009 07:14 PM
Copy-Paste ever changing ranges Sandy Microsoft Excel Discussion 3 18th Nov 2007 10:36 AM
copy paste different ranges Khalil Handal Microsoft Excel Discussion 3 16th Aug 2007 06:07 PM


Features
 

Advertising
 

Newsgroups
 


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