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.
 
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
Thats a cool idea! Thanks for help!
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,214,932
Messages
6,122,331
Members
449,077
Latest member
jmsotelo

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