簡體   English   中英

Excel VBA內聯IF語句使變量0

[英]Excel VBA inline IF statement makes variable 0

我試圖減少If ...那么...其他代碼的代碼量,並嘗試使用IIf(expression,true,false)設置變量。 我有一個問題,其中以某種方式將變量設置為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.

好吧,所以當我發布這個問題時,我已經弄清楚了。 第一個工作正常,而那個沒有,所以我開始質疑這是我的問題,是這個問題。 錯誤是我基本上做了一個聲明,

largetop = largetop = xval

以粗體顯示(如果在代碼中,則不顯示粗體):

largetop = IIf(xval> largetop,IIf(Range(“ B2”))。值=“ RED或BLUE”,IIf(材料= 18,IIf(當前> = 41並且當前<= 64,xval,largetop),IIf( currenty> = 26並且currenty <= 64,xval,largetop)),IIf(currenty> = 22並且currenty <= 49, largetop = xval ,largetop)),largetop)

將其更改為xval可解決問題! 希望有人會發現這個有用...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM