0

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 つのセルが必要になるため、この式は適用できません。

私は合計関数を使用することを考えていました。したがって、y、g、r の countif を合計し、その合計が 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"

や。。など

また、ワークシートをロックする必要がありますが、マクロは実行し続ける必要があります。それ、どうやったら出来るの?

4

1 に答える 1

2

セルの合計を取得した後、選択ケースを使用できます。この例では、ifs や else ifs を使用するよりも少しきれいなので select case を使用しますが、それは個人的な好みです。select case のその他の例については、こちらを確認してください

ワークシートをロックするには、次の 1 行が必要です。

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

ワークシートのロックの詳細については、このリンクをチェックしてください

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
于 2012-11-21T14:40:39.463 に答える