Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default using if statement to compare text

My program is supposed to read dates out of the spreadsheet, but
sometimes there are text strings instead. On these instances I need to
write an if statement telling the program to go elsewhere when it
encounters this string. This may be really simple, but when i type
If MyDate = "TK" then
blah blah blah....


i get a mismatch error on the "TK" part. am i doing something wrong
there? what is the proper way to compare a text string to a variable.
and i know MyDate is getting the "tk" cause i had a message box pop up
to tell me the value of it right above it.

thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default using if statement to compare text

How have you declared the variable MyDate? I suspect you declared
it as a numeric type or as Date. Try changing it to Variant.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"josh" wrote in message
oups.com...
My program is supposed to read dates out of the spreadsheet,
but
sometimes there are text strings instead. On these instances I
need to
write an if statement telling the program to go elsewhere when
it
encounters this string. This may be really simple, but when i
type
If MyDate = "TK" then
blah blah blah....


i get a mismatch error on the "TK" part. am i doing something
wrong
there? what is the proper way to compare a text string to a
variable.
and i know MyDate is getting the "tk" cause i had a message box
pop up
to tell me the value of it right above it.

thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default using if statement to compare text

Make sure that myDate is not declared as a Date, but as a variant, to be
able to handle different data types.

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"josh" wrote in message
oups.com...
My program is supposed to read dates out of the spreadsheet, but
sometimes there are text strings instead. On these instances I need to
write an if statement telling the program to go elsewhere when it
encounters this string. This may be really simple, but when i type
If MyDate = "TK" then
blah blah blah....


i get a mismatch error on the "TK" part. am i doing something wrong
there? what is the proper way to compare a text string to a variable.
and i know MyDate is getting the "tk" cause i had a message box pop up
to tell me the value of it right above it.

thanks



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default using if statement to compare text

I tried changing it to a variant and got the same error. here is the
code...


ActiveCell.Value = mydate
MsgBox (mydate)
If mydate = "TK" Then
MsgBox ("worked")
Else


the cell it's looking at is formatted to date, but has the text "TK" in
it. if that matters.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default using if statement to compare text

Didn't you want to try:

dim myDate as Variant
mydate = activecell.value



josh wrote:

I tried changing it to a variant and got the same error. here is the
code...

ActiveCell.Value = mydate
MsgBox (mydate)
If mydate = "TK" Then
MsgBox ("worked")
Else

the cell it's looking at is formatted to date, but has the text "TK" in
it. if that matters.


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
IF Statement - compare to coloured cell LinLin Excel Worksheet Functions 7 April 14th 09 11:43 PM
using IF statement to compare text RichN Excel Worksheet Functions 4 November 1st 06 09:48 PM
If statement to compare for wildcard value in another cell. Karen53 Excel Worksheet Functions 3 July 12th 06 05:28 AM
compare TIME in IF statement No Name Excel Programming 8 July 10th 05 09:11 PM
compare time in IF statement Excel Worksheet Functions 4 July 10th 05 06:18 PM


All times are GMT +1. The time now is 04:00 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"