Forum Discussion

tomc72's avatar
tomc72
Brass Contributor
Jul 16, 2023
Solved

Identifying the content of a Cell by searching a range for its particular Format

Hi there!

In an Excel spreadsheet, I have a block of scheduling dates ($H$30:$K$42) that are four weeks apart. I have a Condional Formatting rule that changes the display format of the next schedule date (based upon the Today() date) from black on white to white on black.

From a Cell outside of the range, I want to find the content of the one cell in the range that has a different format from the others.

I would be so grateful for any help with achieving this.

Many thanks.

  • tomc72 

    In Excel, there is no built-in formula that directly identifies cells based on their formatting. The formula-based approach is limited to identifying the content of cells, not their formatting.

    To identify the contents of a cell with a specific format in a range, you can use VBA (Visual Basic for Applications) code in Excel. Here is an example code snippet that demonstrates how you can achieve this:

    Vba code:

    Sub FindCellWithDifferentFormat()
        Dim rng As Range
        Dim cell As Range
        Dim targetCell As Range
        
        Set rng = Range("H30:K42") ' Specify the range you want to search
        
        For Each cell In rng
            If cell.DisplayFormat.Interior.Color = RGB(0, 0, 0) And _
                cell.DisplayFormat.Font.Color = RGB(255, 255, 255) Then
                ' Check if the cell has the formatting you applied (white on black)
                ' Modify the RGB values above to match your specific formatting
                
                ' Set the target cell as the cell with different format
                Set targetCell = cell
                Exit For ' Exit the loop once a cell with different format is found
            End If
        Next cell
        
        If Not targetCell Is Nothing Then
            MsgBox "Cell with different format: " & targetCell.Value
        Else
            MsgBox "No cell with different format found."
        End If
    End Sub

    Here's how you can use the above code:

    1. Press ALT + F11 to open the VBA editor in Excel.
    2. Insert a new module by clicking on "Insert" > "Module".
    3. Copy and paste the code into the module.
    4. Modify the range H30:K42 in the code to match your specific range.
    5. Customize the RGB values in the code to match the formatting of the cell you want to find (white on black in this example).
    6. Close the VBA editor.
    7. Press ALT + F8 to open the "Macro" dialog box.
    8. Select the "FindCellWithDifferentFormat" macro and click "Run".

    The code will search the specified range for a cell with a different format (white on black in this example) and display a message box with the contents of that cell. If no cell with a different format is found, it will display a message indicating so.

    Make sure to save your Excel workbook with macros enabled for the code to work properly.

    The text, steps and functions were created with the help of vatious AI.

    My answers are voluntary and without guarantee!

    Hope this will help you.

3 Replies

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    tomc72 

    In Excel, there is no built-in formula that directly identifies cells based on their formatting. The formula-based approach is limited to identifying the content of cells, not their formatting.

    To identify the contents of a cell with a specific format in a range, you can use VBA (Visual Basic for Applications) code in Excel. Here is an example code snippet that demonstrates how you can achieve this:

    Vba code:

    Sub FindCellWithDifferentFormat()
        Dim rng As Range
        Dim cell As Range
        Dim targetCell As Range
        
        Set rng = Range("H30:K42") ' Specify the range you want to search
        
        For Each cell In rng
            If cell.DisplayFormat.Interior.Color = RGB(0, 0, 0) And _
                cell.DisplayFormat.Font.Color = RGB(255, 255, 255) Then
                ' Check if the cell has the formatting you applied (white on black)
                ' Modify the RGB values above to match your specific formatting
                
                ' Set the target cell as the cell with different format
                Set targetCell = cell
                Exit For ' Exit the loop once a cell with different format is found
            End If
        Next cell
        
        If Not targetCell Is Nothing Then
            MsgBox "Cell with different format: " & targetCell.Value
        Else
            MsgBox "No cell with different format found."
        End If
    End Sub

    Here's how you can use the above code:

    1. Press ALT + F11 to open the VBA editor in Excel.
    2. Insert a new module by clicking on "Insert" > "Module".
    3. Copy and paste the code into the module.
    4. Modify the range H30:K42 in the code to match your specific range.
    5. Customize the RGB values in the code to match the formatting of the cell you want to find (white on black in this example).
    6. Close the VBA editor.
    7. Press ALT + F8 to open the "Macro" dialog box.
    8. Select the "FindCellWithDifferentFormat" macro and click "Run".

    The code will search the specified range for a cell with a different format (white on black in this example) and display a message box with the contents of that cell. If no cell with a different format is found, it will display a message indicating so.

    Make sure to save your Excel workbook with macros enabled for the code to work properly.

    The text, steps and functions were created with the help of vatious AI.

    My answers are voluntary and without guarantee!

    Hope this will help you.

    • tomc72's avatar
      tomc72
      Brass Contributor
      Excellent. Works a charm! So clever! Bravo, NikolinoDE! Thank you so much!
      Tom
    • tomc72's avatar
      tomc72
      Brass Contributor
      Hi NicolinoDE, Many thanks for taking the time and trouble to compile your comprehensive reply. I am extremely grateful. I will study your reply later today and will get back to you to let you know how I get on! 👍👍👍

Resources