ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Looping Code (https://www.excelbanter.com/excel-programming/435035-looping-code.html)

tpeter

Looping Code
 
I have that attached code that I want to run based and the number of cell m2.
So if cell m2 = 5 I would like the copy and insert to happen 4 times. This is
for a custom form made by the end user. Any help would be great, I have never
tried to loop the same code over again.

Range("C15:K15").Select
Selection.Copy
Range("C15").Select
Selection.Insert Shift:=xlDown (need this to happen # of times
inidicated in cell m2)

Tim Peter


Patrick Molloy[_2_]

Looping Code
 
no loop required.
See my answer from this morning. It only required a small change. Its better
for everybody to follow the same thread - it just measn people don't waste
their time sending the same work as already done. If its not what you need,
just reply and say why. thanks

Option Explicit
Sub Main()
Dim qrows As Long
qrows = Range("M2") ' InputBox("How many rows")
If qrows 0 Then
Range("C15:k15").Copy
Range("C16").Resize(qrows - 1).PasteSpecial xlPasteAll
Application.CutCopyMode = False
End If


End Sub


"tpeter" wrote:

I have that attached code that I want to run based and the number of cell m2.
So if cell m2 = 5 I would like the copy and insert to happen 4 times. This is
for a custom form made by the end user. Any help would be great, I have never
tried to loop the same code over again.

Range("C15:K15").Select
Selection.Copy
Range("C15").Select
Selection.Insert Shift:=xlDown (need this to happen # of times
inidicated in cell m2)

Tim Peter


Jacob Skaria

Looping Code
 
You dont need to loop. Try the below

Sub Macro2()
Range("C15:K15").Copy
Range("C15").Resize(Range("M2") - 1).Insert Shift:=xlDown
Application.CutCopyMode = False
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"tpeter" wrote:

I have that attached code that I want to run based and the number of cell m2.
So if cell m2 = 5 I would like the copy and insert to happen 4 times. This is
for a custom form made by the end user. Any help would be great, I have never
tried to loop the same code over again.

Range("C15:K15").Select
Selection.Copy
Range("C15").Select
Selection.Insert Shift:=xlDown (need this to happen # of times
inidicated in cell m2)

Tim Peter


Patrick Molloy[_2_]

Looping Code
 
I see my code failed....i left off the INSERT bit.



"Patrick Molloy" wrote:

no loop required.
See my answer from this morning. It only required a small change. Its better
for everybody to follow the same thread - it just measn people don't waste
their time sending the same work as already done. If its not what you need,
just reply and say why. thanks

Option Explicit
Sub Main()
Dim qrows As Long
qrows = Range("M2") ' InputBox("How many rows")
If qrows 0 Then
Range("C15:k15").Copy
Range("C16").Resize(qrows - 1).PasteSpecial xlPasteAll
Application.CutCopyMode = False
End If


End Sub


"tpeter" wrote:

I have that attached code that I want to run based and the number of cell m2.
So if cell m2 = 5 I would like the copy and insert to happen 4 times. This is
for a custom form made by the end user. Any help would be great, I have never
tried to loop the same code over again.

Range("C15:K15").Select
Selection.Copy
Range("C15").Select
Selection.Insert Shift:=xlDown (need this to happen # of times
inidicated in cell m2)

Tim Peter


tpeter

Looping Code
 
Awsome,

Thank both of you so much, I have been pulling my hair out.

"Jacob Skaria" wrote:

You dont need to loop. Try the below

Sub Macro2()
Range("C15:K15").Copy
Range("C15").Resize(Range("M2") - 1).Insert Shift:=xlDown
Application.CutCopyMode = False
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"tpeter" wrote:

I have that attached code that I want to run based and the number of cell m2.
So if cell m2 = 5 I would like the copy and insert to happen 4 times. This is
for a custom form made by the end user. Any help would be great, I have never
tried to loop the same code over again.

Range("C15:K15").Select
Selection.Copy
Range("C15").Select
Selection.Insert Shift:=xlDown (need this to happen # of times
inidicated in cell m2)

Tim Peter


tpeter

Looping Code
 
This worked great but now I have to modify the code to find where the text
stops in column c and go down 4 spaces, then select that cell plus 8 to the
right and repeat the copy based on the number of lines requested. I have the
xldown correct but I don't know how to refer to the cell without a number(ex
c15). This number will change based on the number of rows above it. Thanks
again for everyones help.

Sub FindEnd()
Dim LastCell As Range

Set LastCell = Range("c10").End(xlDown)
LastCell.Select
ActiveCell.Offset(4).Select
ActiveCell.Offset(8).End(xlToRight).Copy 'don't know the Row number.Need to
copy 8 to the right
Range("c").Resize(Range("m2") - 1).Insert Shift:=xlDown 'don't know the line
it will be.
Application.CutCopyMode = False

End Sub

"tpeter" wrote:

Awsome,

Thank both of you so much, I have been pulling my hair out.

"Jacob Skaria" wrote:

You dont need to loop. Try the below

Sub Macro2()
Range("C15:K15").Copy
Range("C15").Resize(Range("M2") - 1).Insert Shift:=xlDown
Application.CutCopyMode = False
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"tpeter" wrote:

I have that attached code that I want to run based and the number of cell m2.
So if cell m2 = 5 I would like the copy and insert to happen 4 times. This is
for a custom form made by the end user. Any help would be great, I have never
tried to loop the same code over again.

Range("C15:K15").Select
Selection.Copy
Range("C15").Select
Selection.Insert Shift:=xlDown (need this to happen # of times
inidicated in cell m2)

Tim Peter



All times are GMT +1. The time now is 11:40 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com