Issue with If Statement in excel VBA

creaseA

New Member
Joined
Mar 20, 2023
Messages
18
Office Version
  1. 365
Platform
  1. Windows
I am a strange issue occurring that I can not seem to find the answer to.
Here is snippet of code:

VBA Code:
If value1 > value2 Then
MsgBox(message)
End If

I have ensured that value1 is less than value2, yet each time the code runs I get the message in the message box. Value 1 and Value 2 are defined and given values immediately prior to the If statement. (Can not share rest of code)
Other If statements in the code work as expected but the message in this if statement runs no matter the values of Value1 and Value 2.

Any help here would be appreciated.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
are value1 and value2 both declared variables? if so, are they of the same type?
 
Upvote 0
are value1 and value2 both declared variables? if so, are they of the same type?
Both value1, and value2 are "Dim value1, value2 As String"
Next lines:
value1 = 0.6
value2=0.7

With this case, the message continues to show up within the If statement
 
Upvote 0
Both value1, and value2 are "Dim value1, value2 As String"
Next lines:
value1 = 0.6
value2=0.7

With this case, the message continues to show up within the If statement
This is Double not string
 
Upvote 0
Both value1, and value2 are "Dim value1, value2 As String"
Next lines:
value1 = 0.6
value2=0.7

With this case, the message continues to show up within the If statement
yeah, the way you've got your variables declared, value1 is a variant and value2 is a string, so right off the bat you're attempting to evaluate different data types.

try changing their declarations like this:

VBA Code:
dim value1 as Double
dim value2 as Double


then, when VBA evaluates them against one another, they're both the same type.
 
Last edited:
Upvote 0
Solution
yeah, the way you've got your variables declared, value1 is a variant and value2 is a string, so right off the bat you're attempting to evaluate different data types.

try changing their declarations like this:

VBA Code:
dim value1 as Double
dim value2 as Double


then, when VBA evaluates them against one another, they're both the same type.
Thanks! This fixed the issue!
 
Upvote 0
Thanks! This fixed the issue!
yeah, the way you've got your variables declared, value1 is a variant and value2 is a string, so right off the bat you're attempting to evaluate different data types.

try changing their declarations like this:

VBA Code:
dim value1 as Double
dim value2 as Double


then, when VBA evaluates them against one another, they're both the same type.
So I'm fairly new with VBA.

If I say, Dim variable1, variable2, variable, etc,..... As Double,

Then only the LAST variable in the list is double and the rest are variant?
 
Upvote 0
So I'm fairly new with VBA.

If I say, Dim variable1, variable2, variable, etc,..... As Double,

Then only the LAST variable in the list is double and the rest are variant?
yes, that is correct. i used to declare variables like that until i learned that if you don't explicitly state the variable type for each variable, then it defaults to variant.

you can still do them on the same line, it would just read like Dim var1 as Double, var2 as Double, etc.

on a side note, i use a form of hungarian notation, where i add a 3-byte variable type prefix to my variables. it makes it much easier to keep track of in the code and i know just to look at the variable what data type it is. perhaps you may find it useful to adopt such a practice. :)

VBA Code:
Dim dblValue1   As Double
Dim dblValue2   As Double
Dim strValue1   As String
Dim lngValue1   As Long
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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