Ignore blank cells on formula to join text

Maddy1234

New Member
Joined
Mar 9, 2018
Messages
24
Hi all,

I am using a UDF from njimack to achieve the following result in Excel 2013.

Column 1Column 2Column 3Formula result
1st2nd3rd1st, 2nd, 3rd
3rd3rd

<tbody>
</tbody>

This is his code.
Code:
[COLOR=#333333]Function Concatenate_Range(myrange As Range, Optional myDelimiter As String)[/COLOR]Dim Cell As Range


Application.Volatile


For Each Cell In myrange
    If Len(Cell.Value) > 0 Then
        Concatenate_Range = Concatenate_Range & Cell & myDelimiter
    Else: Concatenate_Range = Concatenate_Range
    End If
Next Cell


If Len(myDelimiter) > 0 Then Concatenate_Range = Left(Concatenate_Range, Len(Concatenate_Range) - Len(myDelimiter))

 [COLOR=#333333]End Function[/COLOR]

This gives a fantastic result. However I wanted to improve it a bit. Instead of getting the result as "1st, 2nd, 3rd" I want the last comma to change to a "&".

So the desired result would be this. "1st, 2nd & 3rd" OR "1st & 2nd". You get the gist.

Any help is extremely appreciated. Thank you guys.
 
Thank you for your kind reply Rick but as soon as I read your question I began to see the glaring problem in my application.

To answer your question, yes the cell B1 always starts with 1st but no, it will not always correspond to a Monday. For example 1st of March was Thursday. So for March 3rd and 4th will be weekends and should appear in Weekend OT Dates column.

Can you still solve this the way it is or will it be easier if we change the dates from just text to dates format.(dd/mm)

Your code works perfectly for the dummy data set though.

Assuming the answer to my above question is "yes", then give this completely revised function a try...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Function ConcatOT() As String
  Dim Cell As Range, OTrng As Range
  Const Delimiter = ", "
  Application.Volatile
  Set OTrng = Intersect(Application.Caller.EntireRow, IIf(Application.Caller.Column = 9, Columns("B:F"), Columns("G:H")))
  For Each Cell In OTrng
    If Cell.Value > 0 Then ConcatOT = ConcatOT & Delimiter & Intersect(Cell.EntireColumn, Rows(1))
  Next
  ConcatOT = Mid(ConcatOT, Len(Delimiter) + 1)
  If InStr(ConcatOT, Delimiter) Then
    ConcatOT = Application.Replace(ConcatOT, InStrRev(ConcatOT, Delimiter), Len(Delimiter), " and ")
  End If
End Function[/TD]
[/TR]
</tbody>[/TABLE]
Note that the function named has changed to ConcatOT and that it takes no arguments. Simply put this in cell I2...

=ConcatOT()

and copy it across to cell J2, then copy them both down to the end of your data.
 
Upvote 0

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.
Some questions...

Can I assume your headers do not stop at 7th then (that is, should I assume there are as many headers as a month has days)?

Where would I get the month and year at then (is that in cell A1 where you show what I thought was a header saying "Date")?
 
Upvote 0
Thanks a lot for your help Rick but I have found a solution in a different post. But I have a different problem now. If you have spare time please do have a look at it do provide some valuable insight.

Here's the link.

Assuming the answer to my above question is "yes", then give this completely revised function a try...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Function ConcatOT() As String
  Dim Cell As Range, OTrng As Range
  Const Delimiter = ", "
  Application.Volatile
  Set OTrng = Intersect(Application.Caller.EntireRow, IIf(Application.Caller.Column = 9, Columns("B:F"), Columns("G:H")))
  For Each Cell In OTrng
    If Cell.Value > 0 Then ConcatOT = ConcatOT & Delimiter & Intersect(Cell.EntireColumn, Rows(1))
  Next
  ConcatOT = Mid(ConcatOT, Len(Delimiter) + 1)
  If InStr(ConcatOT, Delimiter) Then
    ConcatOT = Application.Replace(ConcatOT, InStrRev(ConcatOT, Delimiter), Len(Delimiter), " and ")
  End If
End Function[/TD]
[/TR]
</tbody>[/TABLE]
Note that the function named has changed to ConcatOT and that it takes no arguments. Simply put this in cell I2...

=ConcatOT()

and copy it across to cell J2, then copy them both down to the end of your data.
 
Upvote 0
You are absolutely correct in that there are as many headers as many days in a month. Since this was a huge issue I changed the headers to date format and achieved the result. Here's the post.

Some questions...

Can I assume your headers do not stop at 7th then (that is, should I assume there are as many headers as a month has days)?

Where would I get the month and year at then (is that in cell A1 where you show what I thought was a header saying "Date")?
 
Upvote 0

Forum statistics

Threads
1,216,732
Messages
6,132,409
Members
449,726
Latest member
Skittlebeanz

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