Unsure of Loop

Davebro

Board Regular
Joined
Feb 22, 2018
Messages
120
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
I am looking to find a loop through my macro.

Sub Macro1()
'


'
ActiveCell.Select
Selection.Copy
ActiveCell.Offset(8, -1).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
ActiveCell.Select
Selection.End(xlDown).Select
ActiveCell.Offset(4, 1).Range("A1").Select

End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I asked the same question but have figured it out....

Try this (Will Loop the Macro 100 Times so change the To number to suit your needs...)

Sub Macro1()
'


'
Dim Index As Integer

For Index = 1 To 100
ActiveCell.Select
Selection.Copy
ActiveCell.Offset(8, -1).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
ActiveCell.Select
Selection.End(xlDown).Select
ActiveCell.Offset(4, 1).Range("A1").Select
Next

End Sub
 
Upvote 0
I asked the same question but have figured it out....

Try this (Will Loop the Macro 100 Times so change the To number to suit your needs...)

Sub Macro1()
'


'
Dim Index As Integer

For Index = 1 To 100
ActiveCell.Select
Selection.Copy
ActiveCell.Offset(8, -1).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
ActiveCell.Select
Selection.End(xlDown).Select
ActiveCell.Offset(4, 1).Range("A1").Select
Next

End Sub
Regret this goes to the last row in the spreadsheet with
debug error

ActiveCell.Offset(4, 1).Range("A1").Select
 
Upvote 0
Ah, shame...

It works for what I needed....the fact that it does that doesn't affect what I wanted from it...

You would probably need to use the "While" loop then maybe which uses a condition.
 
Upvote 0
Davebro,

Can you explain in more detail exactly what you are trying to do?
What is the structure of your data?
Where should the loop start, and when should it stop?

Note that while you cannot attach spreadsheets to posts, you can post images, if you feel that would be helpful in explaining your problem.
See this here for details on that: XL2BB - Excel Range to BBCode
 
Upvote 0
Davebro,

Can you explain in more detail exactly what you are trying to do?
What is the structure of your data?
Where should the loop start, and when should it stop?

Note that while you cannot attach spreadsheets to posts, you can post images, if you feel that would be helpful in explaining your problem.
See this here for details on that: XL2BB - Excel Range to BBCode

This macro is the first of two I use to sort data so I can use a pivot table.
Macro 1 starts at ActiveCell B4 and transfers the data in that cell to A12 and the variable end of that section
It then goes down to next equivalent of B4, the gap is always the same as between B4 and A12. 8 rows.

ActiveCell.Select
Selection.Copy
ActiveCell.Offset(8, -1).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
ActiveCell.Select
Selection.End(xlDown).Select
ActiveCell.Offset(4, 1).Range("A1").Select
 
Upvote 0
It then goes down to next equivalent of B4, the gap is always the same as between B4 and A12. 8 rows.
I am not sure what you mean by "next equivalent".
I think this is where seeing an actual example may be really helpful, since you seem to be having trouble explaining it.
 
Upvote 0
Book1
ABC
1
2Data 1Data 1
3Data 1Data 1
4Data 1ITEM 1
5Data 1Data 1
6Data 1Data 1
7Data 1Data 1
8Data 1Data 1
9Data 1Data 1
10Data 1
11DataData 1Data 1
121Data 1Data 1
132Data 1Data 1
143Data 1Data 1
154Data 1Data 1
165Data 1Data 1
176Data 1Data 1
187Data 1Data 1
198Data 1Data 1
209Data 1Data 1
2110Data 1Data 1
2211Data 1Data 1
2312Data 1Data 1
24
25Data 2Data 2
26Data 2Data 2
27Data 2ITEM 2
28Data 2Data 2
29Data 2Data 2
30Data 2Data 2
31Data 2Data 2
32Data 2Data 2
33Data 2
34Data 2Data 2Data 2
351Data 2Data 2
362Data 2Data 2
373Data 2Data 2
384Data 2Data 2
395Data 2Data 2
406Data 2Data 2
417Data 2Data 2
428Data 2Data 2
439Data 2Data 2
4410Data 2Data 2
45
46Data 3Data 3
47Data 3Data 3
48Data 3ITEM 3
49Data 3Data 3
50Data 3Data 3
51Data 3Data 3
52Data 3Data 3
53Data 3Data 3
54Data 3
55Data 3Data 3Data 3
561Data 3Data 3
572Data 3Data 3
583Data 3Data 3
594Data 3Data 3
60
61
62After
63
64Data 1Data 1
65Data 1Data 1
66Data 1ITEM 1
67Data 1Data 1
68Data 1Data 1
69Data 1Data 1
70Data 1Data 1
71Data 1Data 1
72Data 1
73DataData 1Data 1
74ITEM 1Data 1Data 1
75ITEM 1Data 1Data 1
76ITEM 1Data 1Data 1
77ITEM 1Data 1Data 1
78ITEM 1Data 1Data 1
79ITEM 1Data 1Data 1
80ITEM 1Data 1Data 1
81ITEM 1Data 1Data 1
82ITEM 1Data 1Data 1
83ITEM 1Data 1Data 1
84ITEM 1Data 1Data 1
85ITEM 1Data 1Data 1
86
87Data 2Data 2
88Data 2Data 2
89Data 2ITEM 2
90Data 2Data 2
91Data 2Data 2
92Data 2Data 2
93Data 2Data 2
94Data 2Data 2
95Data 2
96Data 2Data 2Data 2
97ITEM 2Data 2Data 2
98ITEM 2Data 2Data 2
99ITEM 2Data 2Data 2
100ITEM 2Data 2Data 2
101ITEM 2Data 2Data 2
102ITEM 2Data 2Data 2
103ITEM 2Data 2Data 2
104ITEM 2Data 2Data 2
105ITEM 2Data 2Data 2
106ITEM 2Data 2Data 2
107
108Data 3Data 3
109Data 3Data 3
110Data 3ITEM 3
111Data 3Data 3
112Data 3Data 3
113Data 3Data 3
114Data 3Data 3
115Data 3Data 3
116Data 3
117Data 3Data 3Data 3
118ITEM 3Data 3Data 3
119ITEM 3Data 3Data 3
120ITEM 3Data 3Data 3
121ITEM 3Data 3Data 3
Sheet1
 
