Remove Spaces till the first character in cell

fari1

Active Member
Joined
May 29, 2011
Messages
362
i'm working on the data, which has all the data in column A in too many cells, and it has problem of extra spaces before the start and end of the character strings e.g
HTML:
          my name is lavisha

and i want to have just my name is lavisha, without any space in the start of MY and after lavisha.
would appreciate any help on it
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
As long as they are regular spaces and not special characters, the TRIM function should do the job.

=TRIM(A1)
 
Upvote 0
Use LTRIM (for left trim) and RTRIM (for right trim) in VBA.
 
Upvote 0
Sub trial()
With ActiveSheet.UsedRange
With .Cells(1, .Columns.Count + 1).EntireColumn.Resize(500, 1)
.FormulaR1C1 = "=TRIM(RC1)"
Range("A1:A50000").Value = .Value
.EntireColumn.Delete
End With
End With
End Sub

i've found a code from here, it does N/A if it doesn't find the row with text, can u amend it to last used row of excel?
 
Upvote 0
It looks like TRIM does work by itself in VBA also (maybe I was thinking of Access functions).

Here is how I would do it for all values in column A:
Code:
Sub MyRemoveSpaces()
 
    Dim cell As Range
    For Each cell In Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        cell = Trim(cell.Text)
    Next cell
    
End Sub
 
Upvote 0
yes, it worked like a charm, can u pl further give me a piece of code, which may delete rows that contain numbers alongwith text, i just want to keep those cells in column A which contain text
 
Upvote 0
can u pl further give me a piece of code, which may delete rows that contain numbers alongwith text, i just want to keep those cells in column A which contain text
Since this a totally separate brand new question, it is best to post a new question for it.

The general rule of thumb is this:

1. If it is a clarification, "bump", or directly-related follow-up (dependent on the previous question), then it should be posted in the same thread as the original question.

2. If it is a brand new question that is not dependent on the previous question (it can stand on its own), it should be posted as a new question.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,673
Members
452,937
Latest member
Bhg1984

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