Vba Code

Varghesetcm

New Member
Joined
Jan 29, 2022
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
I have a work book and i had set a vba code to Count the data from sheet regarding colours , i need more modules as when i change colours it is not getting updated in the total .
2 ndly i need to get the pie chart Colours to change according to the Colours in the data area.


Department: HVAC MonthJanuary
Floor8th9th10th11th12th14th15th16th17th18th19thDateNo. of Rooms
Rooms801901100111011201140115011601170118011901Total Rooms116
8029021002110212021402150216021702180219027-Jan-2223
80390310031103120314031503160317031803190314-Jan-226
8049041004110412041404150416041704180421-Jan-2213
805905100511051205140515051605170528-Jan-220
8069061006110612061406150616061706
8079071007110712071407150716071707
80890810081108120814081508
80990910091109120914091509
810910101011101210
811911101111111211
812912101211121212
814914101411141214
81510151115
81610161116
81710171117
81810181118
 

Attachments

  • excel.jpg
    excel.jpg
    254.7 KB · Views: 14

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Welcome to the forum.

Can you provide the VBA code you currently have?
 
Upvote 0
Function GetColorCount(CountRange As Range, CountColor As Range)
Dim CountColorValue As Integer
Dim TotalCount As Integer
CountColorValue = CountColor.Interior.ColorIndex
Set rCell = CountRange
For Each rCell In CountRange
If rCell.Interior.ColorIndex = CountColorValue Then
TotalCount = TotalCount + 1
End If
Next rCell
GetColorCount = TotalCount
End Function
 
Upvote 0
For now, I just focused on getting the totals to work as you expect. The chart can come later.

I don't find anything wrong with your function. I made a test Sub based on your data to call the function to test it out, and it seems to work fine. I also added checking for TintAndShade, though, in case the fill color palette was used for a darker/lighter shade of the same ColorIndex, but other than that, it seems to work fine.

Can you explain a little more about when you add new colors, the totals don't update? Also, what process are you using to update the totals?

Book2
ABCDEFGHIJKLMN
1Department: HVACMonthJanuary
2Floor8th9th10th11th12th14th15th16th17th18th19thDateNo. of Rooms
3Rooms801901100111011201140115011601170118011901Total Rooms116
48029021002110212021402150216021702180219027-Jan-2219
580390310031103120314031503160317031803190314-Jan-2213
68049041004110412041404150416041704180421-Jan-223
7805905100511051205140515051605170528-Jan-221
88069061006110612061406150616061706
98079071007110712071407150716071707
1080890810081108120814081508
1180990910091109120914091509
12810910101011101210
13811911101111111211
14812912101211121212
15814914101411141214
1681510151115
1781610161116
1881710171117
1981810181118
Sheet1

VBA Code:
Sub UpdateColorTotals()
    Dim rooms As Range
    Dim totalCell As Range
    
    Set rooms = Range("B3:L19")
    For Each totalCell In Range("N4:N7")
        totalCell.Value = GetColorCount(rooms, totalCell)
    Next
End Sub

Function GetColorCount(CountRange As Range, CountColor As Range) As Integer
    Dim CountColorValue As Integer
    Dim CountColorTintShade As Double
    Dim TotalCount As Integer
    Dim rCell As Range
    
    CountColorValue = CountColor.Interior.ColorIndex
    CountColorTintShade = CountColor.Interior.TintAndShade
    Set rCell = CountRange
    For Each rCell In CountRange
        If rCell.Interior.ColorIndex = CountColorValue And _
          rCell.Interior.TintAndShade = CountColorTintShade Then
            TotalCount = TotalCount + 1
        End If
    Next rCell
    GetColorCount = TotalCount
End Function
 
Upvote 0
Dear in the chart when i finsh checking room i will highlight the cells with colours used but after colouring the cells the total is not updating every time i have to go to the data cell for each colour and double click to get the total.

1643825151114.png
 

Attachments

  • 1643825074692.png
    1643825074692.png
    213.3 KB · Views: 4
Upvote 0
Oh, I see. That's a problem that has some discussion on the Internet. See HERE, for example. From this page, I added the following to my Sheet1 code, and although the totals don't update when I make the change to the selected cell, when I select a different cell, the totals update.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Application.CalculateFullRebuild
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

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