Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

How to set output range of a "repeat cell value" automatically

0

Below is a macro that will prompt the user to select a preset cell value (ie.A2) and quantity (ie.B2).  The cell value A2 will be repeated (pasted) by the amount specifed in B2. The macro currently asks the for the starting cell of where those x amount of A2 values should begin to be pasted.

Question - How can the macro be modified to automatically start repeating the x amount of A2 values in the cell below the original A2 value AND for a total count of quantity B2?  IOW, if the quantity = 5, the total repeated values in the column should be 5 (not 6) .

Code below and file attached showing before and after scenarios. 

Sub PartMultiplier()
Dim Rng As Range
Dim InputRng As Range, OutRng As Range
xTitleId = "Part Multiplier"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Select P/N and Quantity Cells:", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
Set OutRng = OutRng.Range("A1")
For Each Rng In InputRng.Rows
    xValue = Rng.Range("A1").Value
    xNum = Rng.Range("B1").Value
    OutRng.Resize(xNum, 1).Value = xValue
    Set OutRng = OutRng.Offset(xNum, 0)
Next
End Sub

Thanks in advance,

~Tina

Answer
Discuss

Answers

0
Selected Answer

 Your code appears very different from the description you give of your intentions. Therefore I have ignored it. The code below does what I understood from your description. Note that this code must be pasted into a standard code module. That is one that you have to insert. Its default name will be like Module1.

Sub NewMultiplier()
    ' Variatus @TeachExcel 23 Feb 2019

    Dim Original As Variant
    Dim Repeats As Integer
    Dim Rng As Range

    Original = Range("A2").Formula
    Repeats = Int(Val(Range("B2").Value))
    Set Rng = Range("A2").Resize(Repeats - 1, 1).Offset(1)
    Rng.Value = Original
End Sub

Still working on the same presumption, I created another solution for you. The code below should be pasted into the code module of the worksheet on which you want the action. If you paste it to a standard code module, as is proper for the above code, it won't work.

Private Sub Worksheet_Change(ByVal Target As Range)
    ' Variatus @TeachExcel 24 Feb 2019

    Const TargetClm As Long = 2                     ' 2 = column B
                                                    ' (can't be < 2)
    Dim Original As Variant
    Dim Repeats As Integer
    Dim Rl As Long
    Dim Rng As Range

    With Target
        ' respond to a change in any cell in column
        ' TargetClm except row 1
        If (.Column = TargetClm) And (.Row > 1) Then
            Repeats = Int(Val(.Value))
            Set Rng = Cells(.Row, TargetClm - 1)    ' column to the left of TargetClm
            With Rng
                Rl = Cells(Rows.Count, .Column).End(xlUp).Row
                Original = .Formula
                If Len(Original) = 0 Then Exit Sub

                If Rl > .Row Then Range(.Offset(1), Cells(Rl, .Column)).ClearContents
                Set Rng = .Resize(Repeats - 1, 1).Offset(1)
            End With
            Rng.Value = Original
            .Offset(Repeats, -1).Select
        End If
    End With
End Sub

If installed in the correct location, as described above, this code will respond to an entry in any row below row 1 of the column designated as TargetClm at the top of the procedure. It will read the value to the left of it in the same row and replicate it as many times as the user has entered. In the left column, any existing values below the Target cell row will be deleted.

Discuss

Discussion

Very nice solutions Varitus!   The first one is the direction I could use. However, can you modify it so that the user selects the part cell "A?" and repeat quantity "B?".  I need an input box to select the A? & B? cells, since it will not always be cells A2 & B2.   As the column gets filled, the user will for example select A11 & B11, and the cells under A11 will thus be filled and so on.    Thank you.
caligirl626 (rep: 2) Feb 23, '20 at 3:55 pm
I really don't like to have an input box because it costs so much time and permits error. Therefore I have improved the event procedure. I'll do the other one if you insist. In fact, it's easier to do. But please try the event procedure first.
Variatus (rep: 4889) Feb 23, '20 at 8:46 pm
YES!  Your suggestion of an event code module is a much more elegant solution. This does exactly what I was looking for!  One more favor if it's not too much to ask for:  When the module is complete, can the final resting place (final selected cell) be set to the next blank "A?" cell?   ty
caligirl626 (rep: 2) Feb 23, '20 at 10:03 pm
I'm glad I could convince you. The improvement you suggest is a good one and I have included this idea in my answer. Actually, it's just one line of code - the last one before the Ends
.Offset(Repeats, -1).Select
Variatus (rep: 4889) Feb 24, '20 at 4:21 am
Beautiful. Thank you.   How can I ever repay you?  :)   I have several more questions.  Can I ask you direct or just post them here?   ~Tina
caligirl626 (rep: 2) Feb 24, '20 at 5:46 pm
Please post them here, each question in its own thread. Thank you.
Variatus (rep: 4889) Feb 24, '20 at 9:16 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login