使用拆分创建数组会产生类型不匹配错误

时间:2019-07-19 12:57:54

标签: excel vba

我有一些工具清单。该程序的重​​点是根据输入的工具编号和工具信息搜索工具,以在特定文件夹中找到相应的工具文件。文件名包含工具信息的一部分。

我首先遍历库存清单,找到了特定的工具后,检索了相应的信息并尝试与文件夹中的文件名匹配。在这里,我创建了另一个循环来浏览文件。

Sub openBaseline(tn)

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object

Dim intpath As String
Dim path As String

Dim pn As String
Dim ps As String
Dim varr()
Dim partnum As String
Dim toolsize As String
Dim toolnumber As String


Dim i As Integer

'Testing If Me.idBox.Value = "" And Me.beadBox.Value = "" And Me.partBox.Value = "" Then Exit Sub

If tn = "" Then tn = InputBox("Scan or enter tool number.", "Load Baseline", "")
If tn = "" Then Exit Sub

'If Right(Left(tn, 2), 1) <> "-" Then
    'If Len(tn) = 5 Then
        'tn = Left(tn, 1) & "-" & Right(tn, 4)
        'Else:
            'MsgBox "Tool numbers should be in the format of '1-1234'", vbOKOnly + vbExclamation, "Error"
            'Exit Sub
    'End If
'End If
toolnumber = tn
'Debug.Print toolnumber



With ThisWorkbook.Sheets("Tool Log")
        intpath = "H:\PROCESS\PROCESS SAMPLES\SI-Baselines\JSP" 'Switch to \woodbridge.corp etc
        Set objFSO = CreateObject("Scripting.Filesystemobject")
        Set objFolder = objFSO.getfolder(intpath)
            'For Each objFile In objFolder.Files
            'varr = Split(objFile.Name, " ")
            'ReDim Preserve filename(objFolder.Files.count, 2)

        For i = 2 To .Cells(Rows.count, 1).End(xlUp).row Step 1
        'Debug.Print .Cells(Rows.count, 1).End(xlUp).row

            Debug.Print .Cells(i, "A")

            If .Cells(i, 1).Text = toolnumber Then
                Debug.Print i
                pn = .Cells(i, 3).Value
                ps = .Cells(i, 4).Value
                Debug.Print pn
                Debug.Print ps
            End If

              'i = 1
            For Each objFile In objFolder.Files
                Debug.Print objFile.Name
                'Debug.Print objFile.path

                varr() = Split(objFile.Name, " ")
                partnum = varr(0)
                toolsize = varr(1)

                Debug.Print partnum
                Debug.Print toolsize

                path = objFile.Name


                'Does not work for family tools
                Select Case toolsize
                Case Is = ps
                    If partnum = pn Then
                        Workbooks.Open filename:=objFile.path, UpdateLinks:=False, ReadOnly:=True
                        Exit For
                    End If
                Case Is = Right(varr(1), Len(varr(1)) - 1)
                    If partnum = pn Then
                        Workbooks.Open filename:=objFile.path, UpdateLinks:=False, ReadOnly:=True
                        Exit For
                    End If
                End Select


              Next objFile

                    'And toolsize = Right(ps, Len(ps) - 1) Then
                    'path = objFile.Name
                    'path = Right(path, Len(path) - Len(pn) - 1)
                    'If Left(path, Len(ps)) = ps Then
                        'Workbooks.Open filename:=objFile.path, UpdateLinks:=False, ReadOnly:=True
                        'Exit For
                    'End If
                'End If

                'i = i + 1
            Next i

End With
End Sub

它在行上给出类型不匹配错误

varr() = Split(objFile.Name, " ")

1 个答案:

答案 0 :(得分:0)

将数组变量声明为String(优选)数组或单个Variant,而不是Variant数组

此外,在分配split-command的结果时,您应该省略括号。

Dim varr() as String
' or: Dim varr as Variant
...
varr = Split(objFile.Name, " ")