简体   繁体   English

Function 在 VBA 但不在电子表格中运行

[英]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.我有一个我认为相对简单的 function 我正在运行的地方,我基本上是想找出有人在队列中等待回电的时间。 It runs great when I'm in the VBA tab, but when I call the function in my spreadsheet I get a #REF.当我在 VBA 选项卡中时,它运行得很好,但是当我在电子表格中调用 function 时,我得到一个#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一些 excel function 需要这个语法

 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.前两个 function 声明工作正常,但第三个不行。 The #REF error is produced by the fact that the TIQ2 function name is also a cell reference as @EvR says. #REF 错误是由于 TIQ2 function 名称也是@EvR 所说的单元格引用而产生的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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