INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Multi-column combobox - populate from a row in Excel

Multi-column combobox - populate from a row in Excel

(OP)
Hi all

I have a combobox with 2 columns. I need it to be filled with Names in col1, and Email in col2.

The data comes from an Excel sheet. We find the row relating to the specific customer, then grab the data, starting at column C.

The data is stored: NAME1 EMAIL1 NAME2 EMAIL2 NAME3 EMAIL3 etc.

What I have so far jumbles everything up, so the combobox ends up like this:

NAME1 EMAIL1
EMAIL1 NAME2
NAME2 EMAIL2
EMAIL2 etc.

CODE

Dim cell As Range
    Dim Rng As Range
    
    With ThisWorkbook.Sheets("Customers")
        .Activate
        Set Rng = .Range("C" & SelCustomer + 2, .Range("C" & SelCustomer + 2).End(xlToRight))
    End With
    
    For Each cell In Rng.Cells
        With Me.comboTo
            .AddItem cell.Value
            .List(.ListCount - 1, 1) = cell.Offset(0, 1).Value
        End With
    Next cell 

SelCustomer is just an integer to locate the row we're using.

Thanks

RE: Multi-column combobox - populate from a row in Excel

Hi,

CODE

'
   For Each cell In Rng.Cells
        With Me.comboTo
          If cell Mod 2 = 0 Then
            .AddItem cell.Value
            .List(.ListCount - 1, 1) = cell.Offset(0, 1).Value
          End If
        End With
    Next cell 
...or it might be If cell Mod 2 = 1.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Multi-column combobox - populate from a row in Excel

(OP)
Hi Skip
Thanks for your help. I was getting a Type Mismatch with that code.

I did in the end get it to work properly with a different method:

CODE

'select the row for the customer
        .Cells(SelCustomer, 3).Select
        
        'find the last column in that row
        Dim LastCol As Integer
        LastCol = .Cells(SelCustomer, .Columns.Count).End(xlToLeft).Column
      
        'loop through the row, adding the name the column 1 and the email to column 2 of both comboboxes
        Dim i As Integer
        For i = 3 To LastCol Step 1     'start at column 3 (first name)
            If .Cells(SelCustomer, 1).Value <> vbNullString Then
                comboTo.AddItem .Cells(SelCustomer, i).Value        'name
                comboTo.List(comboTo.ListCount - 1, 1) = .Cells(SelCustomer, i + 1).Value   'email
                i = i + 1       'move on one column to the next name
            End If
        Next i 

RE: Multi-column combobox - populate from a row in Excel

Duh! So sorry!

If Cell.Column Mod 2 = 0 Then

...or = 1

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close