+ Reply to Thread
Results 1 to 8 of 8

populate list with each new value

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-07-2015
    Location
    cyprus
    MS-Off Ver
    Microsoft 365
    Posts
    182

    populate list with each new value

    I have a file with 3 spreadsheet and one called "parameters" with list for data validation purposes.

    I would like that each time a new value is entered in column 2 of one of the three other sheets (in my example, the code is under "H&M" sheeet), a macro goes to "parameters", column 5, look if the new value exists in the list, if yes nothing, if no, add the new value, and sort the list alphabetically before going back to initial sheet.

    My problem is the .find code to look into "parameters", range E., i get a "type mismatch" error.

    Can someone help?
    Attached Files Attached Files

  2. #2
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: populate list with each new value

    This will find your value in the list, you will have to work from there

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim where As Range, where1 As Range, rng As Range, Vsl As String, lr as Long
    
    If Target.Column = 2 Then
    
    Vsl = Target.Value
    
    With ThisWorkbook.Worksheets("Parameters")
    lr = .Range("e" & Rows.Count).End(xlUp).Row
    Set rng = .Range("E2:E" & lr)
    Set where1 = rng.Find(Vsl, LookIn:=xlValues)
    
            
        If where1 Is Nothing Then
            .Range("E" & lr + 1).Value = Vsl
            .Range("e1", .Range("e1").End(xlDown)).Sort Key1:=.Range("e1"), Order1:=xlAscending
        End If
            
        End With
    End If
    
    End Sub
    Last edited by nigelog; 09-08-2021 at 10:38 AM.

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: populate list with each new value

    Remove all code from the Sheet modules and try this one in ThisWorkbook module.

    Also I would make your Vsl range a dynamic one to avoid all the empty rows at the bottom.

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        If Target.Column <> 2 Then Exit Sub
        If Target.Value = vbNullString Then Exit Sub
        If Sh.Name <> "Parameters" Then
            fRow = Application.Match(Target.Value, Sheets("Parameters").Columns(5), 0)
            If IsError(fRow) Then
                With Sheets("Parameters")
                    .Range("E" & .Rows.Count).End(xlUp).Offset(1) = Target.Value
                    .Range("E2", .Range("E" & .Rows.Count).End(xlUp)).Sort .Range("E2"), xlAscending
                End With
            End If
        End If
    End Sub
    Last edited by bakerman2; 09-08-2021 at 11:03 AM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Forum Contributor
    Join Date
    10-07-2015
    Location
    cyprus
    MS-Off Ver
    Microsoft 365
    Posts
    182

    Re: populate list with each new value

    It works perfectly, i amend it in order to perform the macro if a new value is entered in column 2 of any of the first 3 sheets of the workbook ( or "H&M&, or "crew health" or "A&I P&I"),

    Is there a way that when it add the new value in sheet "parameters" column 5 , it does it as per "parameters" format, without adding a new step copying the format?

    cause it add it but as per initial sheet format (ex in red center) while in "parameters" it's in black
    Attached Files Attached Files

  5. #5
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: populate list with each new value

    adapting Bakerman's code
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim where, where1 As Range, Vsl As String
    
    If Target.Column <> 2 Then Exit Sub
    
    If Target.Value = vbNullString Then Exit Sub
    
    If Target.Column = 2 Then
    
    Vsl = Target.Value
    
        If Sh.Name <> "Parameters" Then
            fRow = Application.Match(Target.Value, Sheets("Parameters").Columns(5), 0)
                If IsError(fRow) Then
                    With Sheets("Parameters")
                        With .Range("E" & .Rows.Count).End(xlUp).Offset(1)
                            .Value = Vsl
                            .HorizontalAlignment = xlLeft
                            .Font.Color = xlblack
                        End With
                    .Range("E2", .Range("E" & .Rows.Count).End(xlUp)).Sort .Range("E2"), xlAscending
                    End With
                End If
        End If
    End If
    End Sub

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: populate list with each new value

    In ThisWorkbook module.

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    
        If Target.Column <> 2 Then Exit Sub
        If Target.Value = vbNullString Then Exit Sub
    
        If Evaluate("count(search({""H&M"",""CREW HEALTH"",""A&I P&I""},""" & Sh.Name & """))=1") Then
            fRow = Application.Match(Target.Value, Sheets("Parameters").Columns(5), 0)
            If IsError(fRow) Then
                With Sheets("Parameters")
                    .Range("E" & .Rows.Count).End(xlUp).Offset(1) = Target.Value
                    .Range("E2", .Range("E" & .Rows.Count).End(xlUp)).Sort .Range("E2"), xlAscending
                End With
            End If
        End If
        
    End Sub
    Fontcolor was a remainder of earlier attempts , just had to put entire column E back to Automatic.

    Changed your Datavalidation to a Dynamic Range to avoid empty rows at the bottom.(only1 now to allow new entries)
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    10-07-2015
    Location
    cyprus
    MS-Off Ver
    Microsoft 365
    Posts
    182

    Re: populate list with each new value

    thank you so much all works perfectly

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: populate list with each new value

    You're welcome.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ 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. Replies: 10
    Last Post: 08-01-2018, 01:15 PM
  2. Replies: 1
    Last Post: 11-05-2013, 12:40 AM
  3. Populate list from single click, add to list from additional clicks.
    By Pewpewpew in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-19-2013, 02:07 PM
  4. Replies: 7
    Last Post: 06-12-2013, 07:09 PM
  5. Replies: 2
    Last Post: 02-03-2013, 02:43 AM
  6. Replies: 3
    Last Post: 02-28-2012, 11:54 AM
  7. Replies: 4
    Last Post: 02-08-2012, 03:14 PM

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