简体   繁体   中英

Function runs in VBA but not the spreadsheet

I have what I think is a relatively simple function I'm running where I'm basically trying to find how much time someone stays waiting in the queue for a call back. It runs great when I'm in the VBA tab, but when I call the function in my spreadsheet I get a #REF. error.

    Function TIQ2()

    Dim time, count, i As Integer
    Dim TIQ
    time = 0
    count = 0

    NumRows = Sheet1.Range("A2", Sheet1.Range("A2").End(xlDown)).Rows.count + 1

    For i = 2 To NumRows
        If Sheet1.Range("c" & i) = "no" Or Sheet1.Range("c" & i) = "No" Then
    
            If Sheet1.Range("d" & i) = "No" Or Sheet1.Range("d" & i) = "no" Then
            time = time + Left(Sheet1.Range("g" & i), 2)
            count = count + 1
            Debug.Print time, count
        End If
    End If
  Next

  TIQ2 = WorksheetFunction.RoundUp(time / count, 0) & " minutes"
  Debug.Print TIQ2

End Function

try changing this line. some excel function requieres this syntaxis

 TIQ2 = WorksheetFunction.RoundUp(time / count, 0) & " minutes"

'-------------------
 TIQ2 =ThisWorkbook.Application.WorksheetFunction.RoundUp(time / count, 0) & " minutes"
Option Explicit

'Public Function fnHelloWorld()
'Function fnTIQ()
Function TIQ2()
    MsgBox "Hello World!"
End Function

Sub Test()
    Call TIQ2
End Sub

Try this...

The first two function declarations work OK but the third one does not. The #REF error is produced by the fact that the TIQ2 function name is also a cell reference as @EvR says.

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