Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-19-2024, 02:44 PM
Chris G Chris G is offline VBA Function to do the job of TEXTJOIN gives extra commas Windows 11 VBA Function to do the job of TEXTJOIN gives extra commas Office 2016
Novice
VBA Function to do the job of TEXTJOIN gives extra commas
 
Join Date: Oct 2023
Posts: 6
Chris G is on a distinguished road
Default VBA Function to do the job of TEXTJOIN gives extra commas

[Excel VERSION without the TEXTJOIN function]

I built a highly automated timesheet template. It uses a macro-enable template, plus a timesheet template, and has a custom tab on the ribbon with a few vital functions.

I have a column with joins the date and the number of hours taken in sick leave for a particular day, i.e on that row of data, and adds "hrs" to the number of hours.

An example of the combined values is:

0.25 hrs 25-12-23 [Non-USA date format]

If there is more than one date on which the person records sick leave for the two week pay period, TEXTJOIN would easily concatenate the values in this column at the bottom of the column, and delimit each with a comma.

Because the user does not have TEXTJOIN in their version of Excel, I use the following Function in the macro-enabled template to do the same job:

Code:
Function TEXTJOINED(delimiter As String, ignore_empty As Boolean, mg As Range) As String
' https://www.youtube.com/watch?v=sWeXtPB7y9o
Dim compiled As String

    For Each cell In mg
        If ignore_empty And IsEmpty(cell.value) Then
        'nothing
    
        Else
            compiled = compiled + IIf(compiled = "", "", delimiter) + CStr(cell.value)
        End If
    Next

    TEXTJOINED = compiled

End Function
In the template I added

=TEXTJOINED(", ",TRUE,V9:V24)



to give me the concatenation of values in the column. (This data is then transferred to another automated worksheet).


This gives me almost what I want, but adds a string of commas every time:

0.25 hrs 25-12-23, 0.5 hrs 26-12-23, , , , , , , , , , ,

Not having worked with Excel VBA, any tips on how to exclude those commas would be most welcome.

Thanks.
Reply With Quote
  #2  
Old 12-20-2024, 03:00 AM
p45cal's Avatar
p45cal p45cal is offline VBA Function to do the job of TEXTJOIN gives extra commas Windows 10 VBA Function to do the job of TEXTJOIN gives extra commas Office 2021
Expert
 
Join Date: Apr 2014
Posts: 947
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

Probably because the cells which appear blank are not in fact balnk. Check for a space or anything else in those cells.
If you have a formula in those blank cells which may result in the cell containing an empty string (eg. =IF(A1>10,10,"") ) then commas will show up.
Instead of checking for Empty, check the length of the cell contents. If at the same time you also want to exclude cells which have only a space or spaces in, then change the line:
Code:
If ignore_empty And IsEmpty(cell.Value) Then
to:
Code:
If ignore_empty And Len(Application.Trim(cell.Value)) = 0 Then
Reply With Quote
  #3  
Old 12-20-2024, 05:35 PM
Chris G Chris G is offline VBA Function to do the job of TEXTJOIN gives extra commas Windows 11 VBA Function to do the job of TEXTJOIN gives extra commas Office 2016
Novice
VBA Function to do the job of TEXTJOIN gives extra commas
 
Join Date: Oct 2023
Posts: 6
Chris G is on a distinguished road
Default

Thanks p45cal, that is the solution. There is indeed a formula in each of the cells being concatenated, e.g. =IF(O20<>0,O20&" hrs " &TEXT(B20,"d-mm-yy"),""), so they look empty when there's no data to display.

So thank you very much indeed for the solution!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to identify and fix long sentences without commas Bunnelope Word VBA 12 03-29-2023 01:33 PM
Lookup and Textjoin help ganesang Excel 19 09-02-2021 12:48 AM
VBA Function to do the job of TEXTJOIN gives extra commas deleting blanks and commas FUGMAN Excel Programming 49 03-20-2017 10:17 AM
Commas within fields in .csv files gar Excel 1 01-29-2015 09:24 PM
VBA Function to do the job of TEXTJOIN gives extra commas No inverted commas in word 2013. tonycrossley Word 3 01-27-2014 04:55 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:53 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft