2015-10-13 52 views
0

我想減少If ... Then ... Else語句的代碼量並嘗試使用IIf(expression,true,false)來設置變量。我有一個問題,在某種程度上變量設置爲0,這是很煩人的。Excel VBA內聯IF語句使變量0

例如:

'Declarations for testing (changing currenty to work for bot and top) 
xval = 15 
currenty = 20 

largebot = 5 
largecen = 5 
largetop = 5 

這一個工程:

largebot = IIf(xval > largebot, IIf(currenty >= -1 And currenty <= 21, xval, largebot), largebot) 

但是這一次沒有:

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. 

回答

1

好了,所以當我發佈這個問題,我想通出來。第一個工作正常,這個沒有,所以我開始懷疑這是否是我的問題,這是什麼。該錯誤是,我基本上由這是

largetop = largetop = xval 

以粗體顯示的說明(如果在代碼中,大膽不顯示):

largetop = IIF(XVAL> largetop,IIF(範圍(」 (當前> = 41,並且當前< = 64,xval,大頂部),IIf(當前> = 26並且當前< = 64,xval,大頂部)),IIF(currenty> = 22和currenty < = 49,largetop = XVAL,largetop)),largetop)

改變這種要只是xval修復了這個問題!希望有人會發現這個有用的...

+0

感謝您張貼您的發現!很高興橡皮鴨幫助你:) – puzzlepiece87