Loop non contingnius cells with vba function

tryagain

Board Regular
Joined
Mar 15, 2010
Messages
102
This function is only a cimple eks. in real world it count specifik char's in specifik cells

Function test(rng)
For Each c In rng
test = test & c
Next
End Function

The function works ok when i do =test(A2:A9)

but if i want to mark non contingnius cells like =test(A2;A4;A6)
using mouse and CTRL together the code fails Why ?

OBS: i am selecting cells with mouse like =test( and then select cpecifik cells )
and the semicolons in eksamble is sepperators in DK-Excel
i tryed komma and column - same result

thanks in advance
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Putting the separators like that makes the function think that you are passing multiple parameters, and it only has one defined. You have 2 options:

First, you can use your function as is, but when you enter the function on your worksheet, enter it like this:
=test(( ... then click on your cells ... ))
The extra parentheses tell it that the whole range is one parameter.

Second, you can use a paramarray in the function to accept a variable number of parameters:

Code:
Public Function test1(ParamArray rng())
Dim c As Range, i As Long


    For i = LBound(rng) To UBound(rng)
        For Each c In rng(i)
            test1 = test1 & c
        Next c
    Next i
    
End Function
Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,979
Messages
6,175,760
Members
452,668
Latest member
mrider123

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