Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi-column combobox - populate from a row in Excel

Status
Not open for further replies.

davedave24

Programmer
Aug 26, 2010
113
GB
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
 
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,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
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
 
Duh! So sorry!

If Cell.Column Mod 2 = 0 Then

...or = 1

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top