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

Variable serial numbers in non adjacent cells

0

For one of my Excel project I need Variable serial numbers in Column A which should get refreshed with the change of dates in Column B. For example for Date1 in cell B1 the cells in A1 should be 1 and A4 should be 2. But with the change of date in cell B7 the cell A7 sould get refreshed to 1, cell A10 to 2 and cell A13 to 3. Again with the change of date in cell B16, the cell A16 should get refreshed to 1, cell A19 to 2, cell A22 to 3 and cell A25 to 4.

Answer
Discuss

Discussion

Honestly, I'm a bit confused. Are you saying that the numbers in column A should line up with dates that can be entered in any row in column B?

Aside from that, I feel like there is a much easier way to do this if you setup your data differently. Can you explain more about what you are actually trying to accomplish with this setup? 
don (rep: 1989) Oct 12, '20 at 4:57 pm
Add to Discussion

Answers

0

This is definitely a little more tricky than your previous question :-) I suppose it could be done with formulas, too but that's beyond my pay grade. So I offer VBA.

Option Explicit

Private Enum Nws                ' worksheet navigation (change values to suit)
    ' 103
    NwsFirstNumberedRow = 3     ' row above this one can't be blank (headers)
    NwsRowsPerDataSet = 3
    NwsNumber = 1               ' Columns:- 1= A, 2=B, 3=C etc
    NwsDate                     ' (no value = previous + 1)
End Enum

Private Sub Worksheet_Change(ByVal Target As Range)
    ' 103

    Dim Arr         As Variant          ' values to paste to column NwsNumber
    Dim n           As Integer          ' NextGroupSize
    Dim Rl          As Long             ' last used row in NwsNumber
    Dim i           As Integer          ' loop counter: groups

    With Target
        If .Cells.CountLarge = 1 Then   ' skip if more than 1 cell was changed (paste)
            Rl = Cells(Rows.Count, NwsNumber).End(xlUp).Row
            If .Address = Cells(Rl + 1, NwsDate).Address Then
                n = NextGroupSize(Rl)
                ReDim Arr(1 To n * NwsRowsPerDataSet)
                For i = 0 To n - 1
                    Arr(i * NwsRowsPerDataSet + 1) = i + 1
                    Arr(i * NwsRowsPerDataSet + 2) = "Name"
                    Arr(i * NwsRowsPerDataSet + 3) = "Roll#"
                    'add more rows here if and when needed
                Next i
                With Cells(.Row, NwsNumber).Resize(UBound(Arr))
                    .Value = Application.Transpose(Arr)
                    ' you can format the cells here
                    .HorizontalAlignment = xlCenter
                End With
            End If
        End If
    End With
End Sub

Private Function NextGroupSize(ByVal Rl As Long) As Integer
    ' 103
    ' return the number of entries in the next group

    Dim Fun         As Integer          ' function return value
    Dim R           As Long             ' row calculator

    If Rl <= NwsFirstNumberedRow Then Rl = 0
    R = NwsFirstNumberedRow
    Do
        Fun = Fun + 1
        R = R + ((Fun + 1) * NwsRowsPerDataSet) + (Fun = 1)
    Loop While R <= Rl

    NextGroupSize = Fun + 1
End Function

Paste the entire code in the code sheet belonging to the worksheet on which you want the action. That's a sheet set up by Excel so that it is informed of worksheet events (standard modules you can add yourself don't have thast feature). The code responds to the Change event which occurs wqhenever a cell in the sheet is modified.

Now, you don't want a big halluballu whenever you change just anything on the sheet. But when you enter a date (anything, in fact, but you could add a test to be sure it's a date) in the cell in the Date column in the row below the last used cell in the Numbers column, then - and only then - you want numbers to be added. This means that at any given moment only a single cell on the entire sheet meets the criteria. On the attached workbook I have highlighted the cells on the TXL_4740 tab. Try your luck with B18.

I converted your hypothetical task into a generic one because hypothesis isn't practical. Please study the Enum[erations] section at the top of the code until you fully understand it. Basically, an enumeration is just a simple way of assigning names to numbers. But it's your control panel where you can assign/change columns and default rows. My example starts in row 3 (NwsFirstNumberedRow = 3). I doubt that your really starts in row 1 but you can choose any row. Just assign another number to the name. You can even add rows to each group in the future, like (Name, First name, Roll#).

Most of my time went into the function NextGroupSize, by the way. It looks innocent but it's loaded with complications.

Discuss


Answer the Question

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