VBA Function

yesterdays

Active Member
Joined
Oct 8, 2014
Messages
337
I'm using below code for joining multiple range(s)'s value to form a string, it work fine,
But now i need to use other formula to form the range inside it, it doesn't work anymore.
please see sample below
[TABLE="width: 256"]
<tbody>[TR]
[TD="class: xl66, width: 64"][/TD]
[TD="class: xl66, width: 64"]A[/TD]
[TD="class: xl66, width: 64"]B[/TD]
[TD="class: xl66, width: 64"]C[/TD]
[/TR]
[TR]
[TD="class: xl66"]1[/TD]
[TD="class: xl66"] a[/TD]
[TD="class: xl66"][/TD]
[TD="class: xl66"][/TD]
[/TR]
[TR]
[TD="class: xl66"]2[/TD]
[TD="class: xl66"] c[/TD]
[TD="class: xl66"][/TD]
[TD="class: xl66"][/TD]
[/TR]
[TR]
[TD="class: xl66"]3[/TD]
[TD="class: xl66"] b[/TD]
[TD="class: xl66"][/TD]
[TD="class: xl66"][/TD]
[/TR]
[TR]
[TD="class: xl66"]4[/TD]
[TD="class: xl66"][/TD]
[TD="class: xl66"][/TD]
[TD="class: xl66"][/TD]
[/TR]
[TR]
[TD="class: xl66"]5[/TD]
[TD="class: xl66"][/TD]
[TD="class: xl66"][/TD]
[TD="class: xl66"][/TD]
[/TR]
</tbody>[/TABLE]

join(", ",A1:A3) will return a, c, b
but when i add a condition to the range, like <>a , it wont work anymore
the result expected is c, b

I know it like the arguments used in Max WorksheetFunction.Max method but can't find the information for it to work:
"Arguments can either be numbers or names, arrays, or references that contain numbers."

Code:
Public Function join(d As String, ParamArray r() As Variant) As String
    Dim s As String, c As Range
    Dim elem As Range
    Dim i As Long
    For i = LBound(r) To UBound(r)
        For Each elem In r(i)
            For Each c In elem
            If CBool(Len(c.Text)) Then _
            s = s & IIf(Len(s), d, vbNullString) & c.Value
            Next c
        Next elem
    Next i
    join = s
End Function

p/s please pardon my English skill.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
What do you mean by 'add a condition'?

Where/how/why are you doing that?
 
Upvote 0
for example, my formula would be
=join(", ",if(A1:A3<>"a",A1:A3))
and then ctrl shift enter
the reason is i want to just join field that satisfied some conditions.
 
Upvote 0
When passed to the function this,

IF(A1:A3<>"a",A1:A3)

isn't passed select a simple array, it's passed select an array of arrays, of event an array of an array of arrays - couldn't quite figure that out.

Anyway, whatever it is it stops the function working.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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