Consulting

Results 1 to 4 of 4

Thread: Solved: Excel 2003 - My for ea loop is not looping per inputbox number

  1. #1
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location

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

    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

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

    Thanks
    [vba]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[/vba]

  2. #2
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    figured it out

    it was silly, just as I thought
    as I was trying to loop from X to X
    [vba]
    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[/vba]

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    But you are still not using the loop control within the loop, so you are just repeating the same action over and over.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •