sub financials
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 个单元格决定了整个单元格。但我意识到有时少于 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"
等等
我也需要锁定工作表,但宏必须继续运行。我怎么做?