VBA Highlight consequent cells based on value from another cell with criteria

Anita89

New Member
Joined
May 8, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello together,

please help me to extend my VBA code to highlight amount of cells (top to bottom) for a certain year based on how many tools are planned in separate table on the right side in attached "example".
So far i wrote code that only counts tools for first country, but i dont know how to bind it to value from another table.

For example in USA it is planned to have 2 cutting tools in 2023 so for 2 cutting tools must be highlighted 2 cells in col. C
thank you in advance, i am totally new to vba.

VBA Code:
Dim rng1, rng2 As Range
    Dim criteria1, criteria2 As Variant
    Dim result As Double
 
    Set rng1 = Range("A2:A14")
    criteria1 = "USA"
 
    Set rng2 = Range("B2:B14")
    criteria2 = "Cutting tool"
 
    result = WorksheetFunction.CountIfs(rng1, "*" & criteria1 & "*", rng2, "*" & criteria2 & "*")

DEVELOPER.xlsm
ABCDEFGHIJKLMNOPQR
1CountryCategory2023202420252026202720232024202520262027
2USACutting toolUSACutting tool23245
3USACutting toolEuropeCutting tool13221
4USACutting tool
5USACutting tool
6USACutting tool
7USACutting tool
8EuropeCutting tool
9EuropeCutting tool
10EuropeCutting tool
11EuropeCutting tool
12EuropeCutting tool
13EuropeCutting tool
14EuropeCutting tool
15
16
Sheet3
Cells with Conditional Formatting
CellConditionCell FormatStop If True
M1:Q1Cellcontains a blank value textNO
C1:G1Cellcontains a blank value textNO
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Why don't you just have a chart?

Lots of different ones to choose from.

So east to set up.
 

Attachments

  • Charts.JPG
    Charts.JPG
    51.8 KB · Views: 6
Upvote 0
Give this a try.

It allows 3 rows under each location until the next location but this can be changed.

It takes a while to run but confirms when it is complete.

VBA Code:
Public Sub subConditionalFormatting()
Dim rngGrid As Range
Dim rngColumn As Range
Dim rng As Range
Dim rngValues As Range
Dim i As Integer
Dim rngLocation As Range
Dim intCol As Integer
Dim strMsg As String

    Range("A2:B20").ClearContents
    
    Set rngValues = Range("M2:Q3")
    
    ActiveWorkbook.Save

    Set rngGrid = Range("$C$2:$G$" & WorksheetFunction.Max(rngValues) + 3)
    
    strMsg = "Conditional Formatting complete for : " & vbCrLf & vbCrLf
    
    For Each rngLocation In rngValues.Rows
        
        intCol = 1
        
        For Each rngColumn In rngGrid.Columns
        
            i = 1
            
            For Each rng In rngColumn.Cells
            
                Cells(rng.Row, 1).Resize(1, 2).Value = rngLocation.Offset(0, -2).Resize(1, 2).Value
                
                rng.FormatConditions.Delete
                
                rng.FormatConditions.Add Type:=xlExpression, Formula1:="=" & rngLocation.Cells(intCol).Address & ">=" & i
                
                rng.FormatConditions(1).Interior.Color = RGB(255, 255, 0)
          
                i = i + 1
                
            Next rng
          
            intCol = intCol + 1
            
        Next rngColumn
        
        strMsg = strMsg & rngGrid.Cells(1).Offset(0, -2).Value & " " & rngGrid.Cells(1).Offset(0, -1).Value & vbCrLf
      
        Set rngGrid = rngGrid.Offset(rngGrid.Rows.Count, 0).Resize(WorksheetFunction.Max(rngLocation.Offset(1, 0)) + 3, rngGrid.Columns.Count)
      
        Next rngLocation
        
        strMsg = strMsg
        
        MsgBox strMsg, vbInformation, "Confirmation"
    
End Sub
 
Upvote 0
I'm glad that it did the job for you.

Can you please mark it as a solution? (Tick in circle on the right.)
 
Upvote 0

Forum statistics

Threads
1,223,882
Messages
6,175,166
Members
452,615
Latest member
bogeys2birdies

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