VBA : Remove Number, Extra Spaces, Mark (.) from a Text

muhammad susanto

Well-known Member
Joined
Jan 8, 2013
Messages
2,077
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hi all.

i need vba code to remove series number from front text, remove extra spaces, mark dot (.)
i want the code work in any sheet name, cause i want to make code as personal macro/excel add-ins
the code work step:
- select range/cell then click/run macro
here this sample
before run macroafter run macro
1. John WeJohn We
2. Sisca MadameSisca Madame
12.Frans BonjourFrans Bonjour
124. Salsa Micro GavSalsa Micro Gav


anyone would help me, greatly appreciated..

susanto
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi, the code below should work:
VBA Code:
Sub forSplitOnPeriod()
  Dim cell As Range
  
  On Error Resume Next
  
  For Each cell In Selection
    cell.Value = Trim(Split(cell.Value, ".")(1)) 'Split on period and remove lead space
  Next cell
End Sub
 
Upvote 0
Solution
hi all.

i need vba code to remove series number from front text, remove extra spaces, mark dot (.)
i want the code work in any sheet name, cause i want to make code as personal macro/excel add-ins
the code work step:
- select range/cell then click/run macro
here this sample
before run macroafter run macro
1. John WeJohn We
2. Sisca MadameSisca Madame
12.Frans BonjourFrans Bonjour
124. Salsa Micro GavSalsa Micro Gav


anyone would help me, greatly appreciated..

susanto

Why VBA if I may ask? A simple Excel formula can achieve what you want.

Excel Formula:
=TRIM(MID(A1,FIND(".",A1)+1,256))

1647857535688.png


If you still want VBA then you can use the above formula in VBA to do the range conversion in ONE GO (No loops required)

VBA Code:
Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long
    Dim rng As Range
    
    '~~> Change this to the relevant sheet
    Set ws = Sheet1
    
    With ws
        '~~> Find last row in Col A
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row
        
        '~~> Identify the range
        Set rng = Range("A1:A" & lRow)
        
        '~~> Update the entire range in 1 GO!
        rng = Evaluate("INDEX(TRIM(MID(" & _
              rng.Address & _
              ",FIND("".""," & _
              rng.Address & _
              ")+1,255)),)")
    End With
End Sub
 
Upvote 0
Similar to @JEC & @Siddharth Rout but less functions required.

VBA Code:
Sub Tidy()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("trim(replace(#,1,find(""."",#),""""))", "#", .Address))
  End With
End Sub

@Siddharth Rout
Your code will give unusual results if Sheet1 is not the active sheet. You have qualified lRow to ws but not rng. Therefore rng will be on the active sheet, not necessarily Sheet1, yet lRow will be determined from Sheet1.

@muhammad susanto
Note that the codes in posts 3, 4 & 5 may not be suitable if you have any data in the columns that does not contain a "."
 
Upvote 0
Similar to @JEC & @Siddharth Rout but less functions required.

VBA Code:
Sub Tidy()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("trim(replace(#,1,find(""."",#),""""))", "#", .Address))
  End With
End Sub

@Siddharth Rout
Your code will give unusual results if Sheet1 is not the active sheet. You have qualified lRow to ws but not rng. Therefore rng will be on the active sheet, not necessarily Sheet1, yet lRow will be determined from Sheet1.

@muhammad susanto
Note that the codes in posts 3, 4 & 5 may not be suitable if you have any data in the columns that does not contain a "."
Yes you are right. I should have qualified the rng object.

VBA Code:
Set rng = Range("A1:A" & lRow)

Should have been

VBA Code:
Set rng = .Range("A1:A" & lRow)
 
Upvote 0
Should have been

VBA Code:
Set rng = .Range("A1:A" & lRow)
Even with that correction, it won't work if Sheet1 is not the active sheet - without further amendment.
I'm not being critical about that since my code (& the others) also only works on the active sheet - just pointing it out in case you thought setting the worksheet like you did would be sufficient to work whether active or not. :)
 
Upvote 0
Even with that correction, it won't work if Sheet1 is not the active sheet - without further amendment.
I'm not being critical about that since my code (& the others) also only works on the active sheet - just pointing it out in case you thought setting the worksheet like you did would be sufficient to work whether active or not. :)

Peter, yes you are right. I should have tested it before posting.

Here is the right way to do it

VBA Code:
Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long
    Dim rng As Range
   
    'MsgBox ActiveSheet.Name
   
    '~~> Change this to the relevant sheet
    Set ws = Sheet1
   
    With ws
        '~~> Find last row in Col A
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row
       
        '~~> Identify the range
        Set rng = .Range("A1:A" & lRow)
       
        '~~> Update the entire range in 1 GO!
        rng = Evaluate("INDEX(TRIM(MID('" & _
              .Name & "'!" & rng.Address & _
              ",FIND(""."",'" & _
              .Name & "'!" & rng.Address & _
              ")+1,255)),)")
    End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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