Question on IF statement

EssKayKay

Active Member
Joined
Jan 5, 2003
Messages
407
Office Version
  1. 2007
Platform
  1. Windows
I’m apparently missing something here. IF
A = dog
B = dog
Then doesn’t A=B

Here is my test IF statements –
Rich (BB code):
  If txtYEAR = 2025 Then MsgBox "#1"
  If Range("S32").End(xlDown).Offset(0, -1) = 2025 Then MsgBox "#2"
  If txtYEAR = Range("S32").End(xlDown).Offset(0, -1) Then MsgBox "#3"

When I run this I only see messages “#1” & “#2”, I do not see “#3”. Any suggestions?

Thanks for viewing,
Steve K.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
When you step through your code what are the data types?

VBA Code:
textYEAR1 = 2025
textYEAR2 = Range("S32").End(xlDown).Offset(0,-1).Value
If textYEAR1 = textYEAR2 then Debug.Print "They are the same"
 
Upvote 0
Thanks for getting back to me Skybot,
I have the code on a test command button. When I click the button I first see message #1; I click OK and I see message #2; I click OK and see nothing.
I ran your code from my command button and I see nothing.
 
Upvote 0
Ahh, I checked the Immediate window and your message did appear. I should have noted, my code resides in a form (frmPITI) and is triggered from cmdTest1_Click. Maybe this is what's causing the problem. It just seemed weird that the first two messages appeared but not the third.
 
Upvote 0
I modified my code. Rather than looking at the txtYEAR box I have it looking at a cell ("T24") which should be the same as txtYEAR. It appears to be working. Still confusing why the other didn't work but I OKAY with this.

Thanks again,
SKK
 
Upvote 0
I modified my code. Rather than looking at the txtYEAR box I have it looking at a cell ("T24") which should be the same as txtYEAR. It appears to be working. Still confusing why the other didn't work but I OKAY with this.
txtYear is a string and 2025 is a number. You are relying on VBA to do an implicit conversion. This can produce unexpected results as you are experiencing.
It's preferable to take control over the conversion and convert explicitly.

What happens if you do this:
VBA Code:
  If CLng(txtYEAR) = Range("S32").End(xlDown).Offset(0, -1) Then MsgBox "#3"
 
Upvote 0
Solution
That worked Alex. Thank you very much for your input. I will use this code.

Again, much appreciated. . .
SKK
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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
Back
Top