detect location of a color

benjamin132

New Member
Joined
Jun 8, 2022
Messages
21
Platform
  1. Windows
Hi everyone,
In my beautiful Worksheet, I need to know between which city there is a green bar.
I explained : for the first line I need to know that there is a green bar between "Lyon St Priest Coque A1" and "Delandine", and between "Delandine" and "Lyon St Priest Coque A2"
the second line : I need to know that there is a green bar between "Lyon St Priest Coque A1" and "Dumont", and between "Dumont" and "Lyon St Priest Coque A2".
in Output i need to have for example 4 variable with a Table like ["Lyon St Priest Coque A1","Delandine","Delandine","Lyon St Priest Coque A2"].
Thanks by advance.

1655974804155.png
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Where do you want the list placed and how, separate cells per to/from or a list all in one cell per row? What is the exact colorindex of the bar, 4?

Try this. I made the list go one cell to the right of the last city name. You may have to change the color index

VBA Code:
Sub FindColor()
'https://www.mrexcel.com/board/threads/detect-location-of-a-color.1208693/
With Sheets("Sheet1")
    lastrow = .Range("A" & Rows.Count).End(xlUp).Row
    LastCol = .Cells(3, .Columns.Count).End(xlToLeft).Column + 2
End With

For j = 5 To lastrow Step 3
    For i = 5 To LastCol
    Cells(j, i).Select
        If Cells(j, i).Interior.ColorIndex = 4 Then
            If x = 0 Then StartCity = Cells(3, i).MergeArea.Cells(1, 1).Value: x = 1
        Else
            If x = 1 Then
                EndCity = Cells(3, i).MergeArea.Cells(1, 1).Value
                x = 0
                If Not Cells(j, lastrow + 1) = "" Then Cells(j, lastrow + 1) = Cells(j, lastrow + 1) & ", " & Chr(10)
                Cells(j, LastCol + 1) = Cells(j, LastCol + 1) & StartCity & " to " & EndCity
            End If
        End If
    Next i

Next j
End Sub
 
Upvote 0
Solution
Awesome you are a genius. Thanks a lot. Just wanted to store the list in a variable because i need it. But i can do it don't worry. Thanks a lot, have a good day
 
Upvote 0
I just realized I left in the .Select statement. I put this in for testing purposes to watch the code progress. This is not necessary and removing it may speed up processing.
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,797
Members
449,337
Latest member
BBV123

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