Getting a Cell Reference from a Combo Box in a User Form

gtvkeith

Board Regular
Joined
Mar 15, 2006
Messages
78
Hi all,

I am trying to find a Cell reference (a row number will do) from a named range in a worksheet by selecting an entry from a Combo Box.

I should point out that I am using a Combo Box from the Visual Basic Toolbox, in a User Form, not in a Worksheet, in Excel 2003.

I need to generate the Cell reference / row number as the form needs to allow the user to edit the value of that cell (in the Combo Box) when it has been selected and save it.

I.e. select "Production Resources" from the list in the Combo Box, (from the Range listed in the Row Source) and change the value in the Combo Box field to "Production Operations" and click a Command Button to save the new Combo Box value into the Cell reference of the old value.

At the moment I a have a Text box and two Command Buttons to move up and down the list, but this takes some time when there are over eighty items in the list.

This question is in a similar vein to others on the board but they don't quite give me the answer I'm looking for.

I am happy to post the existing code that I am using, but there is a fair bit of it.

Any help on this will be greatly appreciated.

Keith
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Code:
With ComboBox1
     MsgBox Range(.RowSource).Cells(.ListIndex + 1,1).Address & " is where the selected item is located"
End With
 
Upvote 0
It can error when nothing is selected and the RowSource is in the Row 1, but I assume (silly me) that the OP is already testing for the "nothing selected" case in the exisiting code.
 
Last edited:
Upvote 0
Here is a template I use that is close to what you are looking for, I think. The behavior is such. Some values are added from a named range to the combobox. If a user selects an item and edits it, the range reflects the edit in realtime. The combobox list is updated as well. However, when the combobox's Exit event fires, the user is prompted to commit the changes. (Yes or No) If no is chosen, the original values are restored to the range and the combobox is updated with the original values as well. It might be easier to download the example and see if the behavior fits.

<a href="http://home.fuse.net/tstom/0725081235.332447.zip"><img src="http://home.fuse.net/tstom/zip.gif"width="48"height="48"border="0"></a> <a href="http://home.fuse.net/tstom/0725081235.332447.zip">0725081235.332447.zip</a>

To create the example above, add UserForm1 containing ComboBox1 and CommandButton1. Enter some data on Sheet1 in range A1:A10. Name this range "ComboBox1RowSource".

Enter this code into the userform:

Code:
Option Explicit

Private LISelected As Long
Private LIRange As Range
Private IgnoreChange As Boolean
Private ListItems As Variant
Private WasEdited As Boolean

Private Sub ComboBox1_Change()
    If IgnoreChange Then Exit Sub
    
    If ComboBox1.ListIndex <> -1 Then
        LISelected = ComboBox1.ListIndex
    Else
        If LISelected <> -1 Then
            LIRange(LISelected + 1) = ComboBox1.Text
            IgnoreChange = True
            UpdateComboList
            IgnoreChange = False
            WasEdited = True
        End If
    End If
End Sub

Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If IgnoreChange Then
        IgnoreChange = False
        Exit Sub
    End If
    
    If WasEdited Then
        If MsgBox("Commit changes?", vbYesNo + vbCritical) <> vbYes Then
            LIRange = ListItems
            IgnoreChange = True
            UpdateComboList
            ComboBox1.Text = "Select an Item"
            IgnoreChange = False
        End If
        WasEdited = False
    End If
End Sub

Private Sub UserForm_Initialize()
    LISelected = -1
    Set LIRange = [ComboBox1RowSource]
    ListItems = LIRange
    ComboBox1.MatchEntry = fmMatchEntryNone
    UpdateComboList
    ComboBox1.Text = "Select an Item"
End Sub

Private Sub UpdateComboList()
    ComboBox1.Clear
    ComboBox1.List = Application.Transpose(LIRange)
End Sub

Private Sub CommandButton1_Click()
    Unload Me
End Sub

Note that your named range must be a contigous range of cells within a single column. The code could be edited to accept other ranges, but Iv'e had no need to bother with it to this point... :)

Tom
 
Upvote 0
Hi, Tom
Good one.

One thing, (because I don't like Tranpose Function)
Can List property be replaced with Column property?
Rich (BB code):
    ComboBox1.Column = LIRange.Value
 
Upvote 0
Wow,

Thankyou guys for that, Tom, I think you've gone over and above on this one.

I'm not quite sure yet if I should add this on to what I already have or rebuild my form around yours, but you have solved a couple of issues that were looming.

Once again, thankyou all for your input, I'm going to have a busy afternoon.

Keith.
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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