How to populate Word userform textbox with results from Combobox If statement results

User selects a name from a combobox drop-down (John Brown).

          If cmbPMname.Value = "John Brown" Then txtPMphone.Value = "(810) 766-07659"

The phone number goes into the Word document using a bookmark called "PMPhone".

         .Bookmarks("PMPhone").Range.Text  =  txtPMPhone.Value

I have a textbox called txtPMPhone on the userform.  I want the telephone number for the

selected name, in this case "John Brown" to also fill into the txtPMPhone textbox so it is visible on the userform.

I don't know how to write the code, and would really appreciate some help.  Obviously very new at this.

 

Answer
Answer
If you are listing known names I would probably use a list box rather than a combo box, but nevertheless you need something like

Private Sub UserForm_Initialize()
With Me.cmbPMname
    .AddItem "John Brown"
    .AddItem "Fred Smith"
    .AddItem "Bill Jones"
    .ListIndex = 0
End With
End Sub

to populate the box and select the first item which will show the associated phone number of that first item in the text box. To account for alternative selections you then need to define the text box values associated with the names

Private Sub cmbPMname_Change()
Select Case cmbPMname.Value
    Case Is = "John Brown": txtPMphone.Value = "(810) 766-07659"
    Case Is = "Fred Smith": txtPMphone.Value = "(810) 785-12345"
    Case Is = "Bill Jones": txtPMphone.Value = "(810) 901-76543"
    Case Else: txtPMphone.Value = "No phone listed"
End Select
End Sub

The Case Else statement allows for the fact that you are using a combo box and the user may enter some name that is not associated with a phone number. The user would still be able to over-write the value in the text box if the text box is editable.
Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated June 22, 2023 Views 1,873 Applies to: