Yet another Replace question...

jacobRadio

New Member
Joined
Jul 15, 2011
Messages
4
First off, this is my first question ever posted to any Excel forum and I have been using Excel and VBA for several years. So I would like to point out that I have found every answer to every question (whether I liked the answer or not) without posting, so please don't be rude if I missed the thread where this was covered, because as it may be quite possible I missed it, I DID look. Plus, I'm very sensitve, so be nice. :beerchug:

I have a list in about 800 cells, which have a changing length and changing string.

(ex.)

AAABBBCCC_TST##.StringNameEtc
DDEEFF_TST##.OtherStringName

What I would like to do is be able to replace the first characters (no matter what the length) up until the "_". This would allow me to replace:

AAABBBCCC_TST##.StringNameEtc
and
DDEEFF_TST##.OtherStringName

With the same variable like:

AhhMuchBetter_TST##.StringNameEtc
AhhMuchBetter_TST##.OtherStringName

Thank you for your replies :)
 
Last edited:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
will all 800 cells have the same prefix before "_"?

Yes.

The list will go from something like this:

AAABBBCCC_TST##.StringTextOne
AAABBBCCC_TST##.StringTextTwo
AAABBBCCC_TST##.StringTextThree
AAABBBCCC_TST##.StringTextFour
AAABBBCCC_TST##.StringTextFive

To:

OTHERPREFIX_TST##.StringOne
OTHERPREFIX_TST##.StringTwo
OTHERPREFIX_TST##.StringThree
OTHERPREFIX_TST##.StringFour
OTHERPREFIX_TST##.StringFive

(and so on)
 
Upvote 0
Try (assuming your list in in column A starting in Row 1)

Code:
Sub MyPrefix()
Dim c As Range
Dim lastRow As Long

lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For Each c In Range("A1:A" & lastRow)
    If c <> "" Then c.Value = "OTHERPREFIX" & Right(c, (Len(c) - WorksheetFunction.Find("_", c)) + 1)
Next c

End Sub
 
Upvote 0
Try (assuming your list in in column A starting in Row 1)

Code:
Sub MyPrefix()
Dim c As Range
Dim lastRow As Long
 
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For Each c In Range("A1:A" & lastRow)
    If c <> "" Then c.Value = "OTHERPREFIX" & Right(c, (Len(c) - WorksheetFunction.Find("_", c)) + 1)
Next c
 
End Sub


Thank you!! That did exactly the what I needed. I didn't know about:
Code:
-WorksheetFunction.Find("_"), c)) + 1)
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,794
Members
452,943
Latest member
Newbie4296

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