简体   繁体   中英

Create VBA function based on user-defined function

Thanks to all friends who helped me on my question how to calculate specific cells in excel

Now, I need help to code that excel function in VBA The function is : =SUM(IFERROR(VALUE(IF(LEN(H27:Q27)=4,IF(ISNUMBER(SEARCH("b",LEFT(H27:Q27,2))),RIGHT(H27:Q27,1),LEFT(H27:Q27,1)),H27:Q27)),0))

Thanks in advance

I think this function implements the formula. It's very difficult to test without your original set of data in the cells. Note the function is called from the Foo sub-routine below - so you can pass in a variable range to the function. Hope that helps.

Function DoIt(rng As Range)
    ' VBA implementation for
    '=SUM(IFERROR(VALUE(IF(LEN(H27:Q27)=4,IF(ISNUMBER(SEARCH("b",LEFT(H27:Q27,2))),RIGHT(H27:Q27,1),LEFT(H27:Q27,1)),H27:Q27)),0))

    Dim dblResult As Double
    Dim rngCell As Range
    Dim intLength As Integer
    Dim strFragment1 As String
    Dim strFragment2 As String
    Dim intPos As Integer

    'set result
    dblResult = 0

    'loop for the array formula
    For Each rngCell In rngTarget

        'check value length = 4
        intLength = Len(rngCell.Value)
        If intLength = 4 Then
            'get bit of string and check for 'b' in string
            strFragment1 = Left(rngCell.Value, 2)
            'search for location of b in cell - use InStr for SEARCH
            intPos = InStr(1, strFragment, "b", vbBinaryCompare)
            If intPos <> 0 Then
                'b in fragment
                strFragment2 = Right(rngCell.Value, 1)
            Else
                'b not in fragment
                strFragment2 = Left(rngCell.Value, 1)
            End If

            '2nd fragment should be a number?  use IsNumeric for ISNUMBER and Val for VALUE
            If IsNumeric(strFragment2) Then
                dblResult = dblResult + Val(strResult)
            End If

        Else
            'cell value length <> 4
            'add cell value to result if is numeric - use IsNumeric for ISNUMBER and Val for VALUE
            If IsNumeric(rngCell.Value) Then
                dblResult = dblResult + Val(rngCell.Value)
            End If

        End If
    'next cell
    Next rngCell

    'return sum
    DoIt = dblResult

End Function

Sub Foo()
    Dim rngTarget As Range

    Set rng = Sheet1.Range("H27:Q27")

    Debug.Print DoIt(rng)

End Sub

Here you go:

Public Function GetTotal(rng As Range) As Long
    Dim tot As Long
    Dim celString As String
    Dim t1String As String, t2String As String

    For Each cel In rng
        If IsNumeric(cel) Then
            tot = tot + cel.Value
        ElseIf Len(cel.Value) = 4 Then
            celString = cel.Value
            t1String = Left(celString, 2)
            If InStr(1, t1String, "b") = 0 Then
                t2String = Left(celString, 1)
            Else
                t2String = Right(celString, 1)
            End If
            tot = tot + t2String
        End If
        Debug.Print tot
    Next
    GetTotal = tot
End Function

You have to give range as input.

See the image below:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM