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!
 
Ok I got it to trim down- THANK YOU for your patience, but I'm still having problems with deleting data and receiving Debug errors. When I delete/clear the data, I receive:

Run-time Error '-2147417848 (80010108)':
Method 'Value' of object 'Range' failed

At the red line of code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim oCell As Range
For Each oCell In Target
    [COLOR=red][B]oCell.Value = WorksheetFunction.Trim(oCell.Value)
[/B][/COLOR]Next oCell
 ..............

Any suggestions? I really appreciate you taking the time with this!
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
You may also want to disable events...

Basically you have an almost never ending loop..

Changing the value of a cell, triggers the Change Event.
Change Event Code then changes the value of target, triggering the Change event again
Change Event Code then changes the value of target, triggering the Change event again
Change Event Code then changes the value of target, triggering the Change event again
Change Event Code then changes the value of target, triggering the Change event again
etc....


At the very beginning of the code, put

Application.EnableEvents = False

Then the rest of your code

Then at the end, reset it to True

Application.EnableEvents = True



Not sure that's the problem, but it should definately help..
 
Upvote 0
WOW! Jonmo1, you are an absolute life saver! I'm humbled beyond words. AWESOME! IT WORKS PERFECTLY!

Thank you so much for following this post and responding so quickly!

Excellent explanations as well!

It is much appreciated!

:beerchug:
 
Upvote 0

Forum statistics

Threads
1,224,604
Messages
6,179,857
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