Truncate a specific character in a column ,some cells in the column might have that paricular character but some dont.

WilsonST

New Member
Joined
Dec 11, 2021
Messages
6
Office Version
  1. 2019
Hi , I am seeking for help of truncate a specific character from range of words in a columns.
In the raange of column, some cell might contain that specific character but some dont.
The result will then substitutes its original data.
any one can help ?

I tried
Sub remove_text_after_char()
Dim rng As Range
Dim cell As Range
Set rng = Application.Selection
For Each cell In rng
cell.Offset(0, 1).Value = Left(cell, InStr(cell, "@") - 1)
Next cell
End Sub

but this will give an error when a cell doesnt consists of that character "@" and yet its result will be copied to next column ( cell)
but I was thinking of getting the result to replace its original data.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
VBA Code:
Public Sub TruncateText()
   Dim rng, cell As Range
   Dim i As Integer, str As String
   Set rng = Selection
   For Each cell In rng
      For i = 1 To cell.Characters.Count
         If cell.Characters(i, 1).Text = "@" Then
            cell.Value2 = cell.Characters(1, i - 1).Text
         End If
      Next i
   Next cell
End Sub
 
Upvote 0
Welcome to the MrExcel board!

Here is another option that could try with a copy of your workbook.

VBA Code:
Sub remove_text_after_char_v2()
  Selection.TextToColumns , xlDelimited, , , False, False, False, False, True, "@", Array(Array(1, 1), Array(2, 9))
End Sub
 
Upvote 0
Another option
VBA Code:
Sub WilsonST()
   With Selection
      .Value = Evaluate(Replace("left(#,find(""@"",#&""@"")-1)", "#", .Address))
   End With
End Sub
 
Upvote 0
Thanks you guys for help ..i ma newbie try to simplify my daily job using own construct of multiple small program ( sub ) into a single one.
You Guys Are Really Awesome !
 
Upvote 0
You're welcome. Glad we could help. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

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