VBA错误代码“编译错误:无效的外部过程”

时间:2019-03-08 12:08:19

标签: excel vba

我正在尝试编写从其他地方复制的代码。

该代码无法正常工作,并给我一个错误。

有人可以查看并告知是否存在语法错误

Dim directory As String, filename As String, sheet As Worksheet, total As Integer
Dim WrdArray() As String

Application.DisplayAlerts = False

directory = "U:\GMR & PAYROLL REPORTS 2018-19\FEBRUARY 2019\COMPLETED\PAYSLIPS\"
filename = Dir(directory & "*.csv")

Do While filename <> " "
    Workbooks.Open (directory & filename)
        WrdArray() = Split(filename, ".")
        For Each sheet In Workbooks(filename).Worksheets
            Wookbooks(filename).ActiveSheet.Name = WrdArray(0)
            total = Workbooks("PAYSLIPS CONSOL.xlsm").Worksheets.Count
            Workbooks(filename).Worksheets(sheet.Name).Copy after:=Workbooks("PAYSLIPS CONSOL.xlxm").Worksheets(total)

            GoTo exitFor:

        Next sheet

exitFor:
    Workbooks(filename).Close
    filename = Dir()
Loop
Sheets("ALL HOMES").Select
lastsheets = Worksheets.Count
For i = 2 To lastsheets
mysheet = Sheets(i).Activate
mysheetrow = Cells(Rows.Count, 1).End(x1Up).Row
Range("A1;U" & mysheetrow).Select
Selection.Copy
Sheets("ALL HOMES").Select
lastrow = Cells(Rows.Count, 1).End(x1Up).Row
Range("A1").Select
Range("A" & lastrow).Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
Next i
MsgBox "Your Report is Ready"

Application.DisplayAlerts = True

End Sub

1 个答案:

答案 0 :(得分:0)

通常,您可以使用VBA编译器检查项目是否可以编译。要在VBA编辑器中执行此操作,请单击“调试”->“编译VBA项目”。每次单击它时,它要么运行平稳,什么也不说,或者向您显示它所遇到的项目中的第一个编译错误(因此您可以对其进行修复,然后再次单击“编译”)。

我试图编译您发布的代码:

首先,如注释中所建议,您的顶部没有Sub行。例如

Sub create_payroll()

,其中create_payroll是此宏的首选名称。将这一行放在您在问题中发布的所有代码的上方。

第二,有一个错字“ Wookbooks”而不是Workbooks。