Creating a Function

G

Guest

If A1 > 0 AND SUM(A2:A12) = 0 then A13 = Jan
If A2 > 0 AND SUM(A3:A12) = 0 then A13 = Feb
If A3 > 0 AND SUM(A4:A12) = 0 then A13 = March
etc.etc.

Rather than just using nested Ifs which is cumbersome I am trying:

Function Month(Integer)

Select Case Integer
Case a1>0,sum(a2:a12)=0
Month = "Jan"

etc....

help is much appreciated - thanks
 
S

Seiya

try
Function mmm(rng As Range) As String
Dim numAry, monthAry, i As Long
numAry = Array(1, 2, 3): monthAry = Array("Jan", "Feb", "Mar")
For i = LBound(numAry) To UBound(numAry)
If rng(numAry(i), 1) <> "" And _
Application.Sum(rng.Resize(rng.Rows.Count -
numAry(i)).Offset(numAry(i))) = 0 Then
mmm = monthAry(i): Exit For
End If
Next
Erase numAry, monthAry
End Function

use like

=mmm(A1:A12)
 
H

Harald Staff

Hi

Try this little thing in A13:

=TEXT(DATE(1,SUMPRODUCT(MAX(ROW(A1:A12)*(A1:A12>0))),1),"MMM")

HTH. Best wishes Harald
 
G

Guest

Harald - thats perfect
can you explain to me what the function below does - very grateful,
I understand each individual function but not working together

SUMPRODUCT(MAX(ROW(A1:A12)*(A1:A12>0)))
 
H

Harald Staff

Hi

An array formula like that is like a package of individual formulas. These
formulas are
Row(A1)*(A1>0) ' returns 1 if A1>0, else 0
Row(A2)*(A2>0) ' returns 2 if A3>0, else 0
Row(A3)*(A3>0) 'and so on
Row(A4)*(A4>0)
....
MAX searches the highest of these results, and SUMPRODUCT wraps it up as an
array formula.

Then we use this result as month number in a date, and we convert that date
to text monthname. I guess you found out already, so just for the
archive....

Best wishes Harald
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top