VBA: How to reference variables using the TextJoin formula?

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
Hello,

How could I reference Variables properly using VBA? Let's say I have a Variable named "Length" and I wanted to use the TextJoin formula within VBA to reference the variables "Length" and the variable "i". How could I do it?

I played around with a few variations but could not figure it out. This is what I have so far, which is wrong:

VBA Code:
"=TEXTJOIN(""-"",,  & length &  , & Range("A" & i) & )"

Any help would be greatly appreciated.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Why not just use the concatenation operator: &.

VBA Code:
myText = length & "-" & Range("A" & i)

If you need to use the TEXTJOIN function for some reason (it's a worksheet function not a VBA function, Join is the VBA function) you need to pass it an array of text to join as below. Note the test sub below overwrites cell A1 so run it in a test worksheet.

VBA Code:
Sub test()
    Dim myText As String, length, i As Long, arr
    i = 1
    length = 33
    [A1] = "meters"
    myText = length & "-" & Range("A" & i)
    MsgBox "using concatenation operator ""&"": length & ""-"" & Range(""A"" & i)" & Chr(10) & Chr(10) & myText
    
    arr = Array(length, Range("A" & i))
    myText = Join(arr, "-")
    MsgBox "using VBA function: Join(arr, ""-"")" & Chr(10) & Chr(10) & myText
    
    myText = Application.TextJoin("-", , arr)
    MsgBox "using worksheet function: Application.TEXTJOIN(arr, ,""-"")" & Chr(10) & Chr(10) & myText
End Sub
 
Upvote 0
Solution
Why not just use the concatenation operator: &.

VBA Code:
myText = length & "-" & Range("A" & i)

If you need to use the TEXTJOIN function for some reason (it's a worksheet function not a VBA function, Join is the VBA function) you need to pass it an array of text to join as below. Note the test sub below overwrites cell A1 so run it in a test worksheet.

VBA Code:
Sub test()
    Dim myText As String, length, i As Long, arr
    i = 1
    length = 33
    [A1] = "meters"
    myText = length & "-" & Range("A" & i)
    MsgBox "using concatenation operator ""&"": length & ""-"" & Range(""A"" & i)" & Chr(10) & Chr(10) & myText
  
    arr = Array(length, Range("A" & i))
    myText = Join(arr, "-")
    MsgBox "using VBA function: Join(arr, ""-"")" & Chr(10) & Chr(10) & myText
  
    myText = Application.TextJoin("-", , arr)
    MsgBox "using worksheet function: Application.TEXTJOIN(arr, ,""-"")" & Chr(10) & Chr(10) & myText
End Sub
Perfect!

It makes more sense to use the concatenation operator. I was essentially just trying to join a variable with a cell reference. Admittedly I always just use the macro recorder to get the VBA equivalent but it always uses FormulaR1C1 etc.

Thank you for the workaround! Much better and easier to use.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,106
Messages
6,123,123
Members
449,096
Latest member
provoking

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