PDA

View Full Version : Solved: Excel 2003 - My for ea loop is not looping per inputbox number



frank_m
01-22-2011, 05:41 AM
I'm trying to make X copies of the selected row, but my code as I have it now is only making one copy regardless of the number that I enter in the inputbox.

Please inform me of the error in my ways :dunno

I know it's a silly mistake that I'm a bit too tired to spot at the moment,
-- your help is appreciated.

Thanks :)
Sub Copy_Record_X_Times()

Dim X As Variant

beginning:
X = Application.InputBox("Please enter the number of copies you need for the selected row.")

If Not IsNumeric(X) Then
MsgBox "Only a number can be entered"
GoTo beginning
End If

If X = False Then
MsgBox "Canceled"
Exit Sub
Else
For X = 1 To X
ActiveCell.EntireRow.Copy
ActiveCell.EntireRow.Insert Shift:=xlDown
ActiveCell.Offset(1, 6 - ActiveCell.Column).ClearContents 'clear col 6 cell for each copy
ActiveCell.Offset(1, 10 - ActiveCell.Column).ClearContents 'clear col 10 cell for each copy
ActiveCell.Offset(1, 12 - ActiveCell.Column).ClearContents 'clear col 12 cell for each copy
ActiveCell.Offset(1, 13 - ActiveCell.Column).ClearContents 'clear col 13 cell for each copy
ActiveCell.Offset(1, 14 - ActiveCell.Column).ClearContents 'clear col 14 cell for each copy
Next

End If

End Sub

frank_m
01-22-2011, 05:49 AM
figured it out

it was silly, just as I thought
as I was trying to loop from X to X :rofl:

Sub Copy_Record_X_Times()

Dim X As Variant
Dim i As Integer '<--
begining:
X = Application.InputBox("Please enter the number of copies you need for this row.")

If Not IsNumeric(X) Then
MsgBox "Only a number can be entered"
GoTo begining
End If

If X = False Then
MsgBox "Canceled"
Exit Sub
Else
For i = 1 To X
ActiveCell.EntireRow.Copy
ActiveCell.EntireRow.Insert Shift:=xlDown
ActiveCell.Offset(1, 6 - ActiveCell.Column).ClearContents
ActiveCell.Offset(1, 10 - ActiveCell.Column).ClearContents
ActiveCell.Offset(1, 12 - ActiveCell.Column).ClearContents
ActiveCell.Offset(1, 13 - ActiveCell.Column).ClearContents
ActiveCell.Offset(1, 14 - ActiveCell.Column).ClearContents
Next

End If

End Sub

Bob Phillips
01-22-2011, 07:35 AM
But you are still not using the loop control within the loop, so you are just repeating the same action over and over.

frank_m
01-22-2011, 07:53 AM
Hi xld,

I'm not sure what you mean.
The action is supposed to repeat by the number entered in the inputbox.
ie: entering 4 makes 4 copies of the row and clears the column 6, 10, 12, and 13 cell's in each of the copies.