获取对象变量或未设置块变量

时间:2015-08-14 00:39:46

标签: excel excel-vba vba

此脚本遍历所有选定的工作表,并在顶部插入10个空白行。然后,它应导出这些工作表并将WS名称保存为csv格式。但是我似乎得到了错误Getting object variable or With Block variable not set

我尝试了一些东西,但到目前为止还没有成功。

Sub Insert_Rows_Loop()
Dim CurrentSheet As Object
Dim WS As Worksheet
Dim path As String


path = ActiveWorkbook.path & "\" & Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".") - 1)


' Loop through all selected sheets.
For Each CurrentSheet In ActiveWindow.SelectedSheets
    ' Insert 10 rows at top of each sheet.
    CurrentSheet.Range("a1:a10").EntireRow.Insert

    'With ws
            WS.SaveAs Filename:=path & "_" & WS.Name & ".csv", FileFormat:=xlCSV, CreateBackup:=False
        'End With


Next CurrentSheet
Application.DisplayAlerts = True
End Sub

1 个答案:

答案 0 :(得分:0)

运行宏时,我遇到了一个错误:运行时错误'429':ActiveX组件无法创建对象。

单击Debug将显示错误:“对象变量或未设置块变量”。

问题出在ThisWorkbook上。我用Application.ThisWorkbook替换了它,它解决了这个问题。因此,请尝试使用Application.ActiveWorkbook