Upvote 0
OK, based on your example posted, can you explain exactly what is supposed to happen?
 
Upvote 0
Book1
ABC
1
2Data 1Data 1
3Data 1Data 1
4Data 1ITEM 1
5Data 1Data 1
6Data 1Data 1
7Data 1Data 1
8Data 1Data 1
9Data 1Data 1
10Data 1
11DataData 1Data 1
121Data 1Data 1
132Data 1Data 1
143Data 1Data 1
154Data 1Data 1
165Data 1Data 1
176Data 1Data 1
187Data 1Data 1
198Data 1Data 1
209Data 1Data 1
2110Data 1Data 1
2211Data 1Data 1
2312Data 1Data 1
24
25Data 2Data 2
26Data 2Data 2
27Data 2ITEM 2
28Data 2Data 2
29Data 2Data 2
30Data 2Data 2
31Data 2Data 2
32Data 2Data 2
33Data 2
34Data 2Data 2Data 2
351Data 2Data 2
362Data 2Data 2
373Data 2Data 2
384Data 2Data 2
395Data 2Data 2
406Data 2Data 2
417Data 2Data 2
428Data 2Data 2
439Data 2Data 2
4410Data 2Data 2
45
46Data 3Data 3
47Data 3Data 3
48Data 3ITEM 3
49Data 3Data 3
50Data 3Data 3
51Data 3Data 3
52Data 3Data 3
53Data 3Data 3
54Data 3
55Data 3Data 3Data 3
561Data 3Data 3
572Data 3Data 3
583Data 3Data 3
594Data 3Data 3
60
61
62After
63
64Data 1Data 1
65Data 1Data 1
66Data 1ITEM 1
67Data 1Data 1
68Data 1Data 1
69Data 1Data 1
70Data 1Data 1
71Data 1Data 1
72Data 1
73DataData 1Data 1
74ITEM 1Data 1Data 1
75ITEM 1Data 1Data 1
76ITEM 1Data 1Data 1
77ITEM 1Data 1Data 1
78ITEM 1Data 1Data 1
79ITEM 1Data 1Data 1
80ITEM 1Data 1Data 1
81ITEM 1Data 1Data 1
82ITEM 1Data 1Data 1
83ITEM 1Data 1Data 1
84ITEM 1Data 1Data 1
85ITEM 1Data 1Data 1
86
87Data 2Data 2
88Data 2Data 2
89Data 2ITEM 2
90Data 2Data 2
91Data 2Data 2
92Data 2Data 2
93Data 2Data 2
94Data 2Data 2
95Data 2
96Data 2Data 2Data 2
97ITEM 2Data 2Data 2
98ITEM 2Data 2Data 2
99ITEM 2Data 2Data 2
100ITEM 2Data 2Data 2
101ITEM 2Data 2Data 2
102ITEM 2Data 2Data 2
103ITEM 2Data 2Data 2
104ITEM 2Data 2Data 2
105ITEM 2Data 2Data 2
106ITEM 2Data 2Data 2
107
108Data 3Data 3
109Data 3Data 3
110Data 3ITEM 3
111Data 3Data 3
112Data 3Data 3
113Data 3Data 3
114Data 3Data 3
115Data 3Data 3
116Data 3
117Data 3Data 3Data 3
118ITEM 3Data 3Data 3
119ITEM 3Data 3Data 3
120ITEM 3Data 3Data 3
121ITEM 3Data 3Data 3
Sheet1
Move Item 1 to (B4) to A12 onwards, this may be be any length, then move on to next section. As in the after example in the sample.
 
Upvote 0

Forum statistics

Threads
1,215,169
Messages
6,123,412
Members
449,098
Latest member
ArturS75

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top