2012-11-21 114 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