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
this is what i have written so far and it works fine but as you can see there are 5 cells taht determine the overall cell. But i’ve realized sometimes there’s less than 5 cells – sometimes there’s only 2 or 3. If there are less than 5 this formula is not applicable since it needs 5 cells to determine the overall cel.
I was thinking of using sum function. so summing up the countifs of y, g, r and if that sum is equal to 1,2,3 then it would do the following command but i’m not sure how to do that
if sum of y,g,r = 3 then do the following:
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"
if sum of y,g,r = 2 then do the following:
if g = 2 then
oh ="G"
elseif g = 1 and y = 1 then
oh = "y"
elseif y = 1 and r =1 then
oh = "R"
and etc
also i need to lock the worksheet but the macro has to keep running. how do i do that?
You can use a select case after getting the sum of the cells. For this example I would use select case because it is a little neater to me than using ifs and else ifs, but that is personal preference. For more examples of select case check here
Locking the worksheet requires one line:
for more on worksheet locking check out this link