EXCEL VBA运行时错误5:过程调用或参数无效

EXCEL VBA运行时错误5:过程调用或参数无效,excel,vba,runtime,Excel,Vba,Runtime,我有一个Xl宏,代码如下 Sub fileDelimiter(wb As Workbook) Const DELIMITER As String = "|" Dim myRecord As Range Dim myRecordSet As Range Dim myField As Range Dim nFileNum As Long Dim sOut As String Dim counter As Long D

我有一个Xl宏,代码如下

Sub fileDelimiter(wb As Workbook)
    Const DELIMITER As String = "|"
    Dim myRecord As Range
    Dim myRecordSet As Range
    Dim myField As Range
    Dim nFileNum As Long
    Dim sOut As String
    Dim counter As Long
    Dim dataElementName As String
    Dim lastRow As Long
    
    lastRow = Cells(Rows.Count, "B").End(xlUp).Row
    
    If lastRow > 1 Then
        counter = 0
        'Debug.Print el_coll(Left(wb.Name, 5))
        dataElementName = el_coll(Left(wb.Name, 5))

        MyPath = "........\" & dataElementName & ".txt"
        
        If Dir(MyPath) = "" Then
            Set myRecordSet = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
        ElseIf FileLen(MyPath) = 0 Then
            Set myRecordSet = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
        Else
            Set myRecordSet = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
        End If

        nFileNum = FreeFile
        Open MyPath For Append As #nFileNum
        For Each myRecord In myRecordSet
            With myRecord
                For Each myField In Range(.Cells, _
                    Cells(.Row, Columns.Count).End(xlToLeft))
                    sOut = sOut & DELIMITER & myField.Text
                Next myField
                Print #nFileNum, Mid(sOut, 2)
                sOut = Empty
            End With
        Next myRecord
        wb.Save
        Close #nFileNum
        wb.Close
    Else
        wb.Save
        wb.Close
    End If
End Sub

我是Excel VBA的新手。似乎我一直在运行运行时错误5:

dataElementName = el_coll(Left(wb.Name, 5))
你能帮我找到这个问题吗? 谢谢

El_coll是活动工作簿中另一个模块中的自定义函数吗?如果不存在,则会出现错误,因为该行无法找到要执行的函数。

选项显式 公共el_coll As系列

'运行主子程序来完成所有操作 次级运行() 将wb设置为工作簿 将日期范围设置为字符串 尺寸表名称为字符串 Dim xFiles()作为字符串 作为字符串的Dim扩展名 Dim变量作为变量

sheet_name = "Title Page"
extension = "xlsx"
Call init_data_el_coll
Call empty_txt_files(ActiveWorkbook.Path)
xFiles = ListAllFile(extension, ActiveWorkbook.Path)

dateRange = InputBox("Enter Data Date yyyy-mm: (Ex: 2014-02)")

If dateRange = "" Then
    Set el_coll = Nothing
    Call MsgBox("Cancelled operation", vbOKOnly, "Cancelled")
    Exit Sub
Else
    For Each vars In xFiles
        Set wb = Workbooks.Open(vars)
        Call delete_sheet(wb, sheet_name) 'Delete the sheet with the name
        wb.Save 'Save the changes
        Call text_manip(wb, dateRange)
    Next
    Set el_coll = Nothing
    Call MsgBox("Completed operation", vbOKOnly, "Completed")
End If

End Sub

你能为
el\u coll()
添加代码吗?我看不到代码中设置的
wb
。@daveExcel它作为参数传递到Sub中。Public el\u coll as Collection是一个数组它是Public el\u coll as Collection答案空间用于答案。如果您需要在问题中添加代码,请输入问题。