Take the code below; I am missing an End With
however when it is compiled the error is: Next without For
which is incorrect.
拿下面的代码;我错过了一个结束但是当它被编译时,错误是:下一个没有For是不正确的。
Option Explicit
Public Sub Payments()
Dim ws As Worksheet
'--> Format the original workbook
For Each ws In ThisWorkbook.Worksheets
With ws
.UsedRange.EntireColumn.Hidden = False
Next
End Sub
I have seen this behaviour with other errors too where the problem is not communicated properly. Why is this?
我已经看到这种行为与其他错误,如果问题没有正确传达。为什么是这样?
3 个解决方案
#1
8
The error is not incorrect. From the interpreter's point of view, the first problem that it encounters is that you have a Next
inside a With
block, which of course is an error.
错误不正确。从解释器的角度来看,它遇到的第一个问题是你在With块中有一个Next,这当然是一个错误。
Compilers and interpreters (with the possible exception of the clang C compiler) are not good at guessing which of two possible errors is the one you "meant" to have.
编译器和解释器(除了clang C编译器之外)不擅长猜测两个可能的错误中的哪一个是你“意味着”的错误。
#2
4
The error message you are getting is correct. "Next without For" is the error message because your code is attempting to terminate a For
code block while still executing a With
code block. Next
and End With
are both terminating statements, and nested blocks must be terminated in the correct order.
您收到的错误消息是正确的。 “Next without For”是错误消息,因为您的代码在执行With代码块时尝试终止For代码块。 Next和End With都是终止语句,嵌套块必须以正确的顺序终止。
A more plain-English reading of the error message would be, "You are trying to terminate a For
block of code, but you are currently in a block of code of some type other than For
so I don't know what to do here." It so happens that you were in a With
block, but the compiler isn't smart enough to include that information in its error message. So, the error message is correct, but it could certainly be improved.
对错误消息的更简单的英语读取将是,“您正在尝试终止For代码块,但您当前处于某种类型的代码块之外,因此我不知道该怎么做“。碰巧你在With块中,但编译器不够智能,不能在错误消息中包含该信息。因此,错误消息是正确的,但它肯定可以改进。
I misunderstood the deliberate nature of the code error in the example. Here is an error-free version for reference purposes:
我误解了示例中代码错误的故意性质。这是一个无错误的版本供参考:
Option Explicit
Public Sub Payments()
Dim ws As Worksheet
'--> Format the original workbook
For Each ws In ThisWorkbook.Worksheets
With ws
.UsedRange.EntireColumn.Hidden = False
End With
Next
End Sub
#3
1
Your question is valid. However, this is how VB compiler/intererpter works.
With the following code, it throws
你的问题是有效的。但是,这就是VB编译器/ intererpter的工作原理。使用以下代码,它会抛出
Compile error: End With without With
编译错误:没有With结束
Public Sub Payments()
Dim ws As Worksheet
'--> Format the original workbook
With ws
.UsedRange.EntireColumn.Hidden = False
For Each ws In ThisWorkbook.Worksheets
End With
End Sub
It looks like it goes last in first out (i.e. found End With
, but the previous element isn't a With
.
它看起来像是先出后退(即找到End With,但前一个元素不是With。
#1
8
The error is not incorrect. From the interpreter's point of view, the first problem that it encounters is that you have a Next
inside a With
block, which of course is an error.
错误不正确。从解释器的角度来看,它遇到的第一个问题是你在With块中有一个Next,这当然是一个错误。
Compilers and interpreters (with the possible exception of the clang C compiler) are not good at guessing which of two possible errors is the one you "meant" to have.
编译器和解释器(除了clang C编译器之外)不擅长猜测两个可能的错误中的哪一个是你“意味着”的错误。
#2
4
The error message you are getting is correct. "Next without For" is the error message because your code is attempting to terminate a For
code block while still executing a With
code block. Next
and End With
are both terminating statements, and nested blocks must be terminated in the correct order.
您收到的错误消息是正确的。 “Next without For”是错误消息,因为您的代码在执行With代码块时尝试终止For代码块。 Next和End With都是终止语句,嵌套块必须以正确的顺序终止。
A more plain-English reading of the error message would be, "You are trying to terminate a For
block of code, but you are currently in a block of code of some type other than For
so I don't know what to do here." It so happens that you were in a With
block, but the compiler isn't smart enough to include that information in its error message. So, the error message is correct, but it could certainly be improved.
对错误消息的更简单的英语读取将是,“您正在尝试终止For代码块,但您当前处于某种类型的代码块之外,因此我不知道该怎么做“。碰巧你在With块中,但编译器不够智能,不能在错误消息中包含该信息。因此,错误消息是正确的,但它肯定可以改进。
I misunderstood the deliberate nature of the code error in the example. Here is an error-free version for reference purposes:
我误解了示例中代码错误的故意性质。这是一个无错误的版本供参考:
Option Explicit
Public Sub Payments()
Dim ws As Worksheet
'--> Format the original workbook
For Each ws In ThisWorkbook.Worksheets
With ws
.UsedRange.EntireColumn.Hidden = False
End With
Next
End Sub
#3
1
Your question is valid. However, this is how VB compiler/intererpter works.
With the following code, it throws
你的问题是有效的。但是,这就是VB编译器/ intererpter的工作原理。使用以下代码,它会抛出
Compile error: End With without With
编译错误:没有With结束
Public Sub Payments()
Dim ws As Worksheet
'--> Format the original workbook
With ws
.UsedRange.EntireColumn.Hidden = False
For Each ws In ThisWorkbook.Worksheets
End With
End Sub
It looks like it goes last in first out (i.e. found End With
, but the previous element isn't a With
.
它看起来像是先出后退(即找到End With,但前一个元素不是With。