Add a formula with concatenate in a range of cells using VBA

jhonty4

Board Regular
Joined
May 16, 2016
Messages
85
Hi, I am trying to put this formula " ="*"&MID(B7,SEARCH("(",B7)+1,SEARCH(")",B7)-SEARCH("(",B7)-1)&"*" " in a range of cell from row 7 to lastrow(Column E) using VBA code. but cannot figure out how to do that using VBA because concatenate is involved.

Code:
For k = 7 To lnglastrow
        Range("G" & k).Formula ="=MID(B7,SEARCH("(",B7)+1,SEARCH(")",B7)-SEARCH("(",B7)-1)"
next k

The above code needs to be changed such that the range will be updated with the formula mentioned above. TIA
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try

Code:
Range("G7:G" & Range("E" & Rows.Count).End(xlUp).Row).FormulaR1C1 = _
        "=MID(RC[-5],SEARCH(""("",RC[-5])+1,SEARCH("")"",RC[-5])-SEARCH(""("",RC[-5])-1)"

or

Code:
Range("G7:G" & Range("E" & Rows.Count).End(xlUp).Row).Formula = _
        "=MID(B7,SEARCH(""("",B7)+1,SEARCH("")"",B7)-SEARCH(""("",B7)-1)"


because concatenate is involved

:confused:
 
Last edited:
Upvote 0
You don't need to loop, just Autofill:

Note the triple quotation marks """ to insert a quotation mark into a string

Code:
Sub SetFormula()
Dim sFormula As String
Dim lngLastRow As Long 'Last row


    sFormula = "=MID(B7,SEARCH(" & """(""" & ",B7)+1,SEARCH(" & """)""" & ",B7)-SEARCH(" & """(""" & ",B7)-1)"

'insert code to set lngLastRow. I wasn't sure which column you use to determine last row

    Range("G7").Formula = sFormula
    Range("G7").AutoFill Range("G7:G" & lngLastRow)
    
End Sub
 
Last edited:
Upvote 0
Note the triple quotation marks """ to insert a quotation mark into a string

It doesn't need triple quotation marks if you don't concatenate the formula only doubles, see post #2
 
Last edited:
Upvote 0
@jhonty4, sorry didn't put in the wildcards

Code:
Range("G7:G" & Range("E" & Rows.Count).End(xlUp).Row).Formula = _
        "=""*""&MID(B7,SEARCH(""("",B7)+1,SEARCH("")"",B7)-SEARCH(""("",B7)-1)&""*"""

or

Code:
Range("G7:G" & Range("E" & Rows.Count).End(xlUp).Row).FormulaR1C1 = _
        "=""*""&MID(RC[-5],SEARCH(""("",RC[-5])+1,SEARCH("")"",RC[-5])-SEARCH(""("",RC[-5])-1)&""*"""

Didn't realise that. I just tested mine and it worked. Yours is much more succinct.

I knew it worked, just a small saving on your fingertips :biggrin:
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

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