对象必需的错误。将ActiveSheet保存到新工作簿

时间:2016-02-22 10:41:28

标签: excel vba excel-vba macros

此宏的目的是选择下拉数据验证列表中的每个项目,更新工作表,然后将工作表另存为新工作簿。我在ws.SaveAs之后得到了代码块的错误“Object required”。任何人都可以看到问题,因为我不能:/

Sub Create_excel_sheets()
Dim strValidationRange As String
Dim rngValidation As Range
Dim rngDepartment As Range
Dim ws As Worksheet

strValidationRange = Range("AD5").Validation.Formula1
Set rngValidation = Range(strValidationRange)

For Each rngDepartment In rngValidation.Cells
    Range("AD5").Value = rngDepartment.Value

        ActiveSheet.Calculate
        Set ws = ActiveSheet
        ws.SaveAs _
        FileFormat:=52, _
        Filename:="C:\Test\" & rngDepartment.Value.xlsx, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

Next

End Sub

1 个答案:

答案 0 :(得分:2)

您似乎在Worksheet.ExportAsFixedFormat Method内使用Worksheet.SaveAs method的参数。

此外,使用filename:= & rngDepartment.Value.xlsx字符串连接有点不稳定。最好简单地省略文件扩展名并允许FileFormat:=参数提供正确的参数。在这种情况下,您选择的XlFileFormat Enumeration(例如 52 )适用于 xlOpenXMLWorkbookMacroEnabled ,但您似乎试图附加 .xlsx 最后。

    ws.SaveAs FileFormat:=xlOpenXMLWorkbookMacroEnabled, _
              Filename:="C:\Test\" & rngDepartment.Value   '<~~no extension

我不确定你是否尝试使用.SaveAs或.ExportAsFixedFormat。我选择了一个简单的.SaveAs用于演示