#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 43
Default Removing text

I have a variable and I need to remove everything after the last "_" in a
string including the "_".

Example
abc_def_ghi_jklmnop

Result: abc_def_ghi
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4,339
Default Removing text

=MID(B1,1,FIND("#",SUBSTITUTE(B1,"_","#",LEN(B1)-LEN(SUBSTITUTE(B1,"_",""))))-1)

data in B1

HTH

"pokdbz" wrote:

I have a variable and I need to remove everything after the last "_" in a
string including the "_".

Example
abc_def_ghi_jklmnop

Result: abc_def_ghi

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4,624
Default Removing text

One way:

=LEFT(A1, FIND("^", SUBSTITUTE(A1, "_", "^", LEN(A1) -
LEN(SUBSTITUTE(A1, "_", "")))) - 1)

In article ,
pokdbz wrote:

I have a variable and I need to remove everything after the last "_" in a
string including the "_".

Example
abc_def_ghi_jklmnop

Result: abc_def_ghi

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 43
Default Removing text

On the last substitue its giving an error: Function or Sub not defined.

"JE McGimpsey" wrote:

One way:

=LEFT(A1, FIND("^", SUBSTITUTE(A1, "_", "^", LEN(A1) -
LEN(SUBSTITUTE(A1, "_", "")))) - 1)

In article ,
pokdbz wrote:

I have a variable and I need to remove everything after the last "_" in a
string including the "_".

Example
abc_def_ghi_jklmnop

Result: abc_def_ghi


  #5   Report Post  
Posted to microsoft.public.excel.misc
CLR CLR is offline
external usenet poster
 
Posts: 1,998
Default Removing text

=left(a1,11)

Vaya con Dios,
Chuck, CABGx3



"pokdbz" wrote:

I have a variable and I need to remove everything after the last "_" in a
string including the "_".

Example
abc_def_ghi_jklmnop

Result: abc_def_ghi



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4,339
Default Removing text

Check for typing errors: it worked fine for me.

"pokdbz" wrote:

On the last substitue its giving an error: Function or Sub not defined.

"JE McGimpsey" wrote:

One way:

=LEFT(A1, FIND("^", SUBSTITUTE(A1, "_", "^", LEN(A1) -
LEN(SUBSTITUTE(A1, "_", "")))) - 1)

In article ,
pokdbz wrote:

I have a variable and I need to remove everything after the last "_" in a
string including the "_".

Example
abc_def_ghi_jklmnop

Result: abc_def_ghi


  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 43
Default Removing text

I put this into the cell formula and it worked fine.

What I need it for is a macro in VBA. I tried it in there and it gave the
error on Substitue and I replaced that with Replace but now Its giving me an
error on the Find .

Do you have any suggestions?

"Toppers" wrote:

Check for typing errors: it worked fine for me.

"pokdbz" wrote:

On the last substitue its giving an error: Function or Sub not defined.

"JE McGimpsey" wrote:

One way:

=LEFT(A1, FIND("^", SUBSTITUTE(A1, "_", "^", LEN(A1) -
LEN(SUBSTITUTE(A1, "_", "")))) - 1)

In article ,
pokdbz wrote:

I have a variable and I need to remove everything after the last "_" in a
string including the "_".

Example
abc_def_ghi_jklmnop

Result: abc_def_ghi

  #8   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4,339
Default Removing text

Range("c1") = Left(Range("a1"), Application.Find("^",
Application.Substitute(Range("A1"), "_", "^", _
Len(Range("a1")) - Len(Application.Substitute(Range("a1"), "_", "")))) - 1)

"pokdbz" wrote:

I put this into the cell formula and it worked fine.

What I need it for is a macro in VBA. I tried it in there and it gave the
error on Substitue and I replaced that with Replace but now Its giving me an
error on the Find .

Do you have any suggestions?

"Toppers" wrote:

Check for typing errors: it worked fine for me.

"pokdbz" wrote:

