TRIM & Proper Code VBA

jeran042

New Member
Joined
Jun 9, 2016
Messages
23
Hello everyone. I have a piece of code that I am trying to simplify. I want to Trim off any leading/trailing spaces and then convert all text to Proper. I want to apply this to the entire sheet. I do not want to have to select a section in order to run the code.

This is what I have so far:

Code:
[Sub ToProperCase()   For Each cell In ActiveSheet.UsedRange.Cells
        cell.Value = WorksheetFunction.Proper(cell.Value)
    Next
    Dim c As Range, rng As Range
    Set rng = Intersect(Selection, Selection.Parent.UsedRange)
    If rng Is Nothing Then
        MsgBox "No cells with values!"
        Exit Sub
    End If
    For Each c In rng
        If Not IsError(c) Then
            c.Value = MEGACLEAN(c)
        End If
    Next c
End Sub
Function MEGACLEAN(varVal As Variant)
Dim NewVal As Variant
If IsMissing(varVal) Then Exit Function
NewVal = Trim(varVal) 'remove spaces
NewVal = Application.WorksheetFunction.Clean(NewVal) 'remove most unwanted characters
NewVal = Application.WorksheetFunction.Substitute(NewVal, Chr(127), "") 'remove ASCII#127
NewVal = Application.WorksheetFunction.Substitute(NewVal, Chr(160), "") 'remove ASCII#160


MEGACLEAN = NewVal
End Function]
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hello everyone. I have a piece of code that I am trying to simplify. I want to Trim off any leading/trailing spaces and then convert all text to Proper. I want to apply this to the entire sheet. I do not want to have to select a section in order to run the code.

This is what I have so far:

Code:
[Sub ToProperCase()   For Each cell In ActiveSheet.UsedRange.Cells
        cell.Value = WorksheetFunction.Proper(cell.Value)
    Next
    Dim c As Range, rng As Range
    Set rng = Intersect(Selection, Selection.Parent.UsedRange)
    If rng Is Nothing Then
        MsgBox "No cells with values!"
        Exit Sub
    End If
    For Each c In rng
        If Not IsError(c) Then
            c.Value = MEGACLEAN(c)
        End If
    Next c
End Sub
Function MEGACLEAN(varVal As Variant)
Dim NewVal As Variant
If IsMissing(varVal) Then Exit Function
NewVal = Trim(varVal) 'remove spaces
NewVal = Application.WorksheetFunction.Clean(NewVal) 'remove most unwanted characters
NewVal = Application.WorksheetFunction.Substitute(NewVal, Chr(127), "") 'remove ASCII#127
NewVal = Application.WorksheetFunction.Substitute(NewVal, Chr(160), "") 'remove ASCII#160


MEGACLEAN = NewVal
End Function]
Hi jeran042, welcome to the boards.

At its most basic this should do what you describe, however depending on how many cells are in the used range of your sheet it the process time will vary:

Code:
Sub TidyValues()


Dim Cell As Range


For Each Cell In ActiveSheet.UsedRange
    Cell.Value = Application.WorksheetFunction.Proper(Cell.Value)
    Cell.Value = Application.WorksheetFunction.Trim(Cell.Value)
Next Cell


End Sub
 
Upvote 0
Hi jeran042, welcome to the boards.

At its most basic this should do what you describe, however depending on how many cells are in the used range of your sheet it the process time will vary:

Rich (BB code):
Sub TidyValues()


Dim Cell As Range


For Each Cell In ActiveSheet.UsedRange
    Cell.Value = Application.WorksheetFunction.Proper(Cell.Value)
    Cell.Value = Application.WorksheetFunction.Trim(Cell.Value)
Next Cell


End Sub
You could do that without using a loop...
Code:
[table="width: 500"]
[tr]
	[td]Sub TrimAndProper()
  ActiveSheet.UsedRange = Evaluate(Replace("IF(@="""","""",PROPER(TRIM(@)))", "@", ActiveSheet.UsedRange.Address))
End Sub[/td]
[/tr]
[/table]



Very Nice, is there a way to define a range? Say A:AK
For my method, like this...
Code:
Sub TrimAndProper()
  With Intersect([COLOR="#0000FF"][B]Columns("A:K")[/B][/COLOR], ActiveSheet.UsedRange)
    .Value = Evaluate(Replace("IF(@="""","""",PROPER(TRIM(@)))", "@", .Address))
  End With
End Sub
 
Last edited:
Upvote 0
Very Nice, is there a way to define a range? Say A:AK
To prevent needing to go to row 1048576 on all 37 columns, try this:

Code:
Sub TidyValues()
' Defines variables
Dim cCell As Range, cRange As Range, uCell As Range, uRange, LastRow As Long

' Sets check range as A1:AK1
Set cRange = Range("A1:AK1")
' For each cell in the check range
For Each cCell In cRange
    ' Define LastRow as the last row of the current column
    LastRow = ActiveSheet.Cells(Rows.Count, cCell.Column)
    ' Sets the update range as row 1 to the last row of the current column
    Set uRange = Range(Cells(1, cCell.Column), Cells(LastRow, cCell.Column))
    ' For each cell in the update range
    For Each uCell In uRange
        ' Update the cell value with Proper format
        uCell.Value = Application.WorksheetFunction.Proper(uCell.Value)
        ' Trim the cell value
        uCell.Value = Application.WorksheetFunction.Trim(uCell.Value)
    ' Move to next cell in update range
    Next uCell
' Move to next cell in check range
Next Cell

End Sub

[EDIT] - I wrote this before I saw Rick's reply. Rick's suggestion will definitely be faster / less resource hungry. I suspect something clever could be done like that here too.

[EDIT2] - Yup! Rick has suggested something good for this one too ;)
 
Last edited:
Upvote 0
Rick - This worked PERFECT!
Thank you for your help everyone,


To prevent needing to go to row 1048576 on all 37 columns, try this:

Code:
Sub TidyValues()
' Defines variables
Dim cCell As Range, cRange As Range, uCell As Range, uRange, LastRow As Long

' Sets check range as A1:AK1
Set cRange = Range("A1:AK1")
' For each cell in the check range
For Each cCell In cRange
    ' Define LastRow as the last row of the current column
    LastRow = ActiveSheet.Cells(Rows.Count, cCell.Column)
    ' Sets the update range as row 1 to the last row of the current column
    Set uRange = Range(Cells(1, cCell.Column), Cells(LastRow, cCell.Column))
    ' For each cell in the update range
    For Each uCell In uRange
        ' Update the cell value with Proper format
        uCell.Value = Application.WorksheetFunction.Proper(uCell.Value)
        ' Trim the cell value
        uCell.Value = Application.WorksheetFunction.Trim(uCell.Value)
    ' Move to next cell in update range
    Next uCell
' Move to next cell in check range
Next Cell

End Sub

[EDIT] - I wrote this before I saw Rick's reply. Rick's suggestion will definitely be faster / less resource hungry. I suspect something clever could be done like that here too.

[EDIT2] - Yup! Rick has suggested something good for this one too ;)
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,632
Members
449,241
Latest member
NoniJ

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