Solved

TextBox Validation in Excel VBA

Posted on 2011-03-03
6
1,077 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
ID: 35033907
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
ID: 35033915
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
ID: 35033918
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
ID: 35033936
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
ID: 35356994
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

792 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