Entering ifs only once in a cycle

Lavina

Board Regular
Joined
Dec 18, 2018
Messages
75
Hello guys,

i am going through a 100 rows and adding the numbers up together, it sums to a 100. Once it reaches 70, 85, 100, different actions are taken.
This is pretty much a manual conditional format.

My code:
Code:
 For i = 2 To lastRow
        endChecker = range("D" & i).Value + endChecker
        If endChecker > 70 And greenCheck = False Then
            Set rng = range(Cells(initialX, 1), Cells(i, lastColumn))
            Call coloringTables("green", rng)
            greenCheck = True
            initialX = i + 1
        ElseIf endChecker > 85 And yellowCheck = False Then
            Set rng = range(Cells(initialX, 1), Cells(i, lastColumn))
            Call coloringTables("yellow", rng)
            yellowCheck = True
            initialX = i + 1
        ElseIf endChecker = 100 Then
            Set rng = range(Cells(initialX, 1), Cells(i, lastColumn))
            Call coloringTables("red", rng)
            initialX = i
        End If
    Next i

Since my endChecker is not a long but a double, i cannot state that when a certain number is reached do.

Thus the only way i can think of is starting that its over 70, but that's true in every situation after it happens once.
Thus i have the boolean that updates once and then prevents the if from being reached again.

Is there a better way to approach this problem of hitting ifs only once in a cycle?
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
change the value you are looking for? Something like
Code:
Dim valToCheck As Integer: valToCheck = 70

For i = 2 To lastRow
    endChecker = Range("D" & i).Value + endChecker
    
    If endChecker > valToCheck Then
        Set Rng = Range(Cells(initialX, 1), Cells(i, lastColumn))
        
        Select Case valToCheck
            Case 70
                Call coloringTables("green", Rng)
                valToCheck = 85
                initialX = i + 1
            
            Case 85
                Call coloringTables("yellow", Rng)
                valToCheck = 99.999
                initialX = i + 1
            
            Case Else
                Call coloringTables("red", Rng)
                initialX = i
        
        End Select
    End If
Next i
 
Upvote 0

Forum statistics

Threads
1,214,621
Messages
6,120,568
Members
448,972
Latest member
Shantanu2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top