Creating an interactive userform

pvman

New Member
Joined
Apr 4, 2006
Messages
37
Office Version
  1. 2010
Platform
  1. Windows
Hello to all experts. Perhaps you can help me with the following problem:

I am a novice VBA user, with a more-than-basic knowledge.
I have created several userforms in the past, but they were all “static”, meaning, the user was allowed certain options from a variety of combo boxes, and after filling all the data, the result was migrated into a sheet (database).

Now, I am trying to create an INTERACTIVE userform in which certain combo boxes will become available based on a user’s selection in a previous combo in the same form.
For example:
If - in a “Payment” combo - the user will select “Check”, 3 new text boxes will appear (Date, Sum, Bank code) and the user will fill certain data related to his selection.
If, on the other hand, the user will select (in the “Payment” box) the option “Installments”, 3 other boxes will appear (Number of installments, first installment month, sum per installment). These boxes can either be text boxes of combo boxes, and here the user will select/fill the relevant data.

Naturally, the previous boxes, related to the “Check” option, will be made invisible.
Personally, I don’t care if ALL boxes are always visible, as long as they are activated or disabled (grayed out) based on user’s selection in the “Payment” combo.

The next step will be to migrate the selected/filled data from the form back into a sheet, where the data will be placed in different columns based on the final selection the user made in the form.

Can any one help me out with a sample file of a basic code that I can work with?
After reading here, I ran a search on "Generic Events" and "Class Modules", but I must admit that I did not really understand how to use the answers to suit my needs.

TIA
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Before selection
28u00mb.jpg


After Selection

2vw69af.jpg


use the .VISIBLE property to hide and display fields based on combo box selections..

This should get you started

Code:
Private Sub cb_tender_Change()
    If cb_Tender = "Check" Then
        tb_date.Visible = True
        tb_sum.Visible = True
        tb_bankcode.Visible = True
    End If
End Sub
 
Upvote 0
Wow… That WAS fast... Thanks :pray:
Can you please tell me should I put this code?
In the code of the form? In a Module? etc...

I saw some threads putting code in both "This worksheet" AND in a "Class Module". does this apply here, as well?

BR
 
Upvote 0
Another example for you to work with

Create a fresh Userform, put on it 1 ComboBox, 2 labels and 2 textboxes

Then in the code window paste this test code (sorry it's a bit scrappy but it does work)

Load your Userform and Interact with it, just apply the principles to your own form

Code:
Private Sub ComboBox1_Change()
Select Case ComboBox1.Value
Case Is = "Payment"
Label1.Caption = "Opt1"
TextBox1.Visible = True
TextBox2.Visible = False
Label2.Caption = ""
Case Is = "Interest"
Label1.Caption = "See"
TextBox1.Visible = True
TextBox2.Visible = True
Label2.Caption = "Test"
Case Else
Label1.Caption = ""
TextBox1.Visible = False
TextBox2.Visible = False
Label2.Caption = ""
End Select
End Sub


Private Sub UserForm_Initialize()
With ComboBox1
    .AddItem "Payment"
    .AddItem "Interest"
End With
Label1.Caption = ""
Label2.Caption = ""
TextBox1.Visible = False
TextBox2.Visible = False
End Sub
 
Upvote 0
Thank you both, phxsportz & Dave3009.
I assume that in Dave3009's reply the code also goes in the userform's code.
I will give both options a test run, and see which is easier for me to follow.

All the best
 
Upvote 0
Yes it goes in the Userform code.
 
Upvote 0
Okay... I started with the code from phxsportz and I get strange results, as if the code does not recognize the text boxes.
This is the code I am using:
Code:
Private Sub cbo_pay_meth_Change()
    If cbo_pay_meth = "Installments" Then
        Cbo_Payment_count.Visible = True
        Txt_first_payment = True
        Txt_next_payment = True
        Cbo_First_payment_month.Visible = True
    Else
        Cbo_Payment_count.Visible = False
        Txt_first_payment = False
        Txt_next_payment = False
        Cbo_First_payment_month.Visible = False
    End If
End Sub

When "Istallments" is selectd, I see all boxes (with the word "True" IN the TEXT boxs). When I select something else, the Combo boxes disapear but the text boxes remain visible AND both show the word "False" IN them.
I don't know how to upload an image, so you get a visual description instead...

I have copy/paste the names of all the controls, so I don't think it a spelling issue.

Any ideas?
 
Last edited:
Upvote 0
Ya forgot the .VISIBLE on the Textboxes

Code:
Private Sub cbo_pay_meth_Change()
    If cbo_pay_meth = "Installments" Then
        Cbo_Payment_count.Visible = True
        Txt_first_payment.Visible = True
        Txt_next_payment.Visible = True
        Cbo_First_payment_month.Visible = True
    Else
        Cbo_Payment_count.Visible = False
        Txt_first_payment.Visible = False
        Txt_next_payment.Visible = False
        Cbo_First_payment_month.Visible = False
    End If
End Sub
 
Last edited:
Upvote 0
****... I should kick myself. I checked it 7 times and overlooked it...
Now it works great and I can continue with developing the form and the code (I hope I will not need to bother you guys again).

Again, thank you very much for your time and attention.

BR
 
Upvote 0

Forum statistics

Threads
1,215,578
Messages
6,125,642
Members
449,245
Latest member
PatrickL

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