【问题标题】:Running an excel 2010 VBA script in excel 2016 - a litany of errors在 excel 2016 中运行 excel 2010 VBA 脚本 - 一连串错误
【发布时间】:2017-04-21 03:42:48
【问题描述】:

我无法在 excel 2016 上运行 excel 2010 文档中的脚本。 一些背景: 这是一组复杂的工作表,用于记录与单个化学品相关的风险和危害。创作花了很长时间,但现在在 2010 年运行良好,但第一个脚本在 excel 2016 中中断,我猜这不仅仅是一个问题。

脚本做的第一件事是保存文件,在文件名中包含化学品的名称:

    Private Sub CommandButton1_Click()
Dim chemical As String, illegal As String, fname As String
Dim X as Integer

    On Error Resume Next

    chemical = Range("Q13")       'this will be used as part of the filename

    If chemical <> "" Then

    Application.ScreenUpdating = False

    illegal = Array("<", ">", "|", "/", "*", "\", "?", "[", "]", ":")
        For X = LBound(illegal) To UBound(illegal)
            chemical = Replace(chemical, illegal(X), "-", 1)                'replaces illegal characters for file name
        Next X


    fname = Application.GetSaveAsFilename(InitialFileName:="Draft PAC " & chemical & ".xlsm")

        Do

        Loop Until fname <> False
        ActiveWorkbook.SaveAs Filename:=fname

    Application.ScreenUpdating = True

    Else: MsgBox "Please enter the name of the chemical into the orange shaded cell"

    End If

    End Sub

问题以“编译错误:预期数组”和 LBound 突出显示开始。 现在,我通过一些谷歌搜索发现新版本的 excel(或 VBA?)需要设置 Option Explicit,所以我已经这样做了,并且我已经声明了我的变量(或者我认为) - 但也许那不是实际问题?再说一次,这里可能不止一个问题。

我迷路了。

【问题讨论】:

  • 我使用的是 Excel 2010,我向您保证该版本中也存在问题。你不能做 LBound(illegal) 因为你已经声明 illegal 不是一个数组。较新版本的 Excel 需要 Option Explicit,但使用它是一个好主意,就像在早期版本中使用它是一个好主意一样。也许您添加了(不正确的)声明并且然后开始遇到您的问题?像 Do Loop Until fname &lt;&gt; False 这样的东西可能会在 2010 年导致无限循环,就像在新版本中一样。
  • 较新版本的 excel(或 VBA?)需要设置 Option Explicit - 哦,我多么希望是这样!
  • @Mat'sMug SO 的 excel-vba 标签将变得几乎多余如果Option Explicit 是强制性的!
  • 即使是较新版本的 RubberDuck 也不需要它:P
  • @Mat'sMug 是的:D 我必须用引号写“需要它”,只要它可以“理论上”被关闭;)

标签: vba excel excel-2010 excel-2016


【解决方案1】:

您需要对代码进行的更正包含在下面,但这些更改都不是由于从 Excel 2010 升级到更高版本 - 在早期版本的 Excel 中也需要它们。

Private Sub CommandButton1_Click()
    'Declare illegal as a Variant array
    Dim chemical As String, illegal() As Variant, fname As String
    Dim X As Integer

    'Get rid of the On Error so that you know when something doesn't work
    'On Error Resume Next

    chemical = Range("Q13").Value       'this will be used as part of the filename

    If chemical <> "" Then

        Application.ScreenUpdating = False

        illegal = Array("<", ">", "|", "/", "*", "\", "?", "[", "]", ":")
        For X = LBound(illegal) To UBound(illegal)
            chemical = Replace(chemical, illegal(X), "-", 1)                'replaces illegal characters for file name
        Next X

        'Put "fname = " within the loop so that it isn't an infinite loop
        'if the user does not select a filename
        Do
            fname = Application.GetSaveAsFilename(InitialFileName:="Draft PAC " & chemical & ".xlsm")
        Loop Until fname <> False
        ActiveWorkbook.SaveAs Filename:=fname

        Application.ScreenUpdating = True

    Else
        MsgBox "Please enter the name of the chemical into the orange shaded cell"
    End If

End Sub

【讨论】:

  • 感谢 YowE3K,它成功了 这看起来像是那些令人讨厌的“一开始不应该工作但它确实有效”的问题之一。我确信在我的代码中还有很多其他的电子表格......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多