Solved

TextBox Validation in Excel VBA

Posted on 2011-03-03
6
1,073 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

770 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