我一直在努力使我的錯誤訊息正常運行。運行宏時,它應該退出并檢查檔案夾匯入模板是否存在于保存主作業簿的檔案夾中,如果它確實顯示一條訊息,建議它已經存在并根據需要重命名或洗掉檔案夾。如果檔案夾不存在,則有代碼來創建檔案夾并將作業簿中的作業表匯出到該檔案夾??中。如果該檔案夾不存在,則代碼運行正常,如果存在,則不會彈出錯誤訊息并顯示運行時錯誤 #75。如果有任何人可以提供幫助,將不勝感激。
Private Sub ExportSheets() 'saves all visible sheets as new xlsx files
Dim ws As Worksheet, wbNew As Workbook
Dim sFolderPath As String
Dim fs As Object
Set wbNew = Application.ThisWorkbook
sFolderPath = wbNew.Path & "\" & "Import Templates"
If Dir(sFolderPath) <> "" Then
'If the folder does exist
MsgBox "The folder currently exists, please rename or delete the folder!", vbInformation, "Import Files"
Else
'If the folder does not exist available
MkDir sFolderPath
End If
For Each ws In ThisWorkbook.Sheets 'for each worksheet
If ws.visible Then 'if it's visible:
Debug.Print "Exporting: " & ws.Name
ws.Copy '(if no params specified, COPY creates activates a new wb)
Set wbNew = Application.ActiveWorkbook 'get new wb object
wbNew.SaveAs sFolderPath & "\" & ws.Name & ".csv", 23 'save new wb
wbNew.Close 'close new wb
Set wbNew = Nothing 'cleanup
End If
Next ws
Set ws = Nothing 'clean up
End Sub
uj5u.com熱心網友回復:
如果檢查您需要使用的檔案夾 vbDirectory
Dir(sFolderPath, vbDirectory)
默認為vbNormal,它只回傳檔案。
Exit Sub如果檔案夾已經存在,大概你應該在訊息框之后。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/339494.html
下一篇:如何防止軸關閉洗掉字幕?
