I am new to this forum and to VBA. I am trying to write an if statement which would check to see if number is decimal or not. I tried looking on MSDN but really could not understand what was going on. Thanks you.
|
I am new to this forum and to VBA. I am trying to write an if statement which would check to see if number is decimal or not. I tried looking on MSDN but really could not understand what was going on. Thanks you.
Code:Public Sub CalculateNorthLambert200(strMapNumXAdj, strMapNumYAdj, strNorthMap200) strMapNumXAdj = "" strMapNumYAdj = "" strNorthMap200 = "" sngMapNumXAdj = intMapNumX / 4 'Trying to check if this answer is a decimal sngMapNumYAdj = intMapNumY / 6 'Trying to check if this answer is a decmal strMapNumXAdj = sngMapNumXAdj 'Trying to transfer decimal number to string to check for "." strMapNumYAdj = sngMapNumYAdj 'Trying to transfer decimal number to string to check for "." strNorthMap200 = strMapNumXAdj & "-" & strMapNumYAdj & "-" & "2" 'Concatinate my string End SubModerator Note:
Last edited by Opie; 2008-01-15 at 11:02 PM. Reason: [CODE] tags added, see Moderator Note
See my inline comments. You should be declaring the type of your variables. If you don't, they are automatically typed as Variant.
BTW, Please use the code tags available in the advanced editor.
Code:Public Sub CalculateNorthLambert200(strMapNumXAdj As String, strMapNumYAdj As String, strNorthMap200 As String) strMapNumXAdj = "" If you clear the arg's here, why have arg's? strMapNumYAdj = "" strNorthMap200 = "" Where do you declare the other var's prefixed with sng and int? They should be dimmed as follows: iMapNumXAdj As Integer 'Use Integer, there is no Single data type dMapNumX As Double 'Double data type will give you "decimal" sngMapNumXAdj = intMapNumX / 4 'Trying to check if this answer is a decimal sngMapNumYAdj = intMapNumY / 6 'Trying to check if this answer is a decmal strMapNumXAdj = sngMapNumXAdj 'Trying to transfer decimal number to string to check for "." strMapNumYAdj = sngMapNumYAdj 'Trying to transfer decimal number to string to check for "." strNorthMap200 = strMapNumXAdj & "-" & strMapNumYAdj & "-" & "2" 'Concatinate my string End Sub
Haa haa, I remember MY first app!
You are passing your "numbers" as strings, then wanting to convert to numeric, then back again to strings for some sort of output. It might be easier if you stick with numbers, which are smaller, quicker, and a whole lot cleaner to deal with, than strings.
Contrary to what was said, there IS a "Single' data type, although there isnt much benefit nowadays to using it - so use doubles.
Im assuming that by 'decimal', you want to check to see if the numbers are WHOLE numbers or not. First, you may want to see if your strings are even NUMBERS ... you are passing them as strings. So use the "Isnumeric' function to make sure there arent any letters inside those strings. Once you know you have a genuine number there, convert to double. Then check to see if its a whole number by comparing it to an integer version of the number.
Something like this: *** warning** code typed on the fly ...
Code:Dim tmpDouble as Double if IsNumeric(strNorthMap200) then ' its a number tmpDouble =Cdbl(strNorthMap200) ' convert to Double precision if Int(tmpDouble)=tmpDouble then ' its a whole number! msgbox "This number is a Whole Number" else ' Nope - has digits to the right of the decimal msgbox "This Number is NOT a whole number" end if end if
' if you dont get a message box at ALL, then your string isnt even convertible
to a number....
Last edited by Opie; 2008-01-28 at 07:33 PM. Reason: [CODE] tags added