+ Reply to Thread
Results 1 to 3 of 3

Combining 2 combobox value together but do not repeat twice if value is the same

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Ottawa, Ontario
    MS-Off Ver
    Excel 2013
    Posts
    104

    Combining 2 combobox value together but do not repeat twice if value is the same

    Hello,

    I am using a userform with comboboxes to collect information from the user. The user can decide to enter only 1 set of data which consist of 4 comboboxes (combobox #1-4). However, the user can also provide an optional 2nd set of data which repeat the same questions as the 1st set of data and use combobox #5-8. I would like then the data to be inputted into the worksheet when the user click on the button and that this data be group together under a single cell for each of the same question: for example combobox #1 and #5 are put into the same cell B2, combox #2 and #6 in cell C2, etc. I was able to accomplish that.

    My problem is that I would like to group together only the data for the 1st and 2nd set of data if they are different. So if the answer to combobox #1 and #5 are the same, I don’t want them to be repeated twice in the same cell as there is no need for that (I just want 1 of the same answer) but if the answer of combox #2 and #6 are different then I would like to see those 2 different answers in the same cells.

    I have included a sample file as well as the part of the code below and in red where I believe where it needs to be updated. Thank you for your time and help!

    Private Sub cmdAdd_Click()
    Dim iRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Entry")
    
    'find first empty row in database
    iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    
    'check for a part number
    If Trim(Me.ComboBox1.Value) = "" Then
      Me.ComboBox1.SetFocus
      MsgBox "Please enter a Vehicule type"
      Exit Sub
    End If
    
    'copy the data to the database
    'use protect and unprotect lines,
    '     with your password
    '     if worksheet is protected
    With ws
    '  .Unprotect Password:="password"
      .Cells(iRow, 2).Value = Me.ComboBox1.Value & " " & ComboBox5.Value
      .Cells(iRow, 3).Value = Me.ComboBox2.Value & " " & ComboBox6.Value
      .Cells(iRow, 4).Value = Me.ComboBox3.Value & " " & ComboBox7.Value
      .Cells(iRow, 5).Value = Me.ComboBox4.Value & " " & ComboBox8.Value
    '  .Protect Password:="password"
    End With
    
    'clear the data
    Me.ComboBox1.Value = ""
    Me.ComboBox2.Value = ""
    Me.ComboBox3.Value = ""
    Me.ComboBox4.Value = ""
    Me.ComboBox5.Value = ""
    Me.ComboBox6.Value = ""
    Me.ComboBox7.Value = ""
    Me.ComboBox8.Value = ""
    Me.ComboBox1.SetFocus
    
    End Sub
    'Source for command add button example:http://www.contextures.com/xlUserForm01.html
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Combining 2 combobox value together but do not repeat twice if value is the same

    
    With ws
    '  .Unprotect Password:="password"
    
    For count = 1 to 4
    if Me.controls("ComboBox" & Count).Value = Me.controls("ComboBox" & Count +1).Value then
     .Cells(iRow, Count +1).Value =Me.controls("ComboBox" & Count).Value
    Else
     .Cells(iRow, Count +1).Value = Me.controls("ComboBox" & Count).Value & " " & Me.controls("ComboBox" & Count +1).Value
    End If
    Next
    
    '  .Protect Password:="password"
    End With
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Ottawa, Ontario
    MS-Off Ver
    Excel 2013
    Posts
    104

    Re: Combining 2 combobox value together but do not repeat twice if value is the same

    Quote Originally Posted by mehmetcik View Post
    
    With ws
    '  .Unprotect Password:="password"
    
    For count = 1 to 4
    if Me.controls("ComboBox" & Count).Value = Me.controls("ComboBox" & Count +1).Value then
     .Cells(iRow, Count +1).Value =Me.controls("ComboBox" & Count).Value
    Else
     .Cells(iRow, Count +1).Value = Me.controls("ComboBox" & Count).Value & " " & Me.controls("ComboBox" & Count +1).Value
    End If
    Next
    
    '  .Protect Password:="password"
    End With
    Hello Mehmetcik,

    Thank you for your help and time. It is working but I had to modify part of the code to show +4 instead of +1 (FYI, see correction in red below). Thank you so much for your quick response and help, greatly appreciated! I will mark as solved.

    With ws
    '  .Unprotect Password:="password"
    For Count = 1 To 4
    If Me.Controls("ComboBox" & Count).Value = Me.Controls("ComboBox" & Count + 4).Value Then
     .Cells(iRow, Count + 1).Value = Me.Controls("ComboBox" & Count).Value
    Else
     .Cells(iRow, Count + 1).Value = Me.Controls("ComboBox" & Count).Value & " " & Me.Controls("ComboBox" & Count + 4).Value
    End If
    Next
    
    '  .Protect Password:="password"

+ 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] ComboBox Show Non Repeat List
    By oskar44 in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 07-11-2015, 08:46 PM
  2. Replies: 4
    Last Post: 03-08-2014, 08:01 AM
  3. how to repeat combobox
    By nityarajan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-30-2012, 04:15 PM
  4. ComboBox AddItem will repeat the list
    By Siops in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2012, 12:15 PM
  5. Combining object (textbox and combobox) change events
    By abousetta in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-11-2011, 10:44 PM
  6. ComboBox ListFillRange: Items Repeat in Dropdown List
    By Bosco6 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-23-2011, 02:51 PM
  7. Combining combobox value into range
    By benno87 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-22-2011, 09:26 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