2012-11-21 72 views
0

sub financialsCOUNTIF/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" 

和等

还我需要锁定工作表,但宏必须保持运行。我怎么做?

+0

什么问题你得到现在当少于5?你是什​​么意思的“5细胞”? – InContext

+0

[这里](http://stackoverflow.com/questions/13014785/ratio-conditional-requirements-for-vba-excel)是一个问题的链接,他从5个单元格中创建这个规则来确定另一个的值。这可能是时候切换到一个案例声明 – scott

回答

2

您可以在获取单元格总和后使用select case。对于这个例子,我将使用select case,因为它比使用ifs和ifs更简单一些,但这是个人偏好。对于选择的情况下检查here

锁定工作表的多个实例,需要一个行:

sheets("worksheetname").protect userinterfaceonly:=True 

更多的工作表上锁定看看这个link

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 
Dim i 
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") 

x = y + g + r 

Select Case x 
    Case Is = 5 
     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 

    Case Is = 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" 
     End If 

    Case Is = 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" 
     End If 

    'more cases here 

    End Select 


End Sub 
+0

谢谢!有没有办法让somone无法访问VBA代码?就像竞争性地隐藏VBA代码,除非我取消保护工作表? – Doolie1106

+0

命中ctrl-r(查看项目浏览器) 与在项目浏览器中选择的项目 工具VBAProject属性 保护选项卡。请记住,excel并不是最安全的系统。有商业产品将能够访问这些密码或只是禁用它们 – scott

+0

如果这是您正在寻找的东西,请不要忘记标记为答案 – scott