ytayta555- you left a comment on my blog, NewtonExcelBach (http://
newtonexcelbach.wordpress.com/), I'll reply here.
A few small changes may do what you want
I didn't expect to get the answer from you youself sir Doug
Jenkins , I am very pleasantly surprised
First I'm assuming that ; is the correct array separator for your
region. For here (Australia) it's a comma.
I am not wrong here , in european sintax array separator
is ; and for array formula is |
Second it might be better to rename the variable "myrow" to "myrange",
since the array Target contains a number of ranges.
Then change: For myrow = 0 To UBound(Target())
to: For myrange = 0 To UBound(Target())
and change: WorksheetFunction.Count(Target(1).Rows(RowCount))
to: WorksheetFunction.Count(Target(myrange).Rows(RowCount))
and change: Next myrow to Next myrange
I made this changes , and it is a big improvement ! Now , UDF
recognise all targets .
In some few minutes we can work in range B1:G20 ,
to see and be shure on results ;
we shall use for this example the next macro :
Function CountUDF(ParamArray Target() As Variant)
CountUDF = True
MyCount = 0
For myranges = 0 To UBound(Target())
For ColCount = 1 To Target(myranges).Columns.Count
MyCount = MyCount + _
WorksheetFunction.Count(Target(myranges).Columns(ColCount))
If MyCount > 1 Then
CountUDF = False
Exit Function
End If
Next ColCount
Next myranges
End Function
In cell I8 we put the UDF : =CountUDF(B1:G10;B14:G16;B18:G20)
If we put a number in cell B1 and then in B7 , will return False =
good result !
The same thing if we put the number in B15 or B 19 instead of B7 .
BUT , if we have a number in cell B1 and we put a number in cell C19 ,
or
C15 , the UDF will return False , what is wrong .
If we have in all range we work (B1:G20) in cell B1 a number and in
cell D19 another number, the right
result for me is TRUE , because there are not 2 numbers in column
B !!
Actually , UDF don't recognise to count every column separately (to
count the same column in this
three ranges like is a one range ( to recognise column
B1:B10;B14:B16;B18:B20 like a *single* range ).
- - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - --
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
[ An example what I need : The worksheet formula look so in this
example : |
=AND(COUNT(B1:B10;B14:B16;B18:B20)<=1;COUNT(C1:C10;C14:C16;C18:C20)<=1;COUNT
|
(D1
10;D14
16;D18
20)<=1;.....E...........F.............COUNT(G1:G10;G14:G16;G18:G20)<=1)
|
Here we can see that the worksheet formula count in every column
separate ] |
- - - - - - - - - - -- - - - - - - - - - - - -- -- - - - - - - --
- - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
This will then:
loop through each range
I need to loop through each column in each range , such as
it is a only column in range B1:B10;B14:B16;B18:B20
but , actually , it count like in three columns (ranges) separate !...
The problem is that this UDF actually count in 3 ranges , not
like in one range , each Column must be recognised like an
only range .
in each range loop through each row
for each row count the number of cells that are not empty
I need to loop and count in every column , not row
My last problem to fix is what I just tried to explain
For this application it may well be quicker to use
worksheetfunction.count than to try and do it entirely in VBA. To do
it in VBA you could:
Convert each range to a variant array:
range_array = Target(myrange).value2
Then loop through each row and each column of the array, and count non-
empty cells. But it may not be any quicker.
Here , I'm totally lose , I need like a beginner all code , but it
doesn't matter
so much , I can not to ask you more , I am very pleased to fix this my
last
problem ( to count in every column separate but not how they are 3
ranges
separate ).
Finally with the code as it is, I don't see any purpose in looping
through each row. Why not simply do the count on each range?
I'll have not just one UDF function , I'll have millions of this kind
of UDF ,
that's why I am looking for fastest speed , an improvement of 1
second
per workbook is very much for me ; with array worksheet formula it
take
60 seconds per workbook , with this UDF can take and 10 seconds
or more slightly , because worksheet formula doesn't have :
,, If MyCount > 1 Then
CountUDF = False
Exit Function ,, ............> Joelwrote ...First, you should put back the "Exit function" to speed up the code. Once
you find one row that is greater than 1 you don't need to test all the
other rows.
I tried in many ways , it is the best and fastest kind of querry in my
kind of database .