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
ABC
1 a
2 c
3 b
4
5

<tbody>
</tbody>

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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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,215,992
Messages
6,128,170
Members
449,429
Latest member
ianharper68

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