在VBA中调用WorksheetFunction.Average时出现奇怪的行为

时间:2014-04-03 08:21:34

标签: excel-vba range vba excel

我有一个子:

Sub TestrowSelectRetsRange()
  Dim lastrow As Long
    Dim i As Long
    Dim n As Integer
    Dim rng As Range
    Dim avgReturn As Long

    With Worksheets("RatiosCalc")
        For i = 354 To 1500
            'compare concatenation of columns L:Q with empty string
            If Trim(Join(Application.Transpose(Application.Transpose(.Range("A" & i & ":HU" & i).Value)), "")) = "" Then
                lastrow = i
                Exit For
            End If
        Next
    End With

    n = lastrow - 1
    'set range
    Set rng = Worksheets("RatiosCalc").Range("E354" & ":E" & n)
    'rng.Select
    avgReturn = Application.WorksheetFunction.Average(rng)
    MsgBox avgReturn
 End Sub

MsgBox返回0,而在工作表中调用的相同函数返回正确的平均值。有谁知道为什么?此外,我选择的值范围无关紧要the sub always returns 0 in MsgBoxand it does not matter which WorksheetFunction I choose, the return value is again 0

最好的问候

0 个答案:

没有答案