Search code examples
excelvba

Add sequential numbers to Cell range


Can you please help me with a problem (I guess it's quite simple)? I have a cell range from B9 to K11; all I want the VBA code to do is:

  • Click a button to open UserForm with a TextBox in it.

  • Insert the first number of the sequence. Example "10" over the TextBox into Cell B9

Then ask for the number of sequences to add to the number and put each of the unique numbers into cells in the cell range from B9 to K11.

Example:

I want to insert number "10" over the UserForm to Cell B9. There should be (this time, but it varies depending on orders) 11 sequences to it. So, number 10 Cell B9, number 11 Cell C9, number 12 Cell D9,... and number 11 Cell B10. Can you help me with that? I have written a formula, but it only repeats the first number. I cannot get it to work.

Sub Add_same_number_to_a_range_of_cells()
'declare variables
Dim ws As Worksheet
Dim rng As Range
Dim myVal As Range
Set ws = Worksheets("PV KOZAREC")
Set rng = ws.Range("B9:K11")
For Each myVal In rng
myVal = myVal.Value + ws.Range("B9")
Next myVal
End Sub

Solution

  • If you had more plans than just the needed formula Dominique provided, here's the VBA version:

    Presuming there is one textbox for the number and one for the sequence amount + 1 button to hit it all off. I had a button in my sheet to re-show the UserForm1 Following is in the UserForm1 code-page:

    Private Sub CommandButton1_Click()
        Dim numb, rep, ws As Worksheet
        numb = TextBoxNumb.Value
        rep = TextBoxAmt.Value
        
        If IsNumeric(numb) And IsNumeric(rep) Then
            Set ws = ThisWorkbook.Worksheets("Sequences")
            Dim i As Long, r As Long, c As Long
            For i = 1 To rep
                ws.Range("B9").Offset(r, c).Value = numb + i - 1
                c = c + 1
                If c > 9 Then
                    c = 0
                    r = r + 1
                End If
            Next i
            UserForm1.Hide
        Else
            MsgBox "Please only enter numbers (integer for the sequence)", vbCritical
        End If
    End Sub
    

    Or you can have the code in a separate module and you can also use an array if the sequence would get lengthier and thus take more time writing cell by cell to the sheet: Userform1 code-page:

    Private Sub CommandButton1_Click()
        Dim numb, rep, ws As Worksheet
        numb = TextBoxNumb.Value
        rep = TextBoxAmt.Value
    
        If IsNumeric(numb) And IsNumeric(rep) Then
            Set ws = ThisWorkbook.Worksheets("Sequences")
            lRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
            ws.Range("B9:K" & lRow).Clear 'clear previous sequence
            alternateSequence numb, rep, 10, ws.Range("B9")
            '10 is the amount of columns from B to K
            UserForm1.Hide
        Else
            MsgBox "Please only enter numbers (integer for the sequence)", vbCritical
        End If
    End Sub
    

    Separate module, i.e. Module1:

    Sub alternateSequence(numb As Long, seq As Long, cols As Long, startRng As Range)
        Dim arr, i As Long, r As Long, c As Long
        ReDim arr(0 To Application.WorksheetFunction.RoundUp(seq / 10, 0) - 1, 0 To IIf(cols > seq, seq, cols) - 1)
        For i = 1 To seq
            arr(r, c) = numb + i - 1
            c = c + 1
            If c > UBound(arr, 2) Then 'next row in the array needed
                c = 0
                r = r + 1
            End If
        Next i
        startRng.Resize(UBound(arr, 1) + 1, UBound(arr, 2) + 1).Value = arr
        'Resize to allow the entire array to be given at once
    End Sub
    

    This makes the separate sub re-usable as well.