对象必需错误:保存文件

时间:2016-04-23 21:01:17

标签: macos vba csv export excel-vba-mac

更新:将ActiveWorkSheet切换到ActiveSheet并且似乎修复了对象所需的错误,但现在我收到此错误:

'calendar2016-04-23.csv' cannot be accessed.
The file may be read-only, or you may be trying to access a read-only location. Or, the server the document is stored on may not be responding.

取消后会出现此错误:

Run-time error '1004':
SaveAs method of Worksheet class failed

在excel中保存活动工作表时遇到问题。我正在尝试将2张纸保存到2个不同的csv文件中。但我遇到了两个主要问题

1: Object required error when it gets to the save portion.
2: It transfers to the new (failed) save file

我想将它们导出到单独的csv文件中,但仍保留在源文件中。

我的整个代码是

Sub prep()
'
' prep Macro
'

'
    Columns("G:G").Select
    ActiveWorkbook.Worksheets("1Input").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("1Input").Sort.SortFields.Add Key:=Range("G1"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("1Input").Sort
        .SetRange Range("A2:O9995")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With



    Columns("D:D").Select
    ExecuteExcel4Macro _
        "FORMULA.REPLACE("":00 GMT"","""",2,1,FALSE,FALSE,,FALSE,FALSE,FALSE,FALSE)"
    Columns("I:I").Select
    ExecuteExcel4Macro _
        "FORMULA.REPLACE(""(booster)"",""."",2,1,FALSE,FALSE,,FALSE,FALSE,FALSE,FALSE)"
    Columns("N:N").Select
    ExecuteExcel4Macro _
        "FORMULA.REPLACE("" (A)"","""",2,1,FALSE,FALSE,,FALSE,FALSE,FALSE,FALSE)"


    Columns("A:A").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Columns("K:K").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft

    Columns("M:M").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft


    Range("M2").Select
    ActiveCell.FormulaR1C1 = "=left"
    Range("M2").Select
    ActiveCell.FormulaR1C1 = "=LEFT(RC[-10],16)"
    Range("N2").Select
    ActiveCell.FormulaR1C1 = "=RIGHT(RC[-11],5)"
    Range("M2:N2").Select
    Selection.AutoFill Destination:=Range("M2:N1000"), Type:=xlFillDefault
    Range("M2:N164").Select
    Columns("B:B").Select
    ActiveWorkbook.Worksheets("1Input").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("1Input").Sort.SortFields.Add Key:=Range("B1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

    Range("O1").Select
    ActiveCell.FormulaR1C1 = "Subject"
    Range("P1").Select
    ActiveCell.FormulaR1C1 = "Start Date"
    Range("R1").Select
    ActiveCell.FormulaR1C1 = "Arrive By"
    Range("Q1").Select
    ActiveCell.FormulaR1C1 = "Start Time"
    Range("S1").Select
    ActiveCell.FormulaR1C1 = "Description"
    Range("U1").Select
    ActiveCell.FormulaR1C1 = "Driver First Name"

    Range("T1").Select
    ActiveCell.FormulaR1C1 = "End Time"


    Range("R2").Select
    ActiveCell.FormulaR1C1 = "=RIGHT(RC[-16],5)"

    Range("O2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(ISBLANK(RC[-10]),"""",CONCATENATE(""Text: "",RC[-3],"" ::::: "",RC[4]))"
    Range("P2").Select
    ActiveCell.FormulaR1C1 = "=TODAY()"
    Range("Q2").Select
    ActiveCell.FormulaR1C1 = "=RC[-3]-""1:05"""
    Range("R2").Select
    ActiveCell.FormulaR1C1 = "=RC[-4]-""0:05"""
    Range("T2").Select
    ActiveCell.FormulaR1C1 = "=RC[-3]-""00:55"""
    Range("U2").Select
    ActiveCell.FormulaR1C1 = "=LEFT(RC[-9],FIND("" "",RC[-9]&"" "")-1)"

'



    Columns("O:O").Select
    Selection.NumberFormat = "m/d/yy"


    Range("N2:T2").Select
    Selection.AutoFill Destination:=Range("N2: T10000"), Type:=xlFillDefault
    Range("N2:Z10000").Select
    Columns("A:Z").EntireColumn.AutoFit



    Columns("A:A").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft


'
    Range("R2").Select
    ActiveCell.FormulaR1C1 = _
        "=CONCATENATE(""Hi "",RC[2],"", Please arrive by "",TEXT(RC[-1],""hh:mm AM/PM""),"" for your next ride. Thank you."")"
    Range("R2").Select
    Selection.AutoFill Destination:=Range("R2:R10000"), Type:=xlFillDefault
    Range("T2").Select
    Selection.AutoFill Destination:=Range("T2:T10000"), Type:=xlFillDefault

    Columns("I:J").Select
    Selection.Delete Shift:=xlToLeft

    Columns("L:L").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = False
    End With
    Selection.Merge
    ExecuteExcel4Macro "PATTERNS(1,0,10,TRUE,2,4,0,0)"


    Range("O:O").Activate
    Selection.NumberFormat = "[$-F400]h:mm:ss AM/PM"
    Columns("P:P").Select
    Selection.NumberFormat = "[$-F400]h:mm:ss AM/PM"
    Columns("R:R").Select
    Selection.NumberFormat = "[$-F400]h:mm:ss AM/PM"


    Rows("1:2").Select
    Selection.Font.Bold = False
    Selection.Font.Bold = True


    Columns("M:S").Select
    Selection.Cut
    Sheets("CSV Export").Select
    Range("A1").Select
    ActiveSheet.Paste

    Sheets("CSV Export").Select
    ActiveWorkSheet.SaveAs _
    Filename:="/Users/Max/Desktop/Rides/Exported/calendar" _
    & Format(Date, "yyyy-mm-dd") & ".csv", _
    FileFormat:=xlCSVWindows


    Columns("A:G").Select
    Selection.Delete Shift:=xlToLeft

    Sheets("1Input").Select
    ActiveWorkSheet.SaveAs Filename:= _
        "/Users/Max/Desktop/Rides /Exported/rides.csv", FileFormat:=xlCSVWindows, _
        CreateBackup:=False

    Cells.Select
    Selection.Delete Shift:=xlUp

End Sub

我得到错误(在这里的代码底部):

Sheets("CSV Export").Select
ActiveWorkSheet.SaveAs _
Filename:="/Users/Max/Desktop/Rides/Exported/calendar" _
& Format(Date, "yyyy-mm-dd") & ".csv", _
FileFormat:=xlCSVWindows

我为这两个代码练习的地方是:

Sheets("CSV Export").Select
ActiveWorkSheet.SaveAs _
Filename:="/Users/Max/Desktop/Rides/Exported/calendar" _
& Format(Date, "yyyy-mm-dd") & ".csv", _
FileFormat:=xlCSVWindows


Columns("A:G").Select
Selection.Delete Shift:=xlToLeft

Sheets("1Input").Select
ActiveWorkSheet.SaveAs Filename:= _
    "/Users/Max/Desktop/Rides /Exported/rides.csv", FileFormat:=xlCSVWindows, _
    CreateBackup:=False

提前致谢!

1 个答案:

答案 0 :(得分:0)

我找到的决心归功于:Chris Bryson:https://www.youtube.com/watch?v=hnsL_01bHbU

我将他的宏附加到与我的主脚本相同的模块作为辅助宏。然后从原始宏调用他的宏。最终的脚本是:

Sub aprep()
'
' prep Macro
'

'
ActiveSheet.SaveAs _
Filename:="/Users/Max/Desktop/Rides/Extractor/Exported/" & "Extract" & Format(Date, "yyyy-mm-dd") & ".csv"

    Columns("G:G").Select
    ActiveWorkbook.Worksheets("Rides").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Rides").Sort.SortFields.Add Key:=Range("G1"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Rides").Sort
        .SetRange Range("A2:O9995")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With



    Columns("D:D").Select
    ExecuteExcel4Macro _
        "FORMULA.REPLACE("":00 GMT"","""",2,1,FALSE,FALSE,,FALSE,FALSE,FALSE,FALSE)"
    Columns("I:I").Select
    ExecuteExcel4Macro _
        "FORMULA.REPLACE(""(booster)"",""."",2,1,FALSE,FALSE,,FALSE,FALSE,FALSE,FALSE)"
    Columns("N:N").Select
    ExecuteExcel4Macro _
        "FORMULA.REPLACE("" (A)"","""",2,1,FALSE,FALSE,,FALSE,FALSE,FALSE,FALSE)"


    Columns("A:A").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Columns("K:K").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft

    Columns("M:M").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft


    Range("M2").Select
    ActiveCell.FormulaR1C1 = "=LEFT(RC[-10],16)"
    Range("N2").Select
    ActiveCell.FormulaR1C1 = "=RIGHT(RC[-11],5)"
    Range("M2:N2").Select
    Selection.AutoFill Destination:=Range("M2:N1000"), Type:=xlFillDefault
    Range("M2:N164").Select
    Columns("B:B").Select
    ActiveWorkbook.Worksheets("Rides").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Rides").Sort.SortFields.Add Key:=Range("B1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

    Range("O1").Select
    ActiveCell.FormulaR1C1 = "Subject"
    Range("P1").Select
    ActiveCell.FormulaR1C1 = "Start Date"
    Range("R1").Select
    ActiveCell.FormulaR1C1 = "Arrive By"
    Range("Q1").Select
    ActiveCell.FormulaR1C1 = "Start Time"
    Range("S1").Select
    ActiveCell.FormulaR1C1 = "Description"
    Range("U1").Select
    ActiveCell.FormulaR1C1 = "Driver First Name"

    Range("T1").Select
    ActiveCell.FormulaR1C1 = "End Time"


    Range("R2").Select
    ActiveCell.FormulaR1C1 = "=RIGHT(RC[-16],5)"


    Columns("T:T").Select
    Selection.NumberFormat = "m/d/yy"

    Range("O2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(ISBLANK(RC[-10]),"""",CONCATENATE(""Text: "",RC[-3],"" ::::: "",RC[4]))"
    Range("P2").Select
    ActiveCell.FormulaR1C1 = "=IF(ISBLANK(RC[-4]),""."",TODAY())"
    Range("Q2").Select
    ActiveCell.FormulaR1C1 = "=IF(ISBLANK(RC[-12]),"""",RC[-3]-""1:05"")"
    Range("R2").Select
    ActiveCell.FormulaR1C1 = "=IF(ISBLANK(RC[-13]),"""",RC[-4]-""0:05"")"
    Range("T2").Select
    ActiveCell.FormulaR1C1 = "=IF(ISBLANK(RC[-15]),"""",RC[-3]-""00:55"")"
    Range("U2").Select
    ActiveCell.FormulaR1C1 = "=IF(ISBLANK(RC[-16]),"""",LEFT(RC[-9],FIND("" "",RC[-9]&"" "")-1))"

'



    Columns("P:P").Select
    Selection.NumberFormat = "m/d/yy"


    Range("N2:T2").Select
    Selection.AutoFill Destination:=Range("N2: T10000"), Type:=xlFillDefault
    Range("N2:Z10000").Select
    Columns("A:Z").EntireColumn.AutoFit



    Columns("A:A").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft


'
    Range("R2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(ISBLANK(RC[-16]),"""",CONCATENATE(""Hi "",RC[2],"", Please arrive by "",TEXT(RC[-1],""hh:mm AM/PM""),"" for your next ride. Thank you.""))"
    Range("R2").Select
    Selection.AutoFill Destination:=Range("R2:R10000"), Type:=xlFillDefault
    Range("T2").Select
    Selection.AutoFill Destination:=Range("T2:T10000"), Type:=xlFillDefault

    Columns("I:J").Select
    Selection.Delete Shift:=xlToLeft

    Columns("L:L").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = False
    End With


    Range("O:O").Activate
    Selection.NumberFormat = "[$-F400]h:mm:ss AM/PM"
    Columns("P:P").Select
    Selection.NumberFormat = "[$-F400]h:mm:ss AM/PM"
    Columns("R:R").Select
    Selection.NumberFormat = "[$-F400]h:mm:ss AM/PM"


    Rows("1:2").Select
    Selection.Font.Bold = False
    Selection.Font.Bold = True


    Columns("M:S").Select
    Selection.Cut
    Sheets("Calendar").Select
    Range("A1").Select
    ActiveSheet.Paste

Call Splitbook

Call Clear


End Sub

Sub Splitbook()
MyPath = ThisWorkbook.Path
For Each sht In ThisWorkbook.Sheets
sht.Copy
ActiveSheet.Cells.Copy
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
ActiveSheet.SaveAs _
Filename:="/Users/Max/Desktop/Rides/Extractor/Exported/" & sht.Name & "_" & Format(Date, "yyyy-mm-dd") & ".csv"
ActiveWorkbook.Close savechanges:=True

Next sht
End Sub

Sub Clear()
'
' Clear Macro
'

'
    Cells.Select
    Selection.Delete Shift:=xlUp
    Sheets("Rides").Select
    Cells.Select
    Selection.Delete Shift:=xlUp
End Sub

决赛自行添加:

Call Splitbook

Call Clear


End Sub

Sub Splitbook()
MyPath = ThisWorkbook.Path
For Each sht In ThisWorkbook.Sheets
sht.Copy
ActiveSheet.Cells.Copy
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
ActiveSheet.SaveAs _
Filename:="/Users/Max/Desktop/Rides/Extractor/Exported/" & sht.Name & "_" & Format(Date, "yyyy-mm-dd") & ".csv"
ActiveWorkbook.Close savechanges:=True

Next sht
End Sub

Sub Clear()
'
' Clear Macro
'

'
    Cells.Select
    Selection.Delete Shift:=xlUp
    Sheets("Rides").Select
    Cells.Select
    Selection.Delete Shift:=xlUp
End Sub