+ Reply to Thread
Results 1 to 14 of 14

autonumbering for multiple columns based on two cells

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-21-2022
    Location
    africa
    MS-Off Ver
    2019
    Posts
    146

    autonumbering for multiple columns based on two cells

    Hi
    I want autonumbering and increment in D6 based on match cell D2 with headers in G1:J1 , if they're matched , then should increment number in D6 based on the last row for cell in columns G or H or I or J depends on matching with D2 and after autonumbering and increment in D6 then should copy under last row for cell in column match header with D2 as I highlighted in CR1,CR2,CR2 sheets (theses just examples )
    I put example when use SALES & PURCHASES in D2 and match with columns G or H and will autonumbering and increment in D6 and copy under last row for cell in column G or H as in CR1,CR2,CR2 but CANCEL sheet should delete autonumbering in D6 when clear D2 and delete under last row for cell in column G when press commandbutton1(this case depends on CR3 sheet as in example)
    thanks
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,706

    Re: autonumbering for multiple columns based on two cells

    As far as populating cell D6 the following formula seems to work:
    Formula: copy to clipboard
    =IF(ISTEXT(D2),INDEX(G2:J35,AGGREGATE(14,6,(ROW(G2:J35)-ROW(G1))/(G1:J1=D2)/(G2:J35<>""),1),MATCH(D2,G1:J1,0)),"")

    I can't help with the button, but hopefully someone else can.
    Let us know if you have any questions.
    Consider taking the time to add to the reputation of everybody that has taken the time to respond to your query.

  3. #3
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,481

    Re: autonumbering for multiple columns based on two cells

    Working with sheet "main" only
    There are two cell D2 and D6
    And the "Worksheet_change" event trigger those cells.
    In D2: Pick item from Dropdown list. D6 and corresponding column G:J last row will generate new items with sequence number +1
    In D6: Just Delete then the corresponding value in G:J will be deleted.

    PHP Code: 
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim pos&, LastCell As RangeoldVf
    If Target.Address(00) = "D2" And Not IsEmpty(TargetThen
        pos 
    Evaluate("=match(D2,B2:B5,0)")
        
    Set LastCell Range("F1").Offset(, pos).End(xlDown)
        
    Application.EnableEvents False
        Range
    ("D6").Value Left(LastCellLen(LastCell) - 3) & Format(Right(LastCell3) + 1"000")
        
    Application.EnableEvents True
        LastCell
    .Offset(10).Value Range("D6").Value
    ElseIf Target.Address(00) = "D6" Then
        
    If IsEmpty(TargetThen
            Application
    .Undo
            oldV 
    Target
            Set f 
    Range("G2:J100").Find(oldV)
            If 
    Not f Is Nothing Then f.ClearContents
            Application
    .EnableEvents False
            Target
    .ClearContents
            Application
    .EnableEvents True
        End 
    If
    End If
    End Sub 
    Attached Files Attached Files
    Quang PT

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,637

    Re: autonumbering for multiple columns based on two cells

    1) To a standard code module
    Sub test()
        Dim x, s(1)
        With ActiveSheet
            .[d6].ClearContents
            If .[d2] = "" Then Exit Sub
            x = Application.Match(.[d2], .Rows(1), 0)
            s(0) = .Cells(Rows.Count, x).End(xlUp)
            s(1) = Format$(Val(Right$(s(0), 3)) + 1, "000")
            s(0) = Left(s(0), Len(s(0)) - 3)
            .[d6] = Join(s, "")
            .Cells(Rows.Count, x).End(xlUp)(2) = Join(s, "")
        End With
    End Sub
    2) Add Forms CommandButton, prefer on the worksheet rather than ActiveX object, in each sheet that you want the code to be executed.
    Right click and assign "test" macro.
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    09-21-2022
    Location
    africa
    MS-Off Ver
    2019
    Posts
    146

    Re: autonumbering for multiple columns based on two cells

    I can't help with the button, but hopefully someone else can.
    need vba . anyway thank you

  6. #6
    Forum Contributor
    Join Date
    09-21-2022
    Location
    africa
    MS-Off Ver
    2019
    Posts
    146

    Re: autonumbering for multiple columns based on two cells

    @bebo021999
    fantastic !
    many thanks for your help.

  7. #7
    Forum Contributor
    Join Date
    09-21-2022
    Location
    africa
    MS-Off Ver
    2019
    Posts
    146

    Re: autonumbering for multiple columns based on two cells

    @jindion
    perfect but you forgot last case in CANCEL sheet .
    when clear D2,D6 ,then should clear last cell in column have ever autonumbering .
    example : D2=SALES , when run macro D6=SS NO: S001 , but if I come back to cancelling by clear D2,D6 , then will clear S001 from SALES column .

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,637

    Re: autonumbering for multiple columns based on two cells

    That's a really a stupid way.
    Add CANCEL to the named range
    Sub test()
        Dim x, s(1)
        With ActiveSheet
            If .[d2] = "" Then Exit Sub
            If .[d2] = "CANCEL" Then
                If .Name = "CANCEL" Then
                    .Columns("g").Resize(, .Cells.SpecialCells(11).Column - 7).Replace .[d6], "", 1
                    .[d6].ClearContents
                End If
            Else
                x = Application.Match(.[d2], .Rows(1), 0)
                s(0) = .Cells(Rows.Count, x).End(xlUp)
                s(1) = Format$(Val(Right$(s(0), 3)) + 1, "000")
                s(0) = Left(s(0), Len(s(0)) - 3)
                .[d6] = Join(s, "")
                .Cells(Rows.Count, x).End(xlUp)(2) = Join(s, "")
            End If
        End With
    End Sub

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,637

    Re: autonumbering for multiple columns based on two cells

    Don't like this way though
    1) No CANCEL in the list.
    2) Change test sub to
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim s
        If Target.Address(0, 0) <> "D6" Then Exit Sub
        Application.EnableEvents = False
        If Target.Value = "" Then
            Application.Undo
            s = [d6]
            Columns("g").Resize(, Cells.SpecialCells(11).Column - 6).Replace s, "", 1
            [d6].ClearContents
        End If
        Application.EnableEvents = True
    End Sub
    3) to CANCEL sheet code module
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim s
        If Target.Address(0, 0) <> "D6" Then Exit Sub
        Application.EnableEvents = False
        If Target.Value = "" Then
            Application.Undo
            s = [d6]
            Columns("g").Resize(, Cells.SpecialCells(11).Column - 6).Replace s, "", 1
            [d6].ClearContents
        End If
        Application.EnableEvents = True
    End Sub

  10. #10
    Forum Contributor
    Join Date
    09-21-2022
    Location
    africa
    MS-Off Ver
    2019
    Posts
    146

    Re: autonumbering for multiple columns based on two cells

    sorry jindon !
    procedure cancel should be in the MAIN sheet , not cancel sheet.
    cancel sheet is just example .

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,637

    Re: autonumbering for multiple columns based on two cells

    Then use #9 and 3) to main sheet code module instead of CANCEL sheet.

  12. #12
    Forum Contributor
    Join Date
    09-21-2022
    Location
    africa
    MS-Off Ver
    2019
    Posts
    146

    Re: autonumbering for multiple columns based on two cells

    Then use #9 and 3) to main sheet code module instead of CANCEL sheet.
    yes I will use #3 , but #9 doesn't help because the code doesn't work
    thank you

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,637

    Re: autonumbering for multiple columns based on two cells

    Tested #9 and working here, so bad luck.

  14. #14
    Forum Contributor
    Join Date
    09-21-2022
    Location
    africa
    MS-Off Ver
    2019
    Posts
    146

    Re: autonumbering for multiple columns based on two cells

    so bad luck.
    no !
    I understood this
    2) Change test sub to
    delete test sub . I expect the code in post#9 will do any thing without depend on test sub

    so with two procedures the code works perfectly .
    many thanks for your time & help .

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] autonumbering based on two textboxes together on userform for two sheets
    By Maklil in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-30-2022, 06:01 AM
  2. [SOLVED] pops up zero based on select multiple cells for multiple columns
    By Alaa-A in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-24-2022, 12:18 PM
  3. is it possible autonumbering multiple columns based on select active cell?
    By ABDELFATTA in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-14-2020, 12:53 PM
  4. Populating fields in multiple cells based on criteria in multiple columns
    By excelalways in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-28-2014, 08:17 AM
  5. Autonumbering from two columns
    By whatever61 in forum Excel General
    Replies: 0
    Last Post: 04-11-2012, 11:05 AM
  6. Autonumbering rows across multiple sheets
    By Mattmace in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-22-2011, 03:50 PM
  7. autonumbering cells - yet again
    By dunescratcher in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-05-2006, 08:25 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1