TEXTJOIN with IF Statement VBA

Jean Lewis

New Member
Joined
Apr 28, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have Excel 2016 so I don't have the TEXTJOIN function automatically. Thanks to this article, I have been able to recreate it (function is named my_text_join) through VBA: and it works great if I am only doing the my_text_join function (=my_text_join(";",TRUE,RANGE); however, I need to do something a little extra, which is to join a a specific row of cells (in this case, header cells) only if different/specific cells are not empty. Below is an example of the outcome for this formula - as you can see, it joined the header text 1, 2 and 4 since only the cells below that header had text.

1651174029265.png

I have been able to do this successfully in EXCEL 2019 with this formula =TEXTJOIN(";",TRUE,IF(A5:D5<>"",A$1:D$1,"")) to only join header cells when the reference cell is populated, but I can't get it to work in the Excel 2016 version where I don't have access to TEXTJOIN and have to use the VBA workaround =My_Text_Join(";",TRUE,IF(A2:D5<>"",A$1:D$1,"")).

I'm not sure if the fix is an alteration to the VBA code or to the formula. Below is the VBA code I used, provided by Advanced Excel and VBA tutorials. Any help would be appreciated as I'm currently sending files back and forth between my work laptop and personal laptop since my work laptop doesn't have Excel 2019/365).

Option Explicit

Function My_Text_Join(delimiter As String, ignore_empty As Boolean, text_range As Range) As String

Application.Volatile
Dim c As Range
Dim n As Long
n = 0
For Each c In text_range
If ignore_empty = True Then
If VBA.IsEmpty(c.Value) = False Then
If n = 0 Then
My_Text_Join = c.Value
Else
My_Text_Join = My_Text_Join & delimiter & c.Value
End If
n = n + 1
End If

Else

If n = 0 Then
My_Text_Join = c.Value
Else
My_Text_Join = My_Text_Join & delimiter & c.Value
End If
n = n + 1

End If
Next

End Function



Thank you!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hello,

I suggest to use my UDF Textjoin:
 
Upvote 0
@Sulprobil Thank you for your reply. I pasted that VBA into a new module but I don't really understand how to use it the way I need. I attached a minisheet showing what I am trying to do with the original VBA I was using. Again, this formula works with the standard TEXTJOIN function in Excel 2019, but not the one I created with VBA.

Book1.xlsx
ABCDEFG
1Header 1Header 2Header 3Header 4Header 5TEXTJOIN with IF StatementCorrect Value
2YesYesYes#NAME?Header 1; Header 3; Header 4
3YesYesYes#NAME?Header 2; Header 4; Header 5
4YesYesYes#NAME?Header 1; Header 2; Header 3
Sheet1
Cell Formulas
RangeFormula
F2:F4F2=My_Text_Join(";",TRUE,IF(A2:E2<>"",A$1:E$1,""))
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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