简体   繁体   English

Excel VBA出现错误错误

[英]Excel VBA On Error error

I am rather new at programming, and while learning Python also started experimenting with Excel VBA. 我对编程很陌生,在学习Python的同时也开始尝试Excel VBA。 I have an issue with the last one. 我对最后一个有疑问。

I have some large Excel sheets and tried to validate that data in specific columns matches data on another sheet in certain columns as they will be supposed to relate to each other by these values (and will be connected by a third value). 我有一些大型Excel工作表,并试图验证特定列中的数据是否与某些列中另一工作表上的数据匹配,因为它们应该通过这些值相互关联(并通过第三个值进行连接)。 To make this a bit more difficult, both of these columns may contain more than one value separated by "|". 为了使这一点更加困难,这两个列都可能包含多个用“ |”分隔的值。 So, I have split these values in a list and I try to iterate through them to make sure all these values are set correctly, the connection will work fine. 因此,我将这些值拆分为一个列表,并尝试遍历它们以确保所有这些值均已正确设置,连接将正常工作。

All is fine as long as all is fine :) I have however an issue where there are two values in one of those columns and only one in the other. 只要一切都好,一切都很好:)但是,我有一个问题,其中一个列中有两个值,而另一列中只有一个。 I would like this discrepancy to be noted on a sheet and then proceed to the next item. 我希望将此差异记录在纸上,然后继续进行下一项。

The way that seemed to be applicable for me is to use "On Error GoTo ErrHandler", then note error on another sheet, and then user Resume to proceed. 似乎适用于我的方法是使用“ On Error GoTo ErrHandler”,然后在另一张纸上记录错误,然后再使用Resume继续。

Here is what I came up with: 这是我想出的:

            For h = 0 To UBound(Split1())
                 For j = 1 To GetMaxRow("SpecificSheet", A)
                     On Error GoTo ErrHandler:
                        If Sheets("SpecificSheet").Cells(j, 1).Value = Split1(h) And Sheets("SpecificSheet").Cells(j, 2).Value = Split2(h) Then
                                DependencyOk = DependencyOk + 1
                        End If
                    Next j
            Next h
ErrProceed:

Also ErrHandler is: ErrHandler也是:

ErrHandler:
        Sheets("Issues").Cells(x, 1) = "IssueDescription"
    GoTo ErrProceed

It stops at line 2 with Subscript out of range for Split2(h) rather than moving on to ErrHandler and then ErrProceed. 它停止在第2行,且下标超出了Split2(h)的范围,而不是继续到ErrHandler然后进入ErrProceed。 I have the feeling this must be something very obvious but I am just unable to get this working, and I am not able to find other way (like a try/except) in Excel VBA. 我觉得这一定很明显,但是我无法使它正常工作,而且我无法在Excel VBA中找到其他方式(例如try / except)。

UPDATE: 更新:

Trying to clarify things a bit. 试图澄清一些事情。 The root of the issue is, that the Split2 list is shorter than Split1 - which is an issue with the input data and I'd like to capture this. 问题的根源在于,Split2列表比Split1短-这是输入数据的问题,我想抓住这一点。 I get the Split values from cells, where the values are separated by "|" 我从单元格中获取拆分值,其中值之间用“ |”分隔 characters: 特点:

    CellValue = Sheets("SomeSheet").Cells(RowNumber, ColumNumber)
    CellValueSplit() = Split(CellValue, "|")

And then iterate as: 然后迭代为:

   For h = 0 To UBound(Split1())

So as Split1 moves on to the for example 3rd value, Split2 throws error and script stops. 因此,当Split1移至例如第3个值时,Split2引发错误,脚本停止。 The best I was able to do so far was, that I let it proceed with the loop, but as this is a rather large sheet, it will fill the same error report ca. 到目前为止,我能做的最好的事情就是让它继续循环,但是由于这是一个相当大的工作表,它将填充相同的错误报告ca。 200k times in this case, which I'd like to avoid. 在这种情况下,请避免20万次。 So I'd prefer it to proceed from after this loop once it hits out of range error, and proceed examining the next value. 因此,我希望它从超出此范围错误后的循环开始,并继续检查下一个值。

Thank you for your help so far and in advance! 谢谢您到目前为止的帮助!

You have an issue with your syntax. 您的语法有问题。 The proper Error statement syntax is: 正确的Error语句语法为:

On Error GoTo <string>
On Error Resume Next
On Error GoTo 0

When using On Error GoTo <string> there is no ":" at the end. 使用On Error GoTo <string>时,末尾没有“:”。 The ":" doesn't come into play until you create the target location. 在创建目标位置之前,“:”不起作用。 Example: 例:

On Error GoTo Here
'// ---- Do something ---- //
Here:
'// ---- Handle the error ---- //

If you use On Error Resume Next , then you're telling the machine to ignore errors and proceed on to the next line of code. 如果您使用On Error Resume Next ,那么您是在告诉机器忽略错误并继续执行下一行代码。

When you use On Error Return To 0 , VBA will reset its error handling back to default. 当您使用On Error Return To 0 ,VBA会将其错误处理重置为默认值。 It's a good habit when using On Error Resume Next to insert On Error Return To 0 as soon as you no longer need it. On Error Resume Next不再需要时使用On Error Resume Next插入On Error Return To 0时,这是一个好习惯。 On Error Resume Next has a real potential to break your code and make it behave strangely. On Error Resume Next上,很有可能破坏您的代码并使其表现异常。 Not to mention debugging can be a real nightmare. 更不用说调试可能是一个真正的噩梦。 Check out the VBA manual from Microsoft for a more detailed explanation. 请查阅MicrosoftVBA手册,以获取更详细的说明。

Finally, if your question is answered, you should mark it as answered. 最后,如果您的问题得到回答,则应将其标记为已回答。

The short and quick version is that VBA Error Handling Routine's only handle errors in the actual code execution, they do not fire when conditions expressed by the code are not met. 简短而快速的版本是VBA错误处理例程仅处理实际代码执行中的错误,当不满足代码表示的条件时,它们不会触发。

In your case, you do not need any error handling at all. 就您而言,您根本不需要任何错误处理。 In most cases it is actually best to avoid On Error GoTo ... . 在大多数情况下,实际上最好避免On Error GoTo ... There are cases where it's inevitable, but they are rare. 在某些情况下,这是不可避免的,但很少见。

Try this IF THEN ELSE block: 尝试以下IF THEN ELSE块:

If Sheets("SpecificSheet").Cells(j, 1).Value = Split1(h) And Sheets("SpecificSheet").Cells(j, 2).Value = Split2(h) Then
    DependencyOk = DependencyOk + 1
Else
    Sheets("Issues").Cells(x, 1) = "IssueDescription"
End If

Actually I have just found the issue. 其实我才发现问题。 It was caused by a ":" left after an If statement a few rows earlier. 它是由前面几行的If语句后留下的“:”引起的。 I still don't really understand what it did, but I suggest not to reproduce it :) 我仍然不太了解它的作用,但是我建议不要重现它:)

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

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