VBA concantenate formula creation excel 2010

kainfriends

New Member
Joined
Jun 25, 2013
Messages
2
Hello!
I am creating an excel spreadsheet that will update numbers based product id and product type.
i identify the column by concantenating both into a new cell which is my index.

currently i have VBA primed to add more columns but i have trouble with this one little piece.

testval = ("CONCANTENATE(" & add1 & ","" """ & "," & add2& ") ")
Where add1 = M2, and add2 = M4

the expected output
testval = "CONCANTENATE(M2," ",M4)

I currently get a compile error "expected:)"
i have tried removing the end part
testval = ("CONCANTENATE(" & add1 & ","" """ & "," & add2)
and it yields
"CONCANTENATE(M2," ",M4"

i am still testing and i don't know if the formula will work without the closing parentheses

thanks for your help in advance
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
CONCATENATE is a worksheet function, not a VBA function, just use the ampersand &
Code:
testval=add1 & "," & add2
 
Upvote 0
CONCATENATE is a worksheet function, not a VBA function, just use the ampersand &
Code:
testval=add1 & "," & add2

thanks for the reply.

i am trying to put the concantenate formula in a cell using VBA. I am concerned that if i use the code above, the result will only be recalculated when the code is run. I would prefer excel to recalculate this whenever any changes are made.

hope this gives better definition. if the above will recalculate just like the formula, let me know.

thanks again!
 
Upvote 0
Not sure why you're using a variable to hold the formula. You need to assign the formula to a range:

Code:
Sub test()
Dim add1 As String, add2 As String
add1 = "M2"
add2 = "M4"
Range("A1").Formula = "=CONCATENATE(" & add1 & "," & Chr(34) & " " & Chr(34) & "," & add2 & ")"
End Sub
 
Upvote 0
Alternatively, since there really isn't much point to the CONCATENATE function, this would also do what you want:

Code:
Sub test()
Dim add1 As String, add2 As String
add1 = "M2"
add2 = "M4"
Range("A1").Formula = "=" & add1 & "&" & Chr(34) & " " & Chr(34) & "&" & add2
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,181
Members
452,615
Latest member
bogeys2birdies

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