Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default preselecting in combo box

I'm having a userform which on initialization should select one of the
values in the combo box control. This selection should be based on the
value of particular cell.
For example, if cell (B11) = 20, the selected value in comboBox upon
initializatino should be 20 which is second entry in the complete list
of (10,20,30,40).
Please suggest solution for this.
Appreciate any help.

thanks,
Anand.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default preselecting in combo box

Private Sub UserForm_Initialize()
Dim iCt As Integer

iCt = -1
For Each c In Range(ComboBox1.RowSource)
iCt = iCt + 1
If c = Range("B11") Then
ComboBox1.ListIndex = iCt
Exit Sub
End If
Next c

End Sub

The above assumes the RowSource property has been set and the sheet
with the relevant B11 is the ActiveSheet.

Hth,
Merjet

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default preselecting in combo box

On May 22, 3:53 pm, merjet wrote:
Private Sub UserForm_Initialize()
Dim iCt As Integer

iCt = -1
For Each c In Range(ComboBox1.RowSource)
iCt = iCt + 1
If c = Range("B11") Then
ComboBox1.ListIndex = iCt
Exit Sub
End If
Next c

End Sub

The above assumes the RowSource property has been set and the sheet
with the relevant B11 is the ActiveSheet.

Hth,
Merjet


I have multiple combo boxes to be filled in with such data. In some
cases, the data comes from a defined RowSource. For some it is
populated through some set of cells. How can I achieve same results
with the comboBox populated using addItem()?

Thanks,
Anand.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 421
Default preselecting in combo box

Hi Anand,

In the Userform module, try something
like:

'===========
Private Sub UserForm_Initialize()
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Dim rCell As Range
Dim Res As Variant
Dim arr As Variant

Set WB = ThisWorkbook '<<===CHANGE
Set SH = WB.Sheets("Sheet1") '<<===CHANGE

With SH
Set Rng = .Range("A1:A10") '<<===CHANGE
Set rCell = .Range("B3") '<<===CHANGE
End With

arr = Rng.Value
Res = Application.Match(rCell.Value, arr, 0)

With Me.ComboBox1
.List = arr
If Not IsError(Res) Then
.ListIndex = Res - 1
End If
End With
End Sub
'<<==========



---
Regards.
Norman


"Anand" wrote in message
...
On May 22, 3:53 pm, merjet wrote:
Private Sub UserForm_Initialize()
Dim iCt As Integer

iCt = -1
For Each c In Range(ComboBox1.RowSource)
iCt = iCt + 1
If c = Range("B11") Then
ComboBox1.ListIndex = iCt
Exit Sub
End If
Next c

End Sub

The above assumes the RowSource property has been set and the sheet
with the relevant B11 is the ActiveSheet.

Hth,
Merjet


I have multiple combo boxes to be filled in with such data. In some
cases, the data comes from a defined RowSource. For some it is
populated through some set of cells. How can I achieve same results
with the comboBox populated using addItem()?

Thanks,
Anand.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default preselecting in combo box

Modify the loop like this:

For Each c In ComboBox1.List
iCt = iCt + 1
If CInt(c) = Range("B11") Then
ComboBox1.ListIndex = iCt
Exit Sub
End If
Next c

Be careful about types. When I fill the List with numbers, they are
taken as Text.

Hth,
Merjet



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default preselecting in combo box

On May 22, 4:43 pm, merjet wrote:
Modify the loop like this:

For Each c In ComboBox1.List
iCt = iCt + 1
If CInt(c) = Range("B11") Then
ComboBox1.ListIndex = iCt
Exit Sub
End If
Next c

Be careful about types. When I fill the List with numbers, they are
taken as Text.

Hth,
Merjet


Now I want to create this as function say, prefillComboBox(cbox as
Object, ValRg as Range, matchVal as string).
However, I'm not sure how I can use the range passed through this
function inside the function. the Range() method does not work with
the ValRg parameter.
Appreciate any help.

thanks,
Anand.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filtering or preselecting columns gregfrgusa Excel Discussion (Misc queries) 2 May 5th 08 07:14 PM
Excel VBA Combo Box Populating dependent on other combo box choices ikabodred Excel Programming 1 March 15th 06 03:16 PM
Filtered list for Combo Box ListFillRange - Nested Combo Boxes DoctorG Excel Programming 3 February 23rd 06 12:15 PM
"Combo Box - getting control combo box to stick in place in worksh ajr Excel Discussion (Misc queries) 1 February 16th 05 02:05 AM
"Combo Box - getting control combo box to stick in place in worksh ajr Excel Discussion (Misc queries) 0 February 15th 05 07:45 PM


All times are GMT +1. The time now is 05:46 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"