2016-04-23 37 views
0

UPDATE:交換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 

哪裏IM練了兩個密碼是:

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 

在此先感謝!

+0

您使用的是Linux系統運行Excel或者你保存到一個網站嗎?只是想知道,因爲你在路徑中使用'/'而不是''。另外,你的路徑不是以'C:'或'E:'或'H:'等驅動器字母開始的。你可能想用'Dir'來驗證路徑:'如果Dir(「/用戶/最大/桌面/遊樂設施/導出/日曆/「)= vbNullString然後MsgBox」該路徑不存在!「# – Ralph

+0

Im使用mac。當我記錄最初的宏時,它是如何顯示它的,所以我正在使用它。 –

回答

0

我發現的決心是由於:克里斯·布賴森在這裏: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