VBA Help: Removing Spaces From Left and Right of A Cell On Worksheet_Change

milwphil

Board Regular
Joined
Aug 1, 2010
Messages
120
I'm trying to come up with code and keep the same type of method consistent with the spreadsheet that I inherited.

All I want to accomplish is that if someone types something in a cell and it has spaces on the front end or back end of whatever they typed, the space is removed immediately (after they select another cell)

This is where I thought it was most logical to Trim the spaces, but it's just not happening on Dates etc.

Can anyone tell me what I'm missing or doing wrong?:
Code:
For Each oCell In Target
    oCell.Value = Validations.CleanNonStndChars(CStr(oCell.Value))
    oCell.Value = WorksheetFunction.Trim(CStr(oCell.Value))
   Next oCell

THANKS IN ADVANCE!
 

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.
Is there really a way to change multiple cells at the same time in Change event?

Try
Code:
Target.Value = Trim$(CStr(Target.Value))
 
Upvote 0
Try using a worksheet change function:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Target.Value = WorksheetFunction.Trim(Target.Value)
End Sub
 
Upvote 0
kpark91 and CWatts,

Thanks for the responses! Unfortunately, I haven't been able to get the cells to trim down.

Kpark91, where should I be putting that code? I assume it would replace:
Code:
oCell.Value = WorksheetFunction.Trim(CStr(oCell.Value))

CWatts, I don't understand but I'm getting an error message "Ambigiuous Name Detected: Worksheet_Change"

THANKS AGAIN!
Phil
 
Upvote 0
It's not code that you just place anywhere, it is specific to the sheet. If you look in your project explorer you'll see a tre where you'll have your worksheets listed and your modules.

Doubleclick on the worksheet you want that code to apply to.

Up top, in the drop down, change "(General)" to "Worksheet". In the next sheet change "SelectionChange" to "Change"

You'll see it creates Sub templates for each type. Just replace the default with the code I provided and modify it as you would any VBA code.
 
Upvote 0
Is there really a way to change multiple cells at the same time in Change event?

Try
Code:
Target.Value = Trim$(CStr(Target.Value))

Yes..

Example, if you copied a range...Say A1:E10
And pasted it to G2

Then 50 cells (G2:K11) all just changed at the same time.
And Target is now represents 50 cells (G2:K11), so this line will error
Target.Value = WorksheetFunction.Trim(Target.Value)


That's the purpose for looping through Target..

Code:
For Each oCell In Target
    oCell.Value = WorksheetFunction.Trim(oCell.Value)
Next oCell


Hope that helps.


Another way to change multiple cells at once is to highlight a range, say A1:A10, type anything, and press CTRL + ENTER
 
Last edited:
Upvote 0
Thanks for the response! It appears anytime I use that code on "Worksheet" "Change" no matter what...the second macro also listed as "Worksheet_Change" is flagged as an error (same "ambigious name" error).

I was able to incorporate the line of code into the other "Worksheet_Change" and it removed the spaces without receiving an error.

Of course, Excel can't be that simple:

The problem I'm having now is that when I delete the data from the cells-(cut or keystroke delete), I get a Runtime Error '13' Type Mismatch at that the line I added. Here's my entire macro:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
[COLOR=red][B]Target.Value = WorksheetFunction.Trim(Target.Value)[/B][/COLOR]
 If Target.Cells.Count = 1 Then
  Constants.DisableWorkbook
  If Target.Row > 1 Then
   Select Case Target.Column
    Case 1
     Validations.OrderReport Target
   End Select
  End If
  Constants.EnableWorkbook
 ElseIf Target.Cells.Count > 1 Then
  If Operations.CheckUndoStack("Paste") Then
   Constants.DisableWorkbook
   Constants.DisableScreen
 
   Dim oCell As Range
 
   Application.Undo
   Target.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, False, False
 
 
   For Each oCell In Target
    oCell.Value = Validations.CleanNonStndChars(CStr(oCell.Value))
   Next oCell
   Constants.EnableScreen
   Constants.EnableWorkbook
  End If
  If Target.Cells.Locked Then
   Target.Locked = False
  End If
 End If
End Sub
 
Upvote 0
The problem I'm having now is that when I delete the data from the cells-(cut or keystroke delete), I get a Runtime Error '13' Type Mismatch at that the line I added. Here's my entire macro:

See previous post #6.

When you cut a row, you're changing multiple cells at the same time, so you have to loop through target.
 
Upvote 0
Jonmo1,

Thank you for your response! And please forgive me, but I'm not sure where I should add the loop:
Code:
For Each oCell In Target
    oCell.Value = WorksheetFunction.Trim(oCell.Value)
Next oCell

I think I may be using it incorrectly because by itself and how I'm adding it-it's not trimming the data.
 
Upvote 0
Put it right at the same place you have the red highlighted line..

Just REPLACE this line
Target.Value = WorksheetFunction.Trim(Target.Value)

With
Code:
For Each oCell In Target
    oCell.Value = WorksheetFunction.Trim(oCell.Value)
Next oCell
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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