VBA using INDEX and MATCH based on cell colour

Nashpotatoes

New Member
Joined
Jun 21, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I am trying to populate a rota of myself and colleagues (where unfortunately roles are denoted by colour of cell) and automatically update sub-roles/jobs based on a separate 'jobs-rota'

Cross-posting: I have posted previously here - Help with populating rota based on colour of cell and have tried to solve issue but keep coming across problems

Currently I have managed to achieve what I want, but only for the red cells (I believe this is due to the way that MATCH and INDEX functions work by finding the first cell that meets the criteria - and this happens to be a red cell)

See below for the VBA code I am using currently...

VBA Code:
Sub populaterota()
Dim c As Range
Dim r As Long
For Each c In Selection
If c.Interior.Color = RGB(255, 0, 0) Then c.Value = c.Value & " " & (Application.WorksheetFunction.Index(Sheet2.Range("B3:E17"), Application.WorksheetFunction.Match(Sheet1.Range("D" & c.Row), Sheet2.Range("A3:A17"), 0), Application.WorksheetFunction.Match(Sheet1.Range("B" & c.Row), Sheet2.Range("B2:E2"), 0)))
If c.Interior.Color = RGB(255, 255, 0) Then c.Value = c.Value & " " & (Application.WorksheetFunction.Index(Sheet2.Range("B3:E17"), Application.WorksheetFunction.Match(Sheet1.Range("D" & c.Row), Sheet2.Range("A3:A17"), 0), Application.WorksheetFunction.Match(Sheet1.Range("B" & c.Row), Sheet2.Range("B2:E2"), 0)))
If c.Interior.Color = RGB(68, 114, 196) Then c.Value = c.Value & " " & (Application.WorksheetFunction.Index(Sheet2.Range("B3:E17"), Application.WorksheetFunction.Match(Sheet1.Range("D" & c.Row), Sheet2.Range("A3:A17"), 0), Application.WorksheetFunction.Match(Sheet1.Range("B" & c.Row), Sheet2.Range("B2:E2"), 0)))
Next
End Sub

Please see below for Xl2bb

What I am trying to do is populate the main rota on sheet 1 with the jobs on sheet2 - matching both week number (1-4) and day of week, but only for the defined cell colours on the main rota

Sheet1:

Book1 - VBA practice.xlsm
ABCDEFGHIJ
1Main rota
2People
3Week 1-4DateDay123456
41101/08/2022MondayOff
5102/08/2022TuesdayLong dayOff
6103/08/2022WednesdayOff
7104/08/2022ThursdayOff
8105/08/2022FridayOff
9106/08/2022Saturday
10107/08/2022Sunday
112208/08/2022Monday
12209/08/2022TuesdayLong day
13210/08/2022WednesdayLong day Off
14211/08/2022ThursdayLong dayOff
15212/08/2022Friday Off
16213/08/2022Saturday
17214/08/2022Sunday
183315/08/2022Monday
19316/08/2022TuesdayOC
20317/08/2022Wednesday Off Long day
21318/08/2022ThursdayOff
22319/08/2022Friday
23320/08/2022Saturday
24321/08/2022Sunday
254422/08/2022Monday
26423/08/2022Tuesday
27424/08/2022Wednesday
28425/08/2022Thursday
29426/08/2022Friday
30427/08/2022Saturday
31428/08/2022Sunday
321129/08/2022Monday
33130/08/2022Tuesday
34131/08/2022Wednesday
35101/09/2022ThursdayOC
36102/09/2022Friday
Sheet1


Sheet2:

Book1 - VBA practice.xlsm
ABCDE
1Jobs rota for red, yellow, blue
21234
3MondayLabOfficeFloorOutside
4MondayCollectionStudyStudyStudy
5MondayPick upPick upPick upPick up
6TuesdayStudyOfficeTheatreOffice
7TuesdayClinic 1Clinic 2Clinic 3Clinic 4
8TuesdayStudyStudy
9Wednesday
10Wednesday
11WednesdayTheatreTheatre
12ThursdayClinic 5Clinic 6Clinic 7Clinic 8
13ThursdayPick-upClinic 8Pick -upClinic 6
14ThursdayTheatreTheatre
15FridayClinic 9
16FridayLabClinic 9Lab
17FridayStudyStudy
Sheet2



I would be grateful for any help or suggestions. I am interested in VBA (of which I am definitely a novice) and looking to continue to learn so explanations also very much appreciated. Thank you
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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