1

尝试执行以下子例程时出现错误

“运行时错误:9,下标超出范围”

它突出显示了第一个变量声明。起初我认为这是由于错误的数据类型,但改变和玩弄没有运气。

我也尝试了单元格和范围

Public vFolderPath As String
Public vCMFNewPath As String
Public vKBNewPath As String
Public vDPI As Integer

Private Sub SetGlobal()

Dim vGo As String
Dim vTemplateLocation As String
Dim vCMFFilename As String
Dim vKBFilename As String
Dim vDriver As String
Dim vPKG As String

    vDPI = Workbooks("tools.xlsm").Sheets("SETTINGS").Range("B2").Value

    vFolderPath = Workbooks("tools.xlsm").Sheets("SETTINGS").Range("B3").Value & "\"

有任何想法吗?

4

2 回答 2

3

代码运行良好,从名为 Tools.xslm 的文件运行。带有一个名为“设置”的选项卡,单元格 B2 中的整数和单元格 B3 中的字符串值。

这在从 Personal xlsb 中的模块或从 Tools.xlsm 中运行时有效。即使您没有声明任何变量,它也可以工作。

于 2013-03-22T12:05:28.423 回答
0

试试下面的代码:

Public vFolderPath As String
Public vCMFNewPath As String
Public vKBNewPath As String
Public vDPI As Integer

Private Sub SetGlobal()


    Dim vGo As String
    Dim vTemplateLocation As String
    Dim vCMFFilename As String
    Dim vKBFilename As String
    Dim vDriver As String
    Dim vPKG As String

    Dim wkbSetting As Workbook, shtSetting As Worksheet

    On Error Resume Next
    Set wkbSetting = Workbooks("tools.xlsm")
    On Error GoTo 0

    On Error GoTo err_rout
    If Not wkbSetting Is Nothing Then

        On Error Resume Next
        Set shtSetting = wkbSetting.Sheets("SETTINGS")
        On Error GoTo 0

        On Error GoTo err_rout
        If shtSetting Is Nothing Then
            Err.Raise Number:=32, Description:="Sheets Settings not found"
        End If


        vDPI = CInt(shtSetting.Range("B2").Value)
        vFolderPath = shtSetting.Range("B3").Value & "\"

    Else
        Err.Raise Number:=31, Description:="Workbook - tools.xlsm not found"
    End If


Exit Sub
err_rout:
    MsgBox Err.Description, vbInformation

End Sub
于 2013-03-22T15:00:23.987 回答