Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 408
Default Looping through ranges

Hi ALL,

Ive got this simplified macro which copies data from Range C7:G2000
and pastes it in n other ranges in the same worksheet:

Dim n As Integer, i As Integer
n = Range("L4")
For i = 1 To n
Range("B4") = Cells(7 + i, 11)
€˜do something
Cells(6, 5 * i + 7) = Cells(7 + i, 11)
Range("C7:G2000").Select
Selection.Copy
Cells(7, 5 * i + 7).Select
ActiveSheet.Paste
Cells(6, 5 * i + 7).Select
€˜do something
Next i

Now Im trying to create a macro which does the opposite: Copy Data from
range L7:P2000 ïƒ*Paste the copied data in C8ïƒ*do somethingïƒ*Copy Data from
C7:G2001 and Paste it back in L7. Then Copy Data from the next 5 columns
range Q7:U2000 ïƒ*Paste the copied data in C8ïƒ*do somethingïƒ*Copy Data from
C7:G2000 and Paste back in Q7 and so on n times. As you can see from the
above code n = Range("L4").

Any help is highly appreciated as always.

Tim

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default Looping through ranges

Hi Tim

I'm a bit confused, first you want to copy data from C7:G2001, next time
from C7:G2000. In my solution I assume that you want to copy from C7:G2000
every time.

This should do it:

Sub test1()
Dim n As Long
Dim i As Long
Dim cOff As Long
Dim CopyRange As Range
Dim TargetCell As Range

n = Range("L4").Value
cOff = 0

For i = 1 To n
Range("L7:P2000").Offset(0, cOff).Copy _
Destination:=Range("C8")
'Do things
Range("C7:G2000").Copy Range("L7").Offset(0, cOff)
cOff = cOff + 5
Next
End Sub

Regards,
Per

"Tim" skrev i meddelelsen
...
Hi ALL,

Ive got this simplified macro which copies data from Range C7:G2000
and pastes it in n other ranges in the same worksheet:

Dim n As Integer, i As Integer
n = Range("L4")
For i = 1 To n
Range("B4") = Cells(7 + i, 11)
€˜do something
Cells(6, 5 * i + 7) = Cells(7 + i, 11)
Range("C7:G2000").Select
Selection.Copy
Cells(7, 5 * i + 7).Select
ActiveSheet.Paste
Cells(6, 5 * i + 7).Select
€˜do something
Next i

Now Im trying to create a macro which does the opposite: Copy Data from
range L7:P2000 ïƒ*Paste the copied data in C8ïƒ*do somethingïƒ*Copy Data from
C7:G2001 and Paste it back in L7. Then Copy Data from the next 5 columns
range Q7:U2000 ïƒ*Paste the copied data in C8ïƒ*do somethingïƒ*Copy Data from
C7:G2000 and Paste back in Q7 and so on n times. As you can see from the
above code n = Range("L4").

Any help is highly appreciated as always.

Tim


  #3   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 408
Default Looping through ranges

Works great.
Thank you Per!!!








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
LOOPING multiple ranges Jase Excel Discussion (Misc queries) 1 April 7th 08 06:21 PM
looping through an array of ranges Arnold Klapheck Excel Programming 1 September 28th 06 06:46 PM
Looping through named ranges JFamilo[_12_] Excel Programming 2 August 17th 05 02:23 PM
Looping through named ranges Tim[_38_] Excel Programming 1 May 10th 04 07:20 PM
Looping through Ranges of Rows MS News Excel Programming 6 December 9th 03 06:13 AM


All times are GMT +1. The time now is 06:50 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"