How to use TEXTJOIN in vba

capson

Board Regular
Joined
Jul 9, 2010
Messages
107
Hello,

I am trying to use TEXTOIN in vba

Code:
Sub AAA()
Dim str As String


    str = WorksheetFunction.TextJoin(" ", False, "E2:E6")
    MsgBox str


End Sub

This gives "E2:E6" as the return

If I try this instead:
str = WorksheetFunction.TextJoin(" ", False, E2:E6)

I get error

Is there a way to use TEXTJOIN in vba?

Thanks
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I am trying to use TEXTOIN in vba

Code:
Sub AAA()
Dim str As String

    str = WorksheetFunction.TextJoin(" ", False, "E2:E6")
    MsgBox str

End Sub

This gives "E2:E6" as the return

If I try this instead:
str = WorksheetFunction.TextJoin(" ", False, E2:E6)

I get error

Is there a way to use TEXTJOIN in vba?
I don't have a version of Excel that has TEXTJOIN in it, so this is just a guess. If the WorksheetFunction does, in fact, have a TextJoin member, I would think that third argument should be an actual range and not a text string address. Try the line of code this way and see if it works...

str = WorksheetFunction.TextJoin(" ", False, Range("E2:E6"))

As an aside, str is not a good name for a variable as that is the name of a built-in VB function.
 
Upvote 0
If that doesnt work Range(...)

then try seperating them out, ie:str = WorksheetFunction.TextJoin(" ", False,E2,E3,E4,E5,E6)
 
Upvote 0
Hello all,

I have the TEXTJOIN formula with IF condition.
=TEXTJOIN(",",TRUE,IF(B4:I4="A",B$:I3,""))

A-SHIFT AB-SHIFT BMONTH-JAN
EMPLOYEE12345678DATES OF SHIFT A
EMP01AABAABBB1,2,4,5
EMP02BBAABBBA
EMP03AAAABBBB


Can anyone help me to use this formula in vba (worksheet Function)?
(I don't want to use loop for this, using only worksheet function)
 
Upvote 0
You'd have to use Evaluate for that:

Code:
Dim v
v = Activesheet.Evaluate("TEXTJOIN("","",TRUE,IF(B4:I4=""A"",B$:I3,""""))")

for example.
 
Upvote 0
I didn't notice the error in your original formula. The B$ part at the end is missing a row number (I'd guess you meant B4)
 
Upvote 0
It
You'd have to use Evaluate for that:

Code:
Dim v
v = Activesheet.Evaluate("TEXTJOIN("","",TRUE,IF(B4:I4=""A"",B$:I3,""""))")

for example.
It's perfectly working now.
Can you help me with getting autofill this formula using evaluate? It's autofilling only cell value and not formula.
 
Upvote 0
You can't autofill it precisely because it is not a formula. If you want to autofill, you should just put the formula in the cells. Why do you want to use VBA?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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