为什么某些宏在一夜之间停止工作?

问题描述 投票:0回答:1

多年来,我一直在使用这个宏和其他几个完全相同的宏,只是细胞数量不同。今天我收到“运行时错误 13 不匹配”并且无法弄清楚原因。我没有改变任何东西。

Sub PayALL_ByDate1()

Dim oRng    As Range
Dim i       As Long
Dim dblRes  As Double

Set oRng = Range("C2:C69")
For i = 1 To oRng.Cells.Count
    If oRng(i).Font.ColorIndex = 1 Or oRng(i).Font.ColorIndex = xlColorIndexAutomatic And CDbl(oRng(i).Offset(0, 1)) <= CDbl(Range("D96")) Then
        If InStr(oRng(i).Offset(0, 3), ChrW(8730)) = 0 And CDbl(oRng(i).Offset(0, 1)) <= CDbl(Range("D96")) Then
            dblRes = dblRes + oRng(i)
        End If
    End If
Next i
Application.EnableEvents = False
Range("D89") = dblRes
Application.EnableEvents = True

我曾尝试从过去的工作簿中剪切和粘贴,希望能奏效,但没有成功。不知道该怎么做,因为它已经工作了多年而没有变化。

excel vba
1个回答
0
投票

将测试拆分为更多行,并在进行任何(例如)数字比较之前检查单元格值

Sub PayALL_ByDate1()

    Dim i As Long, c As Range, ws As Worksheet, D96, ci, v
    Dim dblRes  As Double

    Set ws = activesheet
    D96 = CDbl(ws.range("D96").value)

    For Each c in ws.Range("C2:C69").Cells
        ci = c.font.colorindex
        If ci = 1 Or ci = xlColorIndexAutomatic Then
            v = c.offset(0, 1).value
            If isnumeric(v) Then
                If v <= D96 Then
                    If InStr(c.Offset(0, 3), ChrW(8730)) = 0 Then
                        dblRes = dblRes + c.Value
                    End If   'Instr
                End if       '<=D96
            End If           'numeric
        End If               'colorindex
    Next c
    
    Application.EnableEvents = False
    ws.Range("D89") = dblRes
    Application.EnableEvents = True

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.