Solved

TextBox Validation in Excel VBA

Posted on 2011-03-03
6
1,065 Views
Last Modified: 2012-05-11
I have three TextBoxes in my Excel VBA application, i need to perform the below validations

if there is value in first textbox then it should be assigned to a variable say X

If only there is value in first textbox , it should check for second textbox and assign the values to X and Y variables respectively

If no value in first textbox then shouldn't go for second textbox validation at all, instead to third textbox directly from first.

Can anyone write the IF ....Then loop with the above validations


0
Comment
Question by:srikanthv2322
  • 3
6 Comments
 
LVL 23

Expert Comment

by:Michael74
Comment Utility
Is this sort of thing you are after.
Not sure what you want done with 3rd textbox.

There can be 4 combinations of entries to code for ie 1+2, 1+3, 2+3 and 1+2+3 so you just need to provide code for each  option in the form

If...Then
ElseIf...Then
ElseIf...Then
Else...Then
End If

Michael
If textbox1.value <> "" And textbox2.value = "" then
   x = textbox1.value
elseIf textbox1.value <> "" And textbox2.value = "" then
   x = textbox1.value
   y = textbox2.value
End If

Open in new window

0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
Comment Utility
Here it is

Private Sub CommandButton1_Click()
    Dim X, Y, Z
    
    '~~> Check if TextBox 1 has Text
    If Len(Trim(TextBox1.Text)) <> 0 Then
        X = TextBox1.Text
        '~~> Check if TextBox 2 has Text
        If Len(Trim(TextBox2.Text)) <> 0 Then
            Y = TextBox2.Text
        End If
    '~~> Check if TextBox 3 has Text
    ElseIf Len(Trim(TextBox3.Text)) <> 0 Then
        Z = TextBox3.Text
    End If
End Sub

Open in new window


Sid


Book1.xls
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
Michael: Sorry didn't know you were working on this as well. I see that both your if conditions are same?

Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
srikanth: This is what I understood from your question.

if TextBox1 is not empty assign it's value to X
If TextBox1 is not empty,check TextBox2 and assign it's value to Y.
If both TextBox1 and TextBox2 are empty then take TextBox3 into consideration.

Have I understood your query correctly? Please do correct me if I am wrong and I will amend the code.

Sid
0
 
LVL 24

Expert Comment

by:broomee9
Comment Utility
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now