Excel运行时错误'91'

时间:2017-05-13 01:00:55

标签: excel vba

我在模块1中有一个包含代码块的Excel文件。 此代码块正常工作。 当我打开此.xlsm文件并打开Visual Basic编辑器并打开模块(在本例中为模块1)并且不在那里进行任何更改或仅更改注释行中的单词,但在此之后保存模块并关闭出现问题的Excel文件。 当我下次打开文件时,收到错误消息:

运行时错误'91' 对象变量或未设置块变量

当我没有对模块进行任何更改时,怎么可能呢?

为了让它更容易理解,这是我在模块1中的脚本:

Private Sub Auto_Open()

'Sub ImportCSVs()
'Import all CSV files from a folder into separate sheets

Dim fPath   As String
Dim fCSV    As String
Dim wbCSV   As Workbook
Dim wbMST   As Workbook

Set wbMST = ActiveWorkbook

'Update the path to your CSV files below. Add your-username and your-folder
'Don't remove the the final \ from the file path

fPath = "C:\xampp\htdocs\csv\excel\"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
fCSV = Dir(fPath & "*.csv")

Do While Len(fCSV) > 0
    Set wbCSV = Workbooks.Open(fPath & fCSV)
    ActiveSheet.Move After:=wbMST.Sheets(wbMST.Sheets.Count)

    fCSV = Dir
Loop

Set wbCSV = Nothing

'Remove blank sheets, delete the first row from each sheet, zoom out and format as table

Dim ws As Worksheet
For Each ws In Worksheets

If WorksheetFunction.CountA(ws.Cells) <> 0 Then

    'ws.Rows(1).Delete
    ws.Select
        ActiveWindow.Zoom = 80
    ws.UsedRange.Select
        'ActiveSheet.ListObjects.Add(xlSrcRange, ActiveSheet.UsedRange, , xlYes).Name = "Table1"
        'ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium9"
        Selection.Columns.AutoFit

  End If

If WorksheetFunction.CountA(ws.Cells) = 0 Then
    Application.DisplayAlerts = False
            ws.Delete
            Application.DisplayAlerts = True
    End If

Next ws

'End
Application.ScreenUpdating = True

'open entry.xlsm after import all data
Workbooks.Open ("C:\Users\USER\Documents\entry.xlsm")

'minimize the data sheet
Windows("data.xlsm").Visible = False

'Dim wbkMain, wbk_A
'Set wbMain = ThisWorkbook
'Set wbk_A = Workbooks("entry.xlsm")
'Set wbk_A = ActiveWorkbook

End Sub

当我在更改任何内容后保存宏时,行

ActiveSheet.Move After:=wbMST.Sheets(wbMST.Sheets.Count)

在调试器中是高级的yello ...

我对宏编程不是很有经验,我不知道,那里发生了什么。

感谢您的帮助

0 个答案:

没有答案