Trim Black Spaces inform of Number in range

hajiali

Well-known Member
Joined
Sep 8, 2018
Messages
624
Office Version
  1. 2016
Platform
  1. Windows
Hello I have a large Range("F2:SF1500") were only numbers are imputed. I need a macro that will remove/trim any blank spaces before the number in this range. I currently have

VBA Code:
Sub Trim()
Selection.Replace " ", ""
End Sub

However, this takes a very very long time and at times crashes. Not all cells in the range will have value and not all Values in cell will have black spaces before the number. I would like to see if VBA can locate and select the Cells in range which the values have blank spaces before the Number and then run the above to reduce the run time or If there is an alternate way of doing this.

any help is greatly appreciated :).
 

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).
Are you selecting the whole sheet, or just that range?

How exactly are these numbers entered?
It may be better to use Data Validation, if possible, to make sure only valid numbers are entered, instead of trying to fix them after the fact.

Also, you should never use reserved words (names of existing functions, properties, methods, etc) like "Trim" as the name of your procedures, functions, or variables.
Doing so can cause errors and unexpected results.

Not sure if this would run any better for you:
VBA Code:
Sub MyTrim()
    Application.ScreenUpdating = False
    Range("F2:SF1500").Replace " ", ""
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks Joe4 for the Quick Reply. I would just need this done for that range. The numbers are being inputted via Email and Via Userform. never manually typed in the range. also thanks for the tip. I will give that a try.
 
Upvote 0
The numbers are being inputted via Email and Via Userform.
There must be some process that takes them from the Email or UserForm and puts them into Excel.
You may be able to address the issue there.
You may also be able to add criteria to the UserForm to restrict it to only allow numeric values.
 
Upvote 0
The code for the email is on the post HERE. not sure Where in the code I can fix this issue.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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