Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在VBA中调用WorksheetFunction.Average时的奇怪行为_Excel_Range_Vba - Fatal编程技术网

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

Excel 在VBA中调用WorksheetFunction.Average时的奇怪行为,excel,range,vba,Excel,Range,Vba,我有一艘潜艇: 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 colu

我有一艘潜艇:

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,而在工作表中调用的同一函数返回正确的平均值。有人知道为什么吗?此外,无论我选择哪个值范围,
子对象总是在MsgBox中返回0
无论我选择哪个工作表函数,返回值仍然是0


致以最诚挚的问候

avgReturn=Application.WorksheetFunction.Average(rng)
@simoco Nope仍然为0:/在“set rng=”行上停止,并检查n是什么-确保它选择了您期望的值。