Solved

TextBox Validation in Excel VBA

Posted on 2011-03-03
6
1,079 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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 will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

740 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