[算表] VBA IF 判斷條件

看板Office作者 (Andy)時間8年前 (2018/03/31 20:15), 8年前編輯推噓0(006)
留言6則, 1人參與, 8年前最新討論串1/1
大家好,我想要在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://www.ptt.cc/bbs/Office/M.1522498549.A.F60.html ※ 編輯: balalala10 (36.228.148.115), 03/31/2018 20:19:44

03/31 20:42, 8年前 , 1F
巨集寫法類似這樣 https://i.imgur.com/bx7X1IR.jpg
03/31 20:42, 1F

03/31 20:48, 8年前 , 2F
公式的話如a欄只有正數和0值的話
03/31 20:48, 2F

03/31 20:48, 8年前 , 3F

03/31 20:49, 8年前 , 4F
c:e欄只會有正數可用sum,如可能有負數或0值的話用countif
03/31 20:49, 4F

03/31 20:49, 8年前 , 5F
如a欄也會有負數,而要指定0值才為真的話,請再自行加入
03/31 20:49, 5F

03/31 20:50, 8年前 , 6F
a欄的判斷(如a2=0)
03/31 20:50, 6F
文章代碼(AID): #1QltlrzW (Office)
文章代碼(AID): #1QltlrzW (Office)