1

I have an Excel worksheet with some strings in a column. Sometimes all of the entries are the same, and sometimes not:

enter image description here

enter image description here

I wrote a function to pass the range as a parameter:

=Dent_WG(A1:A6)

The VBA function should determine which case is true (all entries = "Al", or at least one entry = "Ag"), then return 0 or 12 respectively:

Function DentWG(WG_Mat As Range) As Single

    Dim dat As Variant, rw As Variant, temp As Single
    dat = WG_Mat
    temp = 0
    
    For rw = LBound(dat, 1) To UBound(dat, 1)
        If dat(rw, 1) = "Ag" Then
            temp = 12
        End If
    Next

    If temp = 12 Then
        DentWG = 12
    Else
        DentWG = 0
    End If

End Function

However, the function always returns 0, even for the 2nd case where "Ag" occurs in the range. I'm sure I'm failing to correctly convert the range into an array or correctly apply the intended logic to that array.

3 Answers 3

2

From your question...

The VBA function should determine which case is true (all entries = "Al", or at least one entry = "Ag"), then return 0 or 12 respectively:

This is what you need.

Function DentWG(WG_Mat As Range) As Long 
    Dim ClCount As Long

    ClCount = WG_Mat.Cells.Count

    If Application.WorksheetFunction.CountIf(WG_Mat, "Al") = ClCount Then
        DentWG = 0
    ElseIf Application.WorksheetFunction.CountIf(WG_Mat, "Ag") > 0 Then
        DentWG = 12
    End If
End Function

The same can be achieved using a formula

=IF(COUNTIF(A1:A6,"Al")=(ROWS(A1:A6)*COLUMNS(A1:A6)),0,IF(COUNTIF(A1:A6,"Ag") > 0‌​,12,""))

In case it will always be 1 Column then you don't need *COLUMNS(A1:A6). This will do.

=IF(COUNTIF(A1:A6,"Al")=ROWS(A1:A6),0,IF(COUNTIF(A1:A6,"Ag") > 0,12,""))

ScreenShot

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

@Siddharth Rout: +1 and check. Thanks for these two solutions. They both work fine for me. My own solution, which I have corrected above, also works. I see your solution invokes Application.WorksheetFunction. In my simple application, resources are not an issue, but generally speaking: Is it more resource intensive to invoke Application.WorksheetFunction within VBA code than to strictly stick with VBA methods?
@assylias: On the contrary, thanks for correcting my loop counter.
It's done - I'm only a casual vba-er and you can tell the difference with pros! ;-)
2

You don't really need a UDF for this. You could just say:

=IF(COUNTIF(A1:A6,"Ag")>=1,12,0)

6 Comments

+ 1 For a non VBA solution :) However you missed one part which case is true (all entries = "Al", or at least one entry = "Ag")
You still forgot about "Al" :)
@SiddharthRout Yes, but from his code I assumed that the OP assumed that the inputs were reliably "Ag" or "Al". Admittedly, that's a lot of assuming :-)
See the screenshots attached. and this part of the question The VBA function should determine which case is true (all entries = "Al", or at least one entry = "Ag"), then return 0 or 12 respectively:
I assumed that the OP assumed that the inputs were reliably "Ag" or "Al". Admittedly, that's a lot of assuming :-) BTW read the second sentence of the Question... Sometimes all of the entries are the same, and sometimes not :)
|
2

This works for me:

Function DentWG(WG_Mat As Range) As Single
    Dim result As Single, cl as Range
    result = 0

    For Each cl In WG_Mat
        If cl = "Ag" Then
            DentWG = 12
            Exit Function
        End If
    Next cl

    DentWG = result
End Function

3 Comments

This also doesn't answer the question.
@Alex P: does not work for me, perhaps because cl is not declared?
Exiting a function from within a loop...Really?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.