If...And Statement does not work

metropol

Board Regular
Joined
Aug 19, 2005
Messages
209
I have created a Userform with different date fields and descriptions fields.
I want to validate the input fields before sending a message, and have created the following macro.

--------------------------------------
DIM TxBeskr as String
DIM TxStart as Date
DIM TxEnd as Date
DIM TxWBS as String
--------------------------
Private sub command_Send_message()

If TxStart > "" And TxEnd > "" And TxWBS > "" And TxBeskr > "" Then
If IsDate(TxStart) = True Then
If IsDate(TxEnd) = True Then

etc

End if
msgbox "You have not entered any data in the userform"

-----------------------------------------------
The problem ist that the first IF/AND statment does not work.

If there has been no values registred in the userform, it should just go to end if and show the message.

BUT. If there has been values registred, the next step should be If IsDate... But it does not recognize the values in the userform and goes directly to the End IF of the first IF statement.

It is it a problem with the variables and declarations?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

StevenD

Active Member
Joined
Nov 17, 2004
Messages
262
Hi Metropol,

Maybe try this.

Code:
If TxStart <> "" And TxEnd <> "" And TxWBS <> "" And TxBeskr <> "" Then

Hope this helps.
 
Upvote 0

metropol

Board Regular
Joined
Aug 19, 2005
Messages
209
Hi Steven

Tried it, but unfortunately it didn't work. It does not recognize the values in the form in the IF statement.
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Why are you using the more than operator?

And why are you using the Date data type?

Also why have you declared variables and not given them any values?

If TxBeskr etc are controls on the userform just refer to them directly.
Code:
' first see if anything is entered in the control
If Me.TxStart.Value = "" Then 
      MsgBox "Please enter something for the start date"
      Exit Sub
End If
 
' now see if it's a date
If Not(IsDate(Me.TxStart.Value)) Then
      Msgbox "Please enter a date for the start date.
      Me.TxStart.Value = ""
      Me.TxStart.SetFocus
End If
By the way you could validate each individual control on it's own rather than trying to do them in one go.

For example.
Code:
Private Sub TxStart(Cancel As Boolean)
     If Not(IsDate(TxStart)) Then
          MsgBox "Please enter a date"
          Cancel = True
     End If
 
Upvote 0

StevenD

Active Member
Joined
Nov 17, 2004
Messages
262
Have you assigned the number specific area of the text form into your variables?

i.e

Code:
TxStart = yourUserForm.txtTxStart.Text
 
Upvote 0

metropol

Board Regular
Joined
Aug 19, 2005
Messages
209
Why are you using the more than operator?

I was trying to be more rational in my coding (and of course I culd have out tricked my self)

Ex. If there was an illegal value in the TxStart (ex "2009-3f-ff") Then the value should be cleard, the background highlighted red, and a messagebox should pop-up. The same for TxEnd.

BUT, If there where no values at all in the userform, it shold not send a mail just tell the user with a msgbox that there where no entries in the userform.
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
You shouldn't need variables, refer to the controls on the userform directly.
 
Upvote 0

Forum statistics

Threads
1,190,558
Messages
5,981,688
Members
439,730
Latest member
gjvv

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top