简体   繁体   English

调试:如果不是 IsError() 那么

[英]Debug: If Not IsError() Then

I've set up the macro below which compares a column of codes (Instrument), one row at a time, by searching another column in a different workbook.我已经设置了下面的宏,它通过搜索不同工作簿中的另一列来比较一列代码(仪器),一次一行。 If no code matches, the macro will alter a date in the first workbook by going back one day, which then changes the original code (in Instrument), and searches again.如果没有代码匹配,宏将通过返回一天来更改第一个工作簿中的日期,然后更改原始代码(在 Instrument 中),并再次搜索。 If a code is found, the macro should end by moving to the next row down:如果找到代码,则宏应通过向下移动到下一行来结束:

Sub YTM()

    Dim N As Long
    N = Cells(5, 24).End(xlDown).Row
    'N = 7
    Dim c As Range
    Dim x As Integer

    For Each c In Range("X2:X" & N)
        c.Select
        If IsError(c) Then
            For x = 1 To 14
                ActiveCell.Offset(0, -18).Value = ActiveCell.Offset(0, -18).Value - x/x
                If Not IsError(c) Then Exit For
            Next x
        End If
    Next c

End Sub

However, my VBA discontinues and wants me to debug the following code:但是,我的 VBA 停止运行并希望我调试以下代码:

If Not IsError(c) Then

I can't seem to work out why.我似乎无法弄清楚为什么。 I'm quite new to VBA, so I'm sure this issue is trivial.我对 VBA 很陌生,所以我确定这个问题很简单。 If further context is needed for clarity I am happy to oblige.如果为了清楚起见需要进一步的上下文,我很乐意提供帮助。 All help is appreciated, thank you!感谢所有帮助,谢谢!

A bit more explicit about using Value , and without any Select/Activate:关于使用Value更明确一点,没有任何选择/激活:

Sub YTM()


    Dim N As Long, c As Range, x As Long

    N = Cells(5, 24).End(xlDown).Row

    For Each c In Range("X2:X" & N).Cells
        If IsError(c.Value) Then
            For x = 1 To 14
                 c.Offset(0, -18).Value = c.Offset(0, -18).Value - 1
                 DoEvents 'allow for calculation...
                 If Not IsError(c.Value) Then Exit For
            Next x
        End If
     Next c

End Sub

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

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