+ Reply to Thread
Results 1 to 21 of 21

change code of combobox to not duplicate values

Hybrid View

matwork change code of combobox to... 08-14-2013, 05:01 AM
millz Re: change code of combobox... 08-14-2013, 05:59 AM
MickG Re: change code of combobox... 08-14-2013, 06:10 AM
jindon Re: change code of combobox... 08-14-2013, 06:55 AM
matwork Re: change code of combobox... 08-14-2013, 07:10 AM
jindon Re: change code of combobox... 08-14-2013, 07:15 AM
matwork Re: change code of combobox... 08-14-2013, 07:20 AM
jindon Re: change code of combobox... 08-14-2013, 07:24 AM
matwork Re: change code of combobox... 08-14-2013, 07:42 AM
jindon Re: change code of combobox... 08-14-2013, 07:54 AM
matwork Re: change code of combobox... 08-14-2013, 08:02 AM
matwork Re: change code of combobox... 08-14-2013, 09:36 AM
matwork Re: change code of combobox... 08-14-2013, 10:17 AM
jindon Re: change code of combobox... 08-14-2013, 10:19 AM
matwork Re: change code of combobox... 08-14-2013, 10:27 AM
jindon Re: change code of combobox... 08-14-2013, 10:39 AM
matwork Re: change code of combobox... 08-15-2013, 04:31 PM
matwork Re: change code of combobox... 08-16-2013, 06:59 AM
jindon Re: change code of combobox... 08-16-2013, 07:40 AM
matwork Re: change code of combobox... 08-16-2013, 08:04 AM
matwork Re: change code of combobox... 08-16-2013, 09:16 AM
  1. #1
    Registered User
    Join Date
    07-16-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    25

    change code of combobox to not duplicate values

    Hi people.

    Can anyone help me please? I want that the Combox1 don't show the duplicated values. Thanks

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Target.Count > 1 Then Exit Sub
        If Target.Address <> "$C$1" Then Exit Sub
    
    
        rws = ActiveSheet.UsedRange.Columns(1).Rows.Count
    
    
        ComboBox1.Clear
    
        Set r = Range(Cells(2, 1), Cells(rws, 1))
    
        For Each c In r.Cells
    
            y = Application.WorksheetFunction.CountIf(Range(Cells(1, 1), Cells(c.Row, 1)), c)
    
            If y = 1 Then ComboBox1.AddItem c
    
        Next c
    
        With ComboBox1
    
            .Activate
            .Application.SendKeys ("%{down}")
            
        End With
    
    
    End Sub

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: change code of combobox to not duplicate values

    See if this works? I added some code that I use for Access.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim notFound As Boolean
        If Target.Count > 1 Then Exit Sub
        If Target.Address <> "$C$1" Then Exit Sub
    
    
        rws = ActiveSheet.UsedRange.Columns(1).Rows.Count
    
    
        ComboBox1.Clear
    
        Set r = Range(Cells(2, 1), Cells(rws, 1))
    
        For Each c In r.Cells
    
            y = Application.WorksheetFunction.CountIf(Range(Cells(1, 1), Cells(c.Row, 1)), c)
    
            If y = 1 Then
                notFound = True
                For k = 0 To ComboBox1.ListCount - 1
                    If c = ComboBox1.ItemData(k) Then
                        notFound = False
                        Exit For
                    End If
                Next
                If notFound Then
                    ComboBox1.AddItem c
                End If
            End If
        Next c
    
        With ComboBox1
    
            .Activate
            .Application.SendKeys ("%{down}")
            
        End With
    
    
    End Sub

  3. #3
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: change code of combobox to not duplicate values

    Perhaps this:-
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Target.Count > 1 Then Exit Sub
        If Target.Address <> "$C$1" Then Exit Sub
    
    
        rws = ActiveSheet.UsedRange.Columns(1).Rows.Count
    
    
        ComboBox1.Clear
    
        Set R = Range(Cells(2, 1), Cells(rws, 1))
    
        For Each c In R.Cells
            y = Application.WorksheetFunction.CountIf(R, c)
            If y = 1 Then ComboBox1.AddItem c
        Next c
    
        With ComboBox1
    
            .Activate
            .Application.SendKeys ("%{down}")
            
        End With
    
    End Sub

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

    Re: change code of combobox to not duplicate values

    Try
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Target.Count > 1 Then Exit Sub
        If Target.Address <> "$C$1" Then Exit Sub
    
        rws = ActiveSheet.UsedRange.Columns(1).Rows.Count
    
        ComboBox1.List = Filter(Evaluate("transpose(if(countif(offset(a2:a" & rws & ",0,0,row(1:" & _
            rws - 1 & ")),a2:a" & rws & ")=1,a2:a" & rws & ",char(2)))"), Chr(2), 0)
    End Sub

  5. #5
    Registered User
    Join Date
    07-16-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: change code of combobox to not duplicate values

    Trabalhadores.xlsm

    Hi. It didnt work. Can u people see the file? i Think its more easier to understand.

    Thanks

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

    Re: change code of combobox to not duplicate values

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Target.Count > 1 Then Exit Sub
        If Target.Address <> "$C$1" Then Exit Sub
    
        rws = ActiveSheet.UsedRange.Columns(1).Rows.Count
        
        ComboBox1.ListFillRange = ""
    
        ComboBox1.List = Filter(Evaluate("transpose(if(countif(offset(a2:a" & rws & ",0,0,row(1:" & _
            rws - 1 & ")),a2:a" & rws & ")=1,a2:a" & rws & ",char(2)))"), Chr(2), 0)
    End Sub

  7. #7
    Registered User
    Join Date
    07-16-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: change code of combobox to not duplicate values

    Quote Originally Posted by jindon View Post
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Target.Count > 1 Then Exit Sub
        If Target.Address <> "$C$1" Then Exit Sub
    
        rws = ActiveSheet.UsedRange.Columns(1).Rows.Count
        
        ComboBox1.ListFillRange = ""
    
        ComboBox1.List = Filter(Evaluate("transpose(if(countif(offset(a2:a" & rws & ",0,0,row(1:" & _
            rws - 1 & ")),a2:a" & rws & ")=1,a2:a" & rws & ",char(2)))"), Chr(2), 0)
    End Sub
    Sorry. i tried this code but the combox1 continue with duplicated names. can u send me the file with that modification? Thanks

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

    Re: change code of combobox to not duplicate values

    See attached.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    07-16-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: change code of combobox to not duplicate values

    Hi. isn't working. when i define the range on combobox1 properties, the names apears duplicated.
    see jpegTrabalhadores.JPG

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

    Re: change code of combobox to not duplicate values

    Don't touch Combobox property in attached file, just click on C1.

  11. #11
    Registered User
    Join Date
    07-16-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: change code of combobox to not duplicate values

    Thank you. its working

  12. #12
    Registered User
    Join Date
    07-16-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: change code of combobox to not duplicate values

    One more thing please. i would like to move the two comboboxs to Sheet2 but mantaining the lists in sheet1. What i have to change in code?

    '----------------------------------------------------------
    Dim rng As Range, c As Range, r As Range, f As Range
    Dim rws As Long, y As Integer
    
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Target.Count > 1 Then Exit Sub
        If Target.Address <> "$C$1" Then Exit Sub
    
        rws = ActiveSheet.UsedRange.Columns(1).Rows.Count
        
        ComboBox1.ListFillRange = ""
    
        ComboBox1.List = Filter(Evaluate("transpose(if(countif(offset(a2:a" & rws & ",0,0,row(1:" & _
            rws - 1 & ")),a2:a" & rws & ")=1,a2:a" & rws & ",char(2)))"), Chr(2), 0)
    End Sub
    
    
    Private Sub ComboBox1_Change()
    
    
        rws = ActiveSheet.UsedRange.Columns(1).Rows.Count
    
        ComboBox2.Clear
    
        Set r = Range(Cells(1, 1), Cells(rws, 1))
    
        For Each c In r.Cells
    
    
            If c = ComboBox1 Then ComboBox2.AddItem c.Offset(0, 1)
    
        Next c
    
        With ComboBox2
    
            .Activate
            .Application.SendKeys ("%{down}")
    
        End With
    
    
    End Sub
    Thanks

  13. #13
    Registered User
    Join Date
    07-16-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: change code of combobox to not duplicate values

    Hi. the combobox1 is already working in sheet2, but combobox2 don't work. Can anyone help me? Thanks

    '----------------------------------------------------------
    Dim rng As Range, c As Range, r As Range, f As Range
    Dim rws As Long, y As Integer
    
    
    
    Private Sub ComboBox2_Change()
    
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Target.Count > 1 Then Exit Sub
        If Target.Address <> "$C$1" Then Exit Sub
    
        rws = ActiveSheet.UsedRange.Columns(1).Rows.Count
        
        Sheets("Sheet2").ComboBox1.ListFillRange = ""
    
         Sheets("Sheet2").ComboBox1.List = Filter(Evaluate("transpose(if(countif(offset(a2:a" & rws & ",0,0,row(1:" & _
            rws - 1 & ")),a2:a" & rws & ")=1,a2:a" & rws & ",char(2)))"), Chr(2), 0)
    End Sub
    
    
    
    Private Sub ComboBox1_Change()
    
    
        rws = ActiveSheet.UsedRange.Columns(1).Rows.Count
    
        Sheets("Sheet2").ComboBox2.Clear
    
        Set r = Range(Cells(1, 1), Cells(rws, 1))
    
        For Each c In r.Cells
    
    
            If c = Sheets("Sheet2").ComboBox1 Then Sheets("Sheet2").ComboBox2.AddItem c.Offset(0, 1)
    
        Next c
    
        With Sheets("Sheet2").ComboBox2
    
            .Activate
            .Application.SendKeys ("%{down}")
    
        End With
    
    
    End Sub

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

    Re: change code of combobox to not duplicate values

    Sheet2???? where is it?
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    07-16-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: change code of combobox to not duplicate values

    sorry. i didnt upload the file updated.

    Trabalhadores.xlsm

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

    Re: change code of combobox to not duplicate values

    ...................
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    07-16-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: change code of combobox to not duplicate values

    Thank you.

  18. #18
    Registered User
    Join Date
    07-16-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: change code of combobox to not duplicate values

    one more question. please. i would like to have a button in sheet2 to run the Reset combobox1 that is on sheet1

    How can i do it?

    Thanks

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

    Re: change code of combobox to not duplicate values

    What for?

    Why on Sheet2?

    That doesn't make sense to me.

  20. #20
    Registered User
    Join Date
    07-16-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: change code of combobox to not duplicate values

    Quote Originally Posted by jindon View Post
    What for?

    Why on Sheet2?

    That doesn't make sense to me.
    Because i want to hide the sheet1. i don't want people change that data.

    But i need people have possibility of clean combobox and make a new search.

    I already thought another way, that click in button, the system selects the c1 of sheet1. It works but system shows the moves. i already put screenupdating false, but he shows the moves =/ and stays on sheet1. i want he stays in sheet2

    i have this
    Private Sub CommandButton1_Click()
    Application.Goto (ActiveWorkbook.Sheets("Sheet1").Range("c1"))
    End Sub

    i already tried to change but gives error =/

  21. #21
    Registered User
    Join Date
    07-16-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: change code of combobox to not duplicate values

    Hi. Problem solved.

    Private Sub CommandButton1_Click()
    Application.ScreenUpdating = False
    Sheets("Sheet1").Activate
    ActiveSheet.Cells(1, 3).Select
    Sheets("Sheet2").Activate
    End Sub

+ 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. change all duplicate values..............
    By sanjoy.dutta in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-15-2013, 10:31 AM
  2. [SOLVED] Option Buttons to Change ComboBox Values Dynamically
    By Stonesteel15 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-03-2012, 11:48 PM
  3. Code to change LinkedCell of ActiveX ComboBox
    By ozhunter in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-08-2011, 07:10 AM
  4. Find ALL WorkSheets according to 2 ComboBox Values.... Help with Code
    By Corey in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-07-2006, 06:35 PM
  5. Create a new chart for every change of values in a combobox
    By uriel78 in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 04-06-2005, 03:06 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