VBA编译错误'没有while'

我在excel中有一个VBAmacros,运行良好。 我一直在做这些循环已经工作的变化,突然代码不编译:

编译错误:Wend without While

我试图改变做… while循环,但我得到了相当的

编译错误:没有做循环

这里是我的代码的结构:

'This loop is fine While '(code for evaluation) 'code to run in loop Wend While '(more eval code) 'code While '(eval code) 'code Wend '<--This is where the compile error occurs 'code While '(eval code) 'code Wend 'code Wend 

有没有人有任何想法可能是什么问题? (奖金:代码的缩进实际上是否重要?)

这个消息Wend without While通常表示一个未被终止的IF

某处可能有一个IF没有匹配的结束如果

(这种错误信息有点烦人,因为它隐藏了真正的错误)

真正的问题是你有一个方法做了太多的事情。

你可以find不匹配的代码块,并修复它 – 但是你会被解雇的可读性问题与“嘿,它的工作,离开它”的借口。

 While 'code to run in loop Wend ' **** move to another method While 'code ' **** move to another method While 'code Wend ' **** move to another method 'code While 'code Wend 'code Wend 

在VBA代码中提取方法可能有点痛苦,因为必须确定参数和返回值,然后需要将提取点转换为方法/函数调用。 幸运的是, 有一个工具 (免责声明:我写了它)。