#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Looping

Hi Guys. I am new to macro programming. Below is my data. I need to write a
loop that will insert new row with same data whenever the quantity is 2, 3
etc. e.g if the quantity is 1,no new row. if is it 2, 2 additional row with
same datas info, if 3, 3 additional rows with same data. thanks

State Style Quantity
Maryland 100g 1
New york xzy10 1
texas 200g 2
OH 3
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 638
Default Looping

On May 19, 3:38*pm, maju wrote:
Hi Guys. I am new to macro programming. Below is my data. I need to write a
loop that will insert new row with same data whenever the quantity is 2, 3
etc. e.g if the quantity is 1,no new row. if is it 2, 2 additional row with
same datas info, if 3, 3 additional rows with same data. thanks

State * * * * Style * Quantity
Maryland * 100g * * * 1
New york * *xzy10 * * 1
texas * * * * *200g * * *2
OH * * * * * * * * * * * * * 3


Here's one way. This is assuming that your quantity is in column C.
It also includes no error handling, so if you have text in column C
other than the header row, this will error out.

Sub oneWay()
Dim r As Long
r = 2
Do Until IsEmpty(Cells(r, "C"))
With Cells(r, "C")
If .Value = "1" Then
r = r + 1
Else
For i = 1 To .Value
.Offset(1, 0).EntireRow.Insert
.EntireRow.Copy _
Destination:=Cells(r + 1, "A")
Next i
r = r + i
End If
End With
Loop
End Sub
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 638
Default Looping

On May 19, 4:13*pm, JW wrote:
On May 19, 3:38*pm, maju wrote:

Hi Guys. I am new to macro programming. Below is my data. I need to write a
loop that will insert new row with same data whenever the quantity is 2, 3
etc. e.g if the quantity is 1,no new row. if is it 2, 2 additional row with
same datas info, if 3, 3 additional rows with same data. thanks


State * * * * Style * Quantity
Maryland * 100g * * * 1
New york * *xzy10 * * 1
texas * * * * *200g * * *2
OH * * * * * * * * * * * * * 3


Here's one way. *This is assuming that your quantity is in column C.
It also includes no error handling, so if you have text in column C
other than the header row, this will error out.

Sub oneWay()
* * Dim r As Long
* * r = 2
* * Do Until IsEmpty(Cells(r, "C"))
* * * * With Cells(r, "C")
* * * * * * If .Value = "1" Then
* * * * * * * * r = r + 1
* * * * * * Else
* * * * * * * * For i = 1 To .Value
* * * * * * * * * * .Offset(1, 0).EntireRow.Insert
* * * * * * * * * * .EntireRow.Copy _
* * * * * * * * * * * * Destination:=Cells(r + 1, "A")
* * * * * * * * Next i
* * * * * * * * r = r + i
* * * * * * End If
* * * * End With
* * Loop
End Sub


oops. Forgot to declare i.

Sub oneWay()
Dim r As Long, i As Long
r = 2
Do Until IsEmpty(Cells(r, "C"))
With Cells(r, "C")
If .Value = "1" Then
r = r + 1
Else
For i = 1 To .Value
.Offset(1, 0).EntireRow.Insert
.EntireRow.Copy _
Destination:=Cells(r + 1, "A")
Next i
r = r + i
End If
End With
Loop
End Sub
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default Looping

Try

Sub insertandcopy()
mc = "c"
For i = Cells(Rows.Count, mc).End(xlUp).Row To 2 Step -1
mv = Cells(i, mc)
If mv 1 Then
Rows(i).Copy
Rows(i).Resize(mv).Insert
End If
Next i
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"maju" wrote in message
...
Hi Guys. I am new to macro programming. Below is my data. I need to write
a
loop that will insert new row with same data whenever the quantity is 2, 3
etc. e.g if the quantity is 1,no new row. if is it 2, 2 additional row
with
same datas info, if 3, 3 additional rows with same data. thanks

State Style Quantity
Maryland 100g 1
New york xzy10 1
texas 200g 2
OH 3


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Looping

ok. thanks. I ran the code but i want it to give me for example, if the
quantity is 2, then i will get 1 extra row with data inserted, if 3, 2 rows
inserted etc

"Don Guillett" wrote:

Try

Sub insertandcopy()
mc = "c"
For i = Cells(Rows.Count, mc).End(xlUp).Row To 2 Step -1
mv = Cells(i, mc)
If mv 1 Then
Rows(i).Copy
Rows(i).Resize(mv).Insert
End If
Next i
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"maju" wrote in message
...
Hi Guys. I am new to macro programming. Below is my data. I need to write
a
loop that will insert new row with same data whenever the quantity is 2, 3
etc. e.g if the quantity is 1,no new row. if is it 2, 2 additional row
with
same datas info, if 3, 3 additional rows with same data. thanks

State Style Quantity
Maryland 100g 1
New york xzy10 1
texas 200g 2
OH 3





  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Looping

why did u resize it?

"Don Guillett" wrote:

Try

Sub insertandcopy()
mc = "c"
For i = Cells(Rows.Count, mc).End(xlUp).Row To 2 Step -1
mv = Cells(i, mc)
If mv 1 Then
Rows(i).Copy
Rows(i).Resize(mv).Insert
End If
Next i
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"maju" wrote in message
...
Hi Guys. I am new to macro programming. Below is my data. I need to write
a
loop that will insert new row with same data whenever the quantity is 2, 3
etc. e.g if the quantity is 1,no new row. if is it 2, 2 additional row
with
same datas info, if 3, 3 additional rows with same data. thanks

State Style Quantity
Maryland 100g 1
New york xzy10 1
texas 200g 2
OH 3



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Looping

Don, i keep getting error msg at
Rows(i).Resize(mv).Insert
Why?

"Don Guillett" wrote:

Try

Sub insertandcopy()
mc = "c"
For i = Cells(Rows.Count, mc).End(xlUp).Row To 2 Step -1
mv = Cells(i, mc)
If mv 1 Then
Rows(i).Copy
Rows(i).Resize(mv).Insert
End If
Next i
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"maju" wrote in message
...
Hi Guys. I am new to macro programming. Below is my data. I need to write
a
loop that will insert new row with same data whenever the quantity is 2, 3
etc. e.g if the quantity is 1,no new row. if is it 2, 2 additional row
with
same datas info, if 3, 3 additional rows with same data. thanks

State Style Quantity
Maryland 100g 1
New york xzy10 1
texas 200g 2
OH 3



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 Maggie[_6_] Excel Discussion (Misc queries) 6 October 2nd 08 09:14 PM
Looping anderssweden Excel Programming 1 June 14th 06 03:00 PM
Looping Louise[_4_] Excel Programming 1 September 10th 04 04:57 PM
looping every third row Jason Hancock Excel Programming 5 July 1st 04 08:00 PM
Looping René Excel Programming 6 May 28th 04 02:14 PM


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