sub financials
COUNTIF/VBA有條件/鎖定宏觀
dim g as long
dim r as long
dim y as long
dim oh as range
dim vr as range
dim sum as long
set vr = Sheets("Financials").Range("B5:B53")
set oh = sheets("Financials").Range("B2")
y = application.worksheetfunction.countif(vr, "y")
g = application.worksheetfunction.countif(vr, "g")
r = application.worksheetfunction.countif(vr, "r")
if g = 5 then
oh = "G"
elseif g = 4 and y = 1 then
oh = "G"
elseif r>=2 then
oh = "R"
elseif y >= 1 and r>= 1 then
oh = "R"
elseif y >=3 then
oh = "R"
elseif g=3 and y=2 then
oh = "Y"
elseif g=4 and r=1 then
oh = "Y"
elseif g=2 and y=3 then
oh = "Y"
elseif y=2 then
oh = "Y"
end if
end sub
這是我迄今書面和它工作正常,但你可以看到有5個細胞taht確定總的電池。但是我意識到有時候只有不到5個細胞 - 有時候只有2或3個。如果小於5這個公式不適用,因爲它需要5個細胞來確定整個細胞。
我在想用sum函數。所以總結y,g,r的標識,如果總和等於1,2,3,那麼它將執行以下命令,但我不知道如何做到這一點,但如果y,g中,r = 3,然後執行下列操作:
if g = 3 then
oh = "G"
elseif g = 1 and y = 2 then
oh = "Y"
elseif g = 2 and r = 1 then
oh = "Y"
elseif g =1 and y = 1 and r =1 then
oh = "R"
elseif y = 2 and r = 1 then
oh = "R"
elseif r = 3 then
oh = "R"
如果y,G,R的總和= 2,則執行下列操作:
if g = 2 then
oh ="G"
elseif g = 1 and y = 1 then
oh = "y"
elseif y = 1 and r =1 then
oh = "R"
和等
還我需要鎖定工作表,但宏必須保持運行。我怎麼做?
什麼問題你得到現在當少於5?你是什麼意思的「5細胞」? – InContext
[這裏](http://stackoverflow.com/questions/13014785/ratio-conditional-requirements-for-vba-excel)是一個問題的鏈接,他從5個單元格中創建這個規則來確定另一個的值。這可能是時候切換到一個案例聲明 – scott