VBA/MACRO to highlight modified cells

VictorKZ

New Member
Joined
Sep 13, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello everyone!

I have a Macro that selects all cells with data.
Now I need to change this Macro to also highlight only the cells that contains blanks spaces in the begging and in the end of the data inside the cells.
This blanks spaces are created normaly by the SpaceBar.
It´s similar to Left Trim and Right Trim, but I just need to highlight the cells that contains this blanks spaces.

Can anyone help me?
Thanks!
VBA Code:
Sub Select_All_Cells_with_Data()

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

End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Here is one way:
VBA Code:
Sub Select_All_Cells_with_Data()

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) = " " And Right(cell, 1) = " " Then cell.Interior.Color = 65535
Next cell

End Sub
 
Upvote 0
Solution
Thank you very much !!
I changed only the AND to IF.
VBA Code:
Sub Select_All_Cells_with_Data()

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) = " " Then cell.Interior.Color = 65535
    If Right(cell, 1) = " " Then cell.Interior.Color = 65535
Next cell

End Sub
 
Upvote 0
Thank you very much !!
I changed only the AND to IF.
VBA Code:
Sub Select_All_Cells_with_Data()

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) = " " Then cell.Interior.Color = 65535
    If Right(cell, 1) = " " Then cell.Interior.Color = 65535
Next cell

End Sub

You are welcome!

I thought you wanted an AND condition (both must be met):
highlight only the cells that contains blanks spaces in the begging and in the end of the data inside the cells.

But if you want an "OR" condition, no need to add an extra IF, just change the AND to an OR, i.e.
VBA Code:
Sub Select_All_Cells_with_Data()

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

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,772
Members
449,049
Latest member
greyangel23

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