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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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
You'd have to use Evaluate for that:

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

for example.

I tried this but it's showing #VALUE!
 
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,214,376
Messages
6,119,174
Members
448,870
Latest member
max_pedreira

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