我是VBA的新手。我尝试执行以下操作,但代码并未按预期工作 -
Sub SheetsToCSV()
' Jerry Beaucaire(2010年1月25日),更新时间(2015年8月15日)
'将每个工作表保存到单个CSV文件中
Dim ws As Worksheet,fPATH As String
Application.ScreenUpdating = False'加速宏
Application.DisplayAlerts = False'自动覆盖旧文件
fPATH =" C:\ 2015 \ CSV \" '保存的路径,记住此字符串中的最终\
For Each ws In Worksheets
ws.Copy
ActiveWorkbook.SaveAs Filename:=fPATH & ActiveSheet.Name & ".csv", FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close
Next ws
Application.ScreenUpdating = True
结束子
答案 0 :(得分:1)
您只需在所有工作表中添加一个简单的循环并测试名称。
试试这个: -
Sub COPYSelectedSheetsToCSV()
Dim ws As Worksheet
'In case something goes wrong
On Error GoTo COPYSelectedSheetsToCSVZ
'Loop through all worksheets
For Each ws In ActiveWorkbook.Sheets
'Does the name contain "Upload"
If InStr(1, ws.Name, "Upload") > 0 Then
'Make the worksheet active
ws.Select
'Save it to CSV
ActiveWorkbook.SaveAs Filename:="/Users/reginaho/Desktop/Upload/" & ws.Name & ".csv", _
FileFormat:=xlCSV, CreateBackup:=False
End If
Next
COPYSelectedSheetsToCSVX:
'Clean up the memory usage
Set ws = Nothing
Exit Sub
COPYSelectedSheetsToCSVZ:
MsgBox Err.Number & " - " & Err.Description
Resume COPYSelectedSheetsToCSVX
End Sub