作者balalala10 (Andy)
看板Visual_Basic
标题[VBA ] IF 判断式
时间Sat Mar 31 20:27:19 2018
大家好,我想要在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,不晓得是范围抓错还是定义有误。
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.228.148.115
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Visual_Basic/M.1522499242.A.61A.html
※ 编辑: balalala10 (36.228.148.115), 03/31/2018 20:28:08
1F:→ hulapig: 函数可以串成一个 03/31 22:58
2F:→ MOONRAKER: 不会吧你怎麽会这样取值 你这样ABCDE只是五个字串啊 04/01 14:36
3F:→ paul60209: 你宣告了一个lastrow但却没用到... 04/03 17:55
4F:→ paul60209: dim A,B,C,D as range 04/03 17:56
5F:→ paul60209: A=range(cells(2,"A"),cells(lastrow,"A")) 04/03 17:57
6F:→ paul60209: IF A.value>0 & B.value<0 then 04/03 17:58
7F:→ paul60209: 你试试看,把宣告的部分跟条件部分改掉,应该就可以了 04/03 17:58