# If statement not working



## andrevni22 (Dec 21, 2022)

I am working with arrays now and the IF statement is judging wrong. I resumed my code:

Sub Reset()
Nr=6
Dim X, Y, MinX, AM, NX, W As Double
Y=200,00
AM=5,00
ReDim X(Nr)
ReDim Z(Nr)


For i = 1 To NrParcelas
     X(i - 1) = Y - WorksheetFunction.Sum(.Range(.Cells(2, LTi.Column + i - 1), .Cells(10, 2 + i - 1)))
     Z(i - 1) = X(i - 1)
     W(i - 1) = 0
     NX(i - 1) = X(i - 1)
Next i

MinX = Format(Application.Min(X), "0.00")
' MinX =10 in my case
If MinX >= AM Then
    'code 1
End If

If MinX < AM Then
    'code 2
end if
end sub

The Problem is that MinX is > than AM and it is not going into the first IF statement (code 1), but in the second. I do not know if I decalred the variables correctly, or something else. If MinX is < AM, it ins going into the second statement as well (code 2).

Could please someone help me? I am more than weeks in this issue... Many Thanks in Advance.


----------



## Flashbond (Dec 21, 2022)

andrevni22 said:


> Y=200,00
> AM=5,00


Howcome you can cast numbers to doubles using ","?


----------



## RoryA (Dec 21, 2022)

Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the *method* of doing so is covered by #13 of the Forum Rules.

*Be sure to follow & read the link at the end of the rule too!*

Cross posted at:








						If statement not judgind correctly
					

I am working with arrays now and the IF statement is judging wrong. I resumed my code: Sub Reset() Nr=6 Dim X, Y, MinX, AM, NX, W As Double Y=200,00 AM=5,00 ReDim X(Nr) ReDim Z(Nr)  For i = 1 To Nr...




					stackoverflow.com
				




If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.


----------



## andrevni22 (Dec 21, 2022)

Flashbond said:


> Howcome you can cast numbers to doubles using ","?


Hi,
My Office is in Portuguese Version. Numbers are with ",". I tested and everything is fine... I will edit the post with more info. Thanks


----------



## andrevni22 (Dec 21, 2022)

An Update:
(Numbers are with ",")

Sub Reset()
Nr=6
Dim X, Y, MinX, AM, NX, W As Double
Y=200,00
AM=5,00
ReDim X(Nr)
ReDim Z(Nr)


For i = 1 To NrParcelas
X(i - 1) = Y - WorksheetFunction.Sum(.Range(.Cells(2, LTi.Column + i - 1), .Cells(10, 2 + i - 1)))
Z(i - 1) = X(i - 1)
W(i - 1) = 0
NX(i - 1) = X(i - 1)
Next i

MinX = Format(Application.Min(X), "0.00")
MsgBox MinX 'It returns 10,00 - I tested it
MsgBox AM  ' It returns 5,00 - I tested it

If MinX >= AM Then
'code 1
End If

If MinX < AM Then
'code 2
end if
end sub


----------



## Micron (Dec 21, 2022)

andrevni22 said:


> I do not know if I decalred the variables correctly,


Depends on what you intended. W is Double, everything else is a variant because they're not explicitly declared. This likely has your values being treated as text, and 5,00 is greater than 10,00 in that case.
Please post code within code tags (vba icon on posting toolbar) to maintain indentation and readability.


----------



## andrevni22 (Dec 21, 2022)

SOLVED!


----------



## andrevni22 (Dec 21, 2022)

Micron said:


> Depends on what you intended. W is Double, everything else is a variant because they're not explicitly declared. This likely has your values being treated as text, and 5,00 is greater than 10,00 in that case.
> Please post code within code tags (vba icon on posting toolbar) to maintain indentation and readability.


Solved! I should declarate explicity all the variables. Thank you very much!


----------



## Micron (Dec 21, 2022)

You're welcome! FYI - you can still do it on one line:
Dim strIn As String, lngA As Long, varB As Variant, M As Integer
although I prefer to group, putting integers on one line, strings on another, objects on another, etc.
Maybe you could mark this one as solved so at to get it off the list of threads requiring solution?
Thanks.


----------

