Inserting "Substitute function" using VBA

mathsbeauty

Board Regular
Joined
Apr 23, 2011
Messages
89
Hello! I want to insert substitute function in excel worksheet. I have tried the following

Range("B1").Formula = "=Substitute(A1,"p","q")"

or

Range("B1").Formula = Application.WorksheetFunction.Substitute(A1, "p", "q")

In first case I get error . In second case i don't get any error, but the formula doesn't get inserted in the cell. Any help?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You were close!
Turn on macro recorder. Click in B1. Press Enter.

This is recorded:
Code:
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "=SUBSTITUTE(RC[-1],""p"",""q"")"
    Range("B2").Select
Which can be simplified to this:
Code:
Range("B1").FormulaR1C1 = "=SUBSTITUTE(RC[-1],""p"",""q"")"
 
Upvote 0
And if you know your formula is going in to B1 and is referring to A1 you can also do it without the R1C1 notation - but the double-quotes were your real issue.
Code:
Range("B1").Formula = "=Substitute(A1,""p"",""q"")"
 
Upvote 0
Thanks Pbornemeier! It works. I forgot basics. Taking the help of macro recorder. I modified it as
Range("B1").Formula = "=Substitute(A1,""p"",""q"")"
 
Upvote 0
There is another trouble.
Code:
Sub macro19()
Dim val As Variant
val = Application.WorksheetFunction.Substitute(a1, "p", "q")
MsgBox val
End Sub
Why nothing happens to val?
 
Upvote 0
This is not a worksheet formula, so you cannot refer to a range by its address (VB thinks it is a variable name). This is how you would write the code you posted...
Code:
Sub macro19()
  Dim val As Variant
  val = Application.WorksheetFunction.Substitute(Range("a1"), "p", "q")
  MsgBox val
End Sub
However, while VB is tolerating it, you should not use val as a variable name because it is the name of a built-in VB function... the Val function converts string values of numbers to actual numbers (look it up in the VB Help Files).
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,424
Members
452,914
Latest member
echoix

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