Word VBA,end sub失败(Word VBA, end sub fails)

编程入门 行业动态 更新时间:2024-10-25 00:24:29
Word VBA,end sub失败(Word VBA, end sub fails)

我有一个单词vba使用excel并有几个调用的过程:

private sub testsub1 'some process call testsub2 (a, b, c,...) end sub private sub testsub2 (byref a as long, byref b as long, byref c as long,...) 'some process call testsub3 (a, b, c,...) end sub private sub testsub3 (byref a as long, byref b as long, byref c as long,...) 'some process 'calls testub2 if a is less than some value. if a < somevalue then call testsub2 (a, b, c,...) end if documents("doc1").close 'closes a document wb.close 'closes a workbook exc.quit 'closes excel set wb = nothing set exc = nothing msgbox "Analysis complete" end sub

问题:在从testsub3调用testsub2之后,我无法在testsub3中结束sub。 在MsgBox之后,它跳转到testsub3中的一些代码(文档(“doc1”)。close)---错误:错误的文件名------>文档已经关闭。

但是如果没有调用testsub2我就能结束。

想法?

谢谢

注意:我不使用循环,因为代码太长(错误:过程太大)。 因此,多个程序/子。

I have a word vba that uses excel and has several procedures that call:

private sub testsub1 'some process call testsub2 (a, b, c,...) end sub private sub testsub2 (byref a as long, byref b as long, byref c as long,...) 'some process call testsub3 (a, b, c,...) end sub private sub testsub3 (byref a as long, byref b as long, byref c as long,...) 'some process 'calls testub2 if a is less than some value. if a < somevalue then call testsub2 (a, b, c,...) end if documents("doc1").close 'closes a document wb.close 'closes a workbook exc.quit 'closes excel set wb = nothing set exc = nothing msgbox "Analysis complete" end sub

Question: I cannot end sub in testsub3 after calling testsub2 from testsub3. After MsgBox, it jumps to some code in testsub3 (documents("doc1").close)---error: bad file name ------>document has already been closed.

But I am able to end if it did not call testsub2.

Ideas?

Thanks

Note: I don't use loop because the code is too long (error: procedure is too large). Hence the multiple procedures/sub.

最满意答案

也许你可以尝试类似的东西:

Private analysisComplete As Boolean Private Sub testsub1() 'some process analysisComplete = False Call testsub2(a, b, c,...) End Sub Private Sub testsub2(ByRef a As Long, ByRef b As Long, ByRef c As Long,...) 'some process Call testsub3(a, b, c) End Sub Private Sub testsub3(ByRef a As Long, ByRef b As Long, ByRef c As Long,...) 'some process 'calls testub2 if a is less than some value. If a < someValue Then Call testsub2(a, b, c) End If If Not analysisComplete Then Documents("doc1").Close 'closes a document wb.Close 'closes a workbook exc.Quit 'closes excel Set wb = Nothing Set exc = Nothing MsgBox "Analysis complete" analysisComplete = True End If End Sub

所以它只执行一次testsub3的最后一部分。

Tried this and worked, no errors

private sub testsub3 (byref a as long, byref b as long, byref c as long,...) 'some process 'calls testub2 if a is less than some value. if a < somevalue then call testsub2 (a, b, c,...) else documents("doc1").close 'closes a document wb.close 'closes a workbook exc.quit 'closes excel set wb = nothing set exc = nothing msgbox "Analysis complete" end if end sub

更多推荐

本文发布于:2023-08-07 19:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465951.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Word   VBA   fails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!