Remove Characters from String Macro

boldcode

Active Member
Joined
Mar 12, 2010
Messages
347
Hi,

The formula below works great when I use it in a cell on a worksheet:

=Left(RunInformation!E6, Find("_", Substitute(RunInformation!E6, "_", "_", 1)) - 1)

What it does, it removes all the characters after the first underscore and the underscore as well. I want to now use this formula with the following VBA code, but I get an error highlighting the word "Subsitute" and saying that the function is not defined. I really don't know how to fix this

Code:
Range("A3:" & Number2Char(LastColumn) & "3").Select
    ActiveCell.FormulaR1C1 = Left(RunInformation!E6, Find("_", Substitute(RunInformation!E6, "_", "_", 1)) - 1) & " Points"
    Selection.Font.Bold = True
    With Selection.Font
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Code:
    ActiveCell.FormulaR1C1 = "Left(RunInformation!E6, Find(""_"", Substitute(RunInformation!E6, ""_"", ""_"", 1)) - 1) & "" Points"""
 
Upvote 0
It's the double quotes. If you want to build a string with double quotes in, you have to repeat them
 
Upvote 0
kpark91,

Your suggested code is not working. When I look at the worksheet it shows me the following:

Left(RunInformation!E6, Find("_", Substitute(RunInformation!E6, "_", "_", 1)) - 1) & " Points"

It should show me:

2010 Points
 
Upvote 0
Try this again;;; I forgot the equal sign.
Code:
    ActiveCell.Formula = "=Left(RunInformation!E6, Find(""_"", Substitute(RunInformation!E6, ""_"", ""_"", 1)) - 1) & "" Points"""

Would this work?
 
Last edited:
Upvote 0
You shouldn't need the R1C1 if you are going to use A1 style references.
 
Upvote 0
It is also very rare that you need to Select anything in VBA you can work with ranges directly.

How are you determining the last column? You shouldn't need a UDF for that either.

Code:
With Range("A3:" & Number2Char(LastColumn) & "3")
    .Formula = "=Left(RunInformation!E6, Find(""_"", Substitute(RunInformation!E6, ""_"", ""_"", 1)) - 1) & "" Points"""
    .Font.Bold = True
End With
 
Upvote 0
Let's go back to your original formula for a moment...

=Left(RunInformation!E6, Find("_", Substitute(RunInformation!E6, "_", "_", 1)) - 1)

Unless you have a type, this part...

Substitute(RunInformation!E6, "_", "_", 1)

appears to do nothing. Well, actually it does take the time to do something... it replace all underbars with underbar which is nothing more than the original contents of the cell. Perhaps you meant this instead...

=Left(RunInformation!E6, Find("_", RunInformation!E6&"_") - 1)

which guarantees that an underbar will be found even if E6 doesn't have one (thus returning the entire contents of E6 and preventing the FIND error when no underbar exists)?
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

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