vba if statement within

Public Schedule Face-to-Face & Virtual Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » VBA if statement within userform

VBA if statement within userform

resolvedResolved · High Priority · Version 2010

Raani has attended:
Excel Advanced course
Excel VBA Intro Intermediate course

VBA if statement within userform


I have a userform where there is one drop down box and a text box beneath it. I'm trying to write an if statement code where, if the 'Standard' option is selected the user has to complete the textbox below with further information, however if any of the other items in the list are selected then the textbox says "N/A". This is the code I have:

Private Sub cboPriority_Change()

If cboPriority.Value = "Standard" Then
txtBooked.Value = ""

Else: txtBooked.Value = "N/A"

End If

End Sub

However when 'Standard' is selected the user is unable to type anything in the textbox, I realise this is because of the "" value I've put in the code but how do I amend this so the textbox can be typed in?

Thanks for your help


RE: VBA if statement within userform

Hi Raani

Thanks for getting in touch. I was surprised this Excel VBA code didn't work. To get round this I added another command enabling or disabling the textbox to ensure control was passed back to the user.

Here's the resulting code:

Private Sub cboPriority_Change()

If cboPriority.Value = "Standard" Then
txtBooked.Value = ""
txtBooked.Enabled = True

txtBooked.Value = "N/A"
txtBooked.Enabled = False

End If

End Sub

Give it a try and let me know how you get on.

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Fri 18 Jul 2014: Automatically marked as resolved.


Training courses


Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


Excel tip:

Adding up rows or columns without seeing formulas

Highlight a column or row and click the Autosum button. This gives you your answer without showing the formula.

View all Excel hints and tips

Server loaded in 0.06 secs.