Userform Combobox associated values

AnnetteTR

Board Regular
Joined
Aug 19, 2010
Messages
85
Hi
I have a Userform with more three comboboxes.
When the user has chosen a value in combobox "Forstevalg" I also want to get the value next to the value from the list.
Excel Workbook
GH
15OverskrifterEnheder
16arealm
17rumhjdem
18luftskifte/h
19Antal personerpers
20Luftmngde per personm/h pr pers
21Sikkerhedsbnk antalstk
22Sikkerhedsbnk breddemm
23Sikkerhedsbnk Minm/h
...


Forstevalg is chosen from the list "Overskrifter" with this code:

With cbxSortering
.RowSource = "Overskrifter"
.ListIndex = 4
End With

I want to get the associated value "Unit" from the list next to it (named "Enheder")

Can you tell med the code for that?

Regards Annette
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I would change the combobox's columncount to 2, remove the RowSource, hide the second column and then put both columns of data into it:
Code:
With cbxSortering
.List = Range("Overskrifter").Resize(, 2).Value
.ListIndex = 4
End With

You can then simply refer to:
Code:
With cbxSortering
  varVal = .List(.Listindex, 1)
End With
 
Upvote 0
Hi rorya<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Thank you for your answer.<o:p></o:p>
I can see that initial the value of varVal is the corresponding "Enheder" to the initial value of "Overskrifter", but when the user changes the value of "Overskrifter" i want varVal to change correspondantly.<o:p></o:p>
<o:p></o:p>
ie. I got a makro called "Sub OutputPrAnlag()" this calls the userform "frmOutput"<o:p></o:p>
in "frmOutput i got the comboboxes - and the code shown above.<o:p></o:p>
<o:p></o:p>
On the form i got the sub:<o:p></o:p>
Rich (BB code):
<o:p></o:p>
Private Sub CmdOK_Click()<o:p></o:p>
<o:p></o:p>
Me.Hide<o:p></o:p>
<o:p></o:p>
End Sub 
<o:p></o:p>
<o:p></o:p>
and<o:p></o:p>
Rich (BB code):
<o:p></o:p>
Private Sub UserForm_Activate()<o:p></o:p>
<o:p></o:p>
With cbxSortering<o:p></o:p>
.RowSource = "Overskrifter"<o:p></o:p>
.ListIndex = 4<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
With cbx1Vis<o:p></o:p>
.List = Range("Overskrifter").Resize(, 2).Value<o:p></o:p>
.ListIndex = 39<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
With cbx1Vis<o:p></o:p>
varVal = .List(.ListIndex, 1)<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
MsgBox " " & varVal<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
With cbx2Vis<o:p></o:p>
.RowSource = "Overskrifter"<o:p></o:p>
.ListIndex = 40<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
End Sub<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
back in the macro "OutputPrAnlag"<o:p></o:p>
I get the value Forstevalg with this code:<o:p></o:p>
Rich (BB code):
Forstevalg = frmOutput.cbx1Vis.Text<o:p></o:p>
<o:p></o:p>
but how do I get the new value of varVal ?<o:p></o:p>
Regards Annette<o:p></o:p>
 
Upvote 0
You would use:

Code:
with frmOutput.cbx1Vis
   Forstevalg = .Text
   varVal = .List(.ListIndex, 1)
End With

in your calling routine.
 
Upvote 0
Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
This works - thank you.<o:p></o:p>
<o:p></o:p>
But now I got a new question:<o:p></o:p>
now I want to use varVal as text in format - I try to use this syntax, but something is wrong:<o:p></o:p>
Rich (BB code):
With ActiveSheet.PivotTables("ItemList").PivotFields("Sum af " & Forstevalg)
    .NumberFormat = "#,##0 " & varVal
End With
<o:p></o:p>

It works if I replace
Rich (BB code):
.NumberFormat = "#,##0 " & varVal
with
Rich (BB code):
.NumberFormat = "#,##0 ""m³/h"""

Regards Annette
 
Upvote 0
I suspect you want:
Code:
.NumberFormat = "#,##0 """ & varVal & """"
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,419
Members
452,325
Latest member
BlahQz

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