vba - VBA错误处理,将错误定义为错误

标签 vba error-handling

我通过VBA宏启动Python。如果Python路径不好,我想要一个正确的错误消息。
我在2台不同的计算机上使用了此宏,而Python路径在这两台计算机上并不相同。 2条路径位于2个不同的单元格上,其名称分别为path_Python和path_Python_2
这是我的代码:

Function launchPython() As Boolean
Dim pathScript As String

pathScript = [path_files] + "code.py"

On Error GoTo err
    Shell [path_Python].Value & " " & pathScript
    launchPython = True
    Exit Function

err:
    On Error GoTo err2
        Shell [path_Python_2].Value & " " & pathScript
        launchPython = True
err2:
    launchPython = False
    MsgBox "Error: please check Python paths"

End Function

问题是,当2的路径不好时,我没有在VBA错误消息上阻塞,而是去了err2。
err:
    On Error GoTo err2
        Shell [path_Python_2].Value & " " & pathScript

我该如何解决?
非常感谢你

最佳答案

我将尝试检查文件是否存在并尝试尝试,而不是尝试将命令添加到可能缺少的路径中。您可以使用许多功能之一,例如http://allenbrowne.com/func-11.html中的FileExists,或仅检查dir $返回非空白。

Dim Path as string

path = path_python

if dir(path) = "" then 
  path = path_python2
  if dir(path) = "" then err.raise
end if

shell path  & " " & pathScript

关于vba - VBA错误处理,将错误定义为错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16415280/

相关文章:

vba - Excel-VBA : If-Else-Statement on convertion of number to text

vba - 将值输入到另一个宏调用的输入框

.net - vb6错误代码在vb.net中等效

android - Android Studio中的IDE问题

php - 我无法将 Sentry 添加到php slim

vba - 复制两个 Excel 实例之间的范围

VBA 将格式从一行复制到多行

sql-server - Excel VBA 和 ADODB : retrieve auto identity of row inserted into SQL Server from VBA

node.js - node express 的默认错误处理程序

ruby-on-rails - 如何在 Ruby on Rails 中单独呈现 XML 错误消息?