Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8866343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:45:46+00:00 2026-06-14T16:45:46+00:00

sub financials dim g as long dim r as long dim y as long

  • 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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:45:47+00:00Added an answer on June 14, 2026 at 4:45 pm

    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:

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

    for more on worksheet locking check out this 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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sub testCov() Rng2 = Sheets(20 Asset Model).Range(b3:f48) Dim covMatrix() As Variant ReDim covMatrix(1 To
Sub RowRangeMove() Sheets.Add().Name = CopySheet With Sheets(BigDataSet - Copy) .Range(A65000, .Range(A13000).End(xlUp)).Copy Destination:=Range(A1) With Sheets(BigDataSet
Public Sub MyFunction() Dim lowstring As String lowstring = hi Me.RichTextView.Find(lowstring, 0, 2) End
Private Sub cmdLogin_Click() On Error GoTo ErrorHandler Dim RowNo As Long Dim Id As
Sub StoreUserData2() If SaveFileDialog1.ShowDialog() Then InputBox(MessageStor, TitleStor, SaveFileDialog1.ShowDialog) WriteUserFile() End Sub It allows me
Sub StoragevsQuota() Charts.Add ActiveChart.ChartType = xlColumnClustered ActiveChart.SetSourceData Source:=Sheets(Data).Range(E1:G32) ActiveChart.Location Where:=xlLocationAsObject, Name:=Storage Charts ActiveChart.Parent.Name =
Private Sub importClipboard_Click() Dim data As Collection data = getClipboardData() ...do something... End Sub
Private Sub Workbook_Open() Sheets(01).Delete Sheets(02).Copy before:=Sheets(02) Sheets(02 (2)).Name = 01 Form1.Show End Sub Form1
sub generateCategoryXML_helper() dim FSO, fileOut set FSO = CreateObject(Scripting.FileSystemObject) set fileOut = FSO.CreateTextFile(Server.MapPath(categoryTree.xml), True)
Public Sub New() If GridElement.typeList(getId(), getLayer()) Is Nothing Then GridElement.typeList(getid(),getlayer()) = GetType(me) End If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.