If statement not working

andrevni22

New Member
Joined
Dec 21, 2022
Messages
9
Office Version
  1. 2021
Platform
  1. Windows
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.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
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 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.
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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!
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,159
Members
452,615
Latest member
bogeys2birdies

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