I am trying to reduce the amount of code from If... Then... Else statements and trying to use IIf(expression, true, false) for setting variables. I am having a problem where somehow the variable gets set to 0 and it's quite annoying.
For example:
'Declarations for testing (changing currenty to work for bot and top)
xval = 15
currenty = 20
largebot = 5
largecen = 5
largetop = 5
This one works:
largebot = IIf(xval > largebot, IIf(currenty >= -1 And currenty <= 21, xval, largebot), largebot)
But this one does not:
largetop = IIf(xval > largetop, IIf(Range("B2").Value = "RED or BLUE", IIf(material = 18, IIf(currenty >= 41 And currenty <= 64, xval, largetop), IIf(currenty >= 26 And currenty <= 64, xval, largetop)), IIf(currenty >= 22 And currenty <= 49, largetop = xval, largetop)), largetop)
'----------COMMENTS TO EXPLAIN DESIRED OUTCOME-------
'If the xval is greater than the highest x value (largetop),
'And if this is for RED or BLUE machines,
'And if on 18 in. material,
'And within the correct range on the material, xval. (largetop should be the value of xval)
'If on 24 in. material (not 18 in.),
'And within the correct range for 24 in. material, xval. (largetop should be the value of xval)
'If not for RED or BLUE machines (GREEN machine, then),
'If within the correct range, (both 18 in. and 24 in. are in the same range)
'largetop should be the value of xval.
'The remaining false statements keep current value.
Alright, so as I was posting this question, I figured it out. The first one worked fine and this one did not, so I was beginning to question if it was my problem, which it was. The error is that I essentially made a statement that was
largetop = largetop = xval
Shown in bold (If in code, bold doesn't show):
largetop = IIf(xval > largetop, IIf(Range("B2").Value = "RED or BLUE", IIf(material = 18, IIf(currenty >= 41 And currenty <= 64, xval, largetop), IIf(currenty >= 26 And currenty <= 64, xval, largetop)), IIf(currenty >= 22 And currenty <= 49, largetop = xval, largetop)), largetop)
Changing that to just xval fixed the problem! Hopefully someone will find this useful...