Excel 2007,VBA:零售 6.5.1057 -- Application.WorksheetFunction.Average()

Excel 2007, VBA: Retail 6.5.1057 -- Application.WorksheetFunction.Average()

抱歉,格式设置与我的浏览器不兼容。

版本 Windows 10 专业版

版本 21H2 OS 内部版本 19044.1586 Excel 2007,VBA:零售 6.5.1057 - Forms3:12.0.6723.500

关注点:

Sheets("SUMMARY").Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) = _
                        Application.WorksheetFunction.Average(irng)

当使用上面的代码时'manually' (F5),在下面的模块中,每次都能完美运行。但是,当 'timer' triggers/engages 时,程序 breaks/stops 死在了那一行。

我不知道如何解决这个问题。我已将代码分散到多个过程中以进行故障排除;对我的事业一点帮助都没有。

恭敬地。

    Option Explicit
    Public RunWhen As Double
    Public Const cRunWhat = "kcal"  ' the name of the procedure to run
    Sub StartTimer()
    RunWhen = TimeSerial(23, 45, 0)
    Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
        Schedule:=True
    End Sub

    Private Sub kcal()
    Sheets("SUMMARY").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = Date
    Sheets("SUMMARY").Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Value = Sheets("TODAY_(24hr)").Range("E40").Value

    kcal2
    End Sub

    Sub kcal2()
    Dim i As String
    Dim irng As Range
    i = Sheets("SUMMARY").Cells(Rows.Count, 2).End(xlUp).Address
    Set irng = Range("B2:" & i)

    Sheets("SUMMARY").Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) = Application.WorksheetFunction.Average(irng)
 
    ActiveWorkbook.SaveCopyAs Filename:=ActiveWorkbook.Path & "\Back_Up\Bak-Up_" & Format(Now, "yyyymmdd") & "_m" & ActiveWorkbook.Name

    StartTimer


    End Sub

试试这个:

Sub kcal2()
    
    Dim irng As Range
    
    With Sheets("SUMMARY")  'make sure all ranges are tied to a specific sheet
         Set irng = .Range("B2:B" & .Cells(.Rows.Count, 2).End(xlUp).Row)
         .Cells(.Rows.Count, 3).End(xlUp).Offset(1, 0) = _
                        Application.WorksheetFunction.Average(irng)
    End With
    ActiveWorkbook.SaveCopyAs Filename:=ActiveWorkbook.Path & _
        "\Back_Up\Bak-Up_" & Format(Now, "yyyymmdd") & "_m" & ActiveWorkbook.Name

    StartTimer

End Sub

Application.OnTime

  • 如果在包含此代码的工作簿中不应发生这种情况,请将所有出现的 ThisWorkbook 替换为正确的工作簿,例如ActiveWorkbook.
Option Explicit

Public RunWhen As Double
Public Const cRunWhat = "kcal"  ' the name of the procedure to run

Sub StartTimer()
    RunWhen = TimeSerial(23, 45, 0)
    Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
        Schedule:=True
End Sub

Private Sub kcal()
    Dim sws As Worksheet: Set sws = ThisWorkbook.Worksheets("TODAY_(24hr)")
    Dim dws As Worksheet: Set dws = ThisWorkbook.Worksheets("SUMMARY")
    dws.Cells(dws.Rows.Count, "A").End(xlUp).Offset(1, 0).Value = Date
    dws.Cells(dws.Rows.Count, "B").End(xlUp).Offset(1, 0).Value _
        = sws.Range("E40").Value
    kcal2
End Sub

Sub kcal2()
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("SUMMARY")
    Dim lRow As Long: lRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    Dim irng As Range: Set irng = ws.Range("B2:B" & lRow)

    ws.Cells(ws.Rows.Count, "C").End(xlUp).Offset(1, 0).Value _
        = Application.WorksheetFunction.Aggregate(1, 6, irng)
    
    Dim FolderPath As String: FolderPath = ThisWorkbook.Path & "\Back_Up\"
    If Len(Dir(FolderPath, vbDirectory)) = 0 Then MkDir FolderPath
    
    ThisWorkbook.SaveCopyAs Filename:=FolderPath & "Bak-Up_" _
        & Format(Now, "yyyymmdd") & "_m" & ThisWorkbook.Name

    StartTimer

End Sub

原始 'posted\broken' 代码的实际修复是更改 --

Set irng = Range("B2:" & i)

-- 至 --

 Set irng = Sheets("SUMMARY").Range("B2:" & i)    

While\When使用错误行

Set irng = Range("B2:" & i)    

如果和当 ActiveSheet -- 是任何其他 sheet 而不是

Sheets("SUMMARY")    

project/procedure 会尝试申请

Sheets("SUMMARY").Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) = Application.WorksheetFunction.Average(irng)   

到那个 ActiveSheet,而不是

Sheets("SUMMARY")    

当 ActiveSheet 不是 Sheets("SUMMARY") 时,在指定的 [irng] 中没有 [Average()] 可以处理的数据。 <-- 这就是代码失败的原因。通过更改

Set irng = Sheets("SUMMARY").Range("B2:" & i)    

这是在指定 'exactly' sheet [ irng ] 应该 [ 设置 ] 到。

Set irng = Range("B2:" & i)    

没有完成。

感谢大家的贡献。