Combo ListIndex?

LEXCERM

Active Member
Joined
Jun 26, 2004
Messages
320
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a combo on a userform which retruns 300 records. The data is something like this:-

Name Dept
John........AG1
Steve......AG2
Mary....... AG3
Dave........AG3
Dennis......AG4
Michael.....AG4
Wayne......AG5 and so on

How can I get the combo list to start at, say, AG5 rather than the first record?

Thanks,
Paul.
 
Depends on your setup and use of named ranges and how you filled the combobox. A Find on the range could find your first row.
 
Upvote 0
Thanks Kenneth Hobson for replying.

I have the following in the rowsource of the combo:-
=offset($A$1,1,0,COUNTA($A:$A),2)

When the userform initally opens, it selects the relevant sheet first.

I understand your FIND concept, but not sure how I would apply it to the combo event.

Thanks again,
Paul.
 
Upvote 0
Paul

What is it you actually want to populate the combobox with?

Is it always row 5 and the subsequent rows?

Using a dynamic named range is one method but there are plenty of others.
 
Upvote 0
Hi Norie,

No, it's not always row 5 - that was just an example. Sorry if I am not explaining my problem clearly.

If we take AG5 as an example. In real terms, dept AG5 is in row 220 of the spreadsheet so you need to scroll down the combo on the userform to get to it. When new records are added or deleted, AG5 could then start at a different row. The Dept's are sorted A>Z.

I am wanting the combo to react so that when a user clicks it, it goes directly to the first AG5 record in the combo rather than having the user scroll down to it.

There are other combo's that do similar things, but once I have learnt this conecpt, I can apply it to others.

Paul.
 
Upvote 0
Here are 2 methods to set the rowsource.
Code:
Private Sub UserForm_Initialize()
  Dim r As Range
  ComboBox1.ColumnCount = 2
  ActiveWorkbook.Names.Add Name:="MyRange", RefersToR1C1:= _
    "=OFFSET(Sheet1!R1C1,1,0,COUNTA(Sheet1!C1),2)"
      
  ComboBox1.RowSource = Range("MyRange").Address
  
  Set r = Range("MyRange").Find("AG5", , xlValues)
  ComboBox1.RowSource = Range(r.Offset(0, -1).Address, Cells(Rows.Count, 1).End(xlUp).Address).Address
  ComboBox1.ListIndex = 0
End Sub
 
Upvote 0
Thanks Keneth Hobson, that's just what I was looking for!

One final question if I may.

I have 10 combos on my UserForm (top to bottom) and alongside each one there are 5 textboxes on each row. There is a Change event which updates textboxes to the right of each combo. Rather than referencing each one by name (which is time consuming) i.e.

Me.TextBox1.value = 123
Me.TextBox2.value = 456 etc

... can I do something like:-

Me.ActiveControl.(offset to 1st textbox) = 123
Me.ActiveControl.(offset to 2nd textbox) = 456

Regards,
Paul.
 
Upvote 0
Depends on how you set it up. Say you made ComboBox1 with TextBox1 to TextBox5 in one row, copied and pasted twice. You now have 3 comboboxes and 15 textboxes.

In this example, as the combobox value is changed, the 5 textbox values are updated. Be sure to uncomment and put the first line in a Module. Obviously, you will need to change the textbox values to meet your needs. In this case, I just concatenated a number to the combobox value for each of the 5 textboxes.

Code:
Option Explicit
'Public cCB() As Control, cTB() As Control 'In a Module
Private Sub UserForm_Initialize()
  FillControlArrays
End Sub

Private Sub ComboBox1_Change()
  FillTBs
End Sub

Private Sub ComboBox2_Change()
  FillTBs
End Sub

Private Sub ComboBox3_Change()
  FillTBs
End Sub

Private Sub FillControlArrays()
  Dim iCB As Integer, iTB As Integer, c As Control
  For Each c In Me.Controls
    Select Case TypeName(c)
      Case "TextBox"
        iTB = iTB + 1
        ReDim Preserve cTB(1 To iTB)
        Set cTB(iTB) = c
       Case "ComboBox"
        iCB = iCB + 1
        ReDim Preserve cCB(1 To iCB)
        Set cCB(iCB) = c
      End Select
    Next c
End Sub

Private Sub FillTBs()
Dim offset As Integer, i As Integer, j As Integer
  
  'Set offsets for textbox controls based on combobox
  Select Case ActiveControl
    Case ComboBox1
      offset = 0
    Case ComboBox2
      offset = 5
    Case ComboBox3
      offset = 10
    Case Else
      offset = -1
  End Select
      
  If offset = -1 Then Exit Sub
  'Fill TextBox controls will values based on combobox that is active and offset
  For i = 1 To 5
    j = i + offset
    cTB(j) = ActiveControl.Value & i
  Next i
End Sub
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top