VBA check if active sheet has values that starts or ends with space

Kra

Board Regular
Joined
Jul 4, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi all!

I have a dataset of over 50k rows in excel. Some of values starts or ends with space and it is incredibly annoying to maintain this kind of data. I keep using TRIM formula but it slows down sheets with lots of data.

I tried to use code to highlight cells with spaces before or after value, but it is not working. It highlights values without spaces. Any idea how to fix it?

Is it also possible to adjust it so it checks whole sheet, not only selection and it shows msg box at the end with information how many cells with spaces were found?

Code I am using:

VBA Code:
Private Sub CommandButton19_Click()

    Dim i As Long, j As Long
    Dim sh As Worksheet
    Dim sheetArr As Variant
    Set sh = ActiveSheet
    sheetArr = sh.UsedRange
    rowC = sh.UsedRange.Rows.Count
    colC = sh.UsedRange.Columns.Count
    For i = 1 To rowC
        For j = 1 To colC
            If Left(sheetArr(i, j), 1) = " " Then
                sh.Cells(i, j).Interior.ColorIndex = 37
            End If
            If Right(sheetArr(i, j), 1) = " " Then
                sh.Cells(i, j).Interior.ColorIndex = 37
            End If
        Next j
    Next i


End Sub

Result in sheet (column D selected):

Book1
ABCDEF
1
2
3No Space
4No Space
5Space After
6 Space Before
7
8
9
10
11
12
Sheet1
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Power Query --> Right click on column --> Trim. Refresh query as needed.
 
Upvote 0
Power Query --> Right click on column --> Trim. Refresh query as needed.
Thank you, but it is important to me to just highlight these cells and count them, not to trim them - it will be done as next step.
 
Upvote 0
Power Query --> Duplicate column --> Right click on new column --> Trim --> New column: length of original column --> New column: length of trimmed column --> conditional column: the two lengths don't match = specific text.
 
Upvote 0
Power Query --> Duplicate column --> Right click on new column --> Trim --> New column: length of original column --> New column: length of trimmed column --> conditional column: the two lengths don't match = specific text.
Thanks! It works, but I would love to have it implemented by macro, so it can be done in one click only and I will see msg box with counter
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,196
Latest member
Maxkapoor

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