Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TextBox Validation in Excel VBA

Posted on 2011-03-03
6
Medium Priority
?
1,090 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:Michael Fowler
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 2000 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:Tracy
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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 create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

719 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