Thread: Looping
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
JW[_2_] JW[_2_] is offline
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