[VBA ] IF 判斷式

作者: balalala10 (Andy)   2018-03-31 20:27:19
大家好,我想要在VBA中使用IF判斷條件,
因為用函數好像沒辦法串聯。
條件如下:
suggest push in = A>0, B<0
suggest push out= A>0, B>0
no demand suggest push out= A=0, C or D or E>0
Excel 函數如下:
=IF(AND(A2>0,B2<0),"suggest push in","")
=IF(AND(A2>0,B2>0),"suggest push out","")
=IF(AND(A4=0,OR(C4>0,D4>0,E4>0)),"no demand sugggest push in","")
表格圖片如下:
[img]https://imgur.com/lLtFRGK[img]
_______________________________________________________________________
VBA code如下:
Sub test()
lastrow = Range("F" & Rows.Count).End(xlUp).Row
For Each cell In Range("F2:F" & lastrow)
A = "A2: A"
B = "B2: B"
C = "C2: C"
D = "D2: D"
E = "E2: E"
If A > 0 & B > 0 Then
cell.Value = "suggest push out"
ElseIf A > 0 & B < 0 Then
cell.Value = "suggest push in"
ElseIf (C > 0 Or D > 0 Or E > 0) And A = 0 Then
cell.Value = "no demand sugggest push in"
End If
Next
End Sub
_______________________________________________________________________
跑後只有push in的result出現在F1,F2,不曉得是範圍抓錯還是定義有誤。
作者: hulapig (呼拉豬)   2018-03-31 22:58:00
函數可以串成一個
作者: MOONRAKER (㊣牛鶴鰻毛人)   2018-04-01 14:36:00
不會吧你怎麼會這樣取值 你這樣ABCDE只是五個字串啊
作者: paul60209 (我是保羅小小)   2018-04-03 17:55:00
你宣告了一個lastrow但卻沒用到...dim A,B,C,D as rangeA=range(cells(2,"A"),cells(lastrow,"A"))IF A.value>0 & B.value<0 then你試試看,把宣告的部分跟條件部分改掉,應該就可以了

Links booklink

Contact Us: admin [ a t ] ucptt.com