VBA Sheet color change condition

VictorKZ

New Member
Joined
Sep 13, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello everybody,
I would like to change the colors of certain sheets, which meet this condition => If Left(cell, 1) = " " Or Right(cell, 1) = " " Then cell.Interior.Color = 65535

VBA Code:
Sub Select_All_Cells_with_Data()

Dim sht
Set Rng = ActiveSheet.UsedRange

Rng.Cells(1, 1).Select

For i = 1 To Rng.Rows.Count
    For j = 1 To Rng.Columns.Count
        If Rng.Cells(i, j) <> "" Then
            Union(Selection, Rng.Cells(i, j)).Select
        End If
    Next j
Next i

'Highlight cells
For Each cell In Selection
    If Left(cell, 1) = " " Or Right(cell, 1) = " " Then cell.Interior.Color = 65535
  
Next cell

For Each sht In ThisWorkbook.Worksheets
  sht.Tab.ColorIndex = 6
 
Next sht

End Sub

Thanks for listening
 

Attachments

  • 1663607138638.png
    1663607138638.png
    326 bytes · Views: 14

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Your macro does what you are asking for as long as your data has a leading or a trailing space. It will highlight the cell even if you meet both conditions.
 
Upvote 0
Hi VictorKZ,

from what I understand:
VBA Code:
Sub MrE1216932()
'https://www.mrexcel.com/board/threads/vba-sheet-color-change-condition.1216932/

Dim wks As Worksheet
Dim rngCell As Range
Dim blnColTab As Boolean

For Each wks In ThisWorkbook.Worksheets
  blnColTab = False
  For Each rngCell In wks.UsedRange
    With rngCell
      If .Value <> "" And (Left(.Value, 1) = " " Or Right(.Value, 1) = " ") Then
        .Interior.Color = vbYellow
        blnColTab = True
      End If
    End With
  Next rngCell
  If blnColTab Then wks.Tab.ColorIndex = 6
Next wks

End Sub
Ciao,
Holger
 
Upvote 0
Solution

Forum statistics

Threads
1,224,812
Messages
6,181,088
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