On the last substitue its giving an error: Function or Sub not defined.

"JE McGimpsey" wrote:

One way:

=LEFT(A1, FIND("^", SUBSTITUTE(A1, "_", "^", LEN(A1) -
LEN(SUBSTITUTE(A1, "_", "")))) - 1)

In article ,
pokdbz wrote:

I have a variable and I need to remove everything after the last "_" in a
string including the "_".

Example
abc_def_ghi_jklmnop

Result: abc_def_ghi

  #9   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 43
Default Removing text

Great thanks

"Toppers" wrote:

Range("c1") = Left(Range("a1"), Application.Find("^",
Application.Substitute(Range("A1"), "_", "^", _
Len(Range("a1")) - Len(Application.Substitute(Range("a1"), "_", "")))) - 1)

"pokdbz" wrote:

I put this into the cell formula and it worked fine.

What I need it for is a macro in VBA. I tried it in there and it gave the
error on Substitue and I replaced that with Replace but now Its giving me an
error on the Find .

Do you have any suggestions?

"Toppers" wrote:

Check for typing errors: it worked fine for me.

"pokdbz" wrote:

On the last substitue its giving an error: Function or Sub not defined.

"JE McGimpsey" wrote:

One way:

=LEFT(A1, FIND("^", SUBSTITUTE(A1, "_", "^", LEN(A1) -
LEN(SUBSTITUTE(A1, "_", "")))) - 1)

In article ,
pokdbz wrote:

I have a variable and I need to remove everything after the last "_" in a
string including the "_".

Example
abc_def_ghi_jklmnop

Result: abc_def_ghi

  #10   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default Removing text

On Mon, 6 Aug 2007 10:32:17 -0700, pokdbz
wrote:

I put this into the cell formula and it worked fine.

What I need it for is a macro in VBA. I tried it in there and it gave the
error on Substitue and I replaced that with Replace but now Its giving me an
error on the Find .

Do you have any suggestions?


Here's a different approach:

=================================
Option Explicit
Sub foo()
Dim c As Range
Dim s() As String

For Each c In Selection
s = Split(c.Text, "_")
ReDim Preserve s(UBound(s) - 1)
c.Offset(0, 1).Value = Join(s, "_")
Next c

End Sub
=================================
--ron


  #11   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default Removing text

On Mon, 06 Aug 2007 20:00:30 -0400, Ron Rosenfeld
wrote:

On Mon, 6 Aug 2007 10:32:17 -0700, pokdbz
wrote:

I put this into the cell formula and it worked fine.

What I need it for is a macro in VBA. I tried it in there and it gave the
error on Substitue and I replaced that with Replace but now Its giving me an
error on the Find .

Do you have any suggestions?


Here's a different approach:

=================================
Option Explicit
Sub foo()
Dim c As Range
Dim s() As String

For Each c In Selection
s = Split(c.Text, "_")
ReDim Preserve s(UBound(s) - 1)
c.Offset(0, 1).Value = Join(s, "_")
Next c

End Sub
=================================
--ron


Might want to do an error handler:

=========================
Option Explicit
Sub foo()
Dim c As Range
Dim s() As String

On Error Resume Next
For Each c In Selection
s = Split(c.Text, "_")
ReDim Preserve s(UBound(s) - 1)
c.Offset(0, 1).Value = Join(s, "_")
Next c

End Sub
==========================
--ron
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing text Dave Excel Discussion (Misc queries) 13 July 23rd 08 09:02 PM
removing blanks at the end of text Roger Excel Worksheet Functions 5 December 12th 06 08:52 PM
Removing Text Tubthumper Excel Worksheet Functions 3 October 19th 06 06:29 PM
Please help with removing text string excelator Excel Discussion (Misc queries) 3 August 15th 06 12:08 AM
Removing text characters Scott Excel Worksheet Functions 4 August 11th 05 12:19 PM


All times are GMT +1. The time now is 02:42 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"