我是 VBA 新手。我尝试执行以下操作,但代码没有按预期运行。
将工作簿中的特定工作表(任何包含“上传”的工作表名称)导出/复制到特定文件目录。
我不希望这些工作表名称改变,也不希望工作簿名称改变。
每个工作表的文件名都是一致的,因此每次运行宏时都可以替换目录中的文件。出现一个对话框询问我是否确定要替换每个文件,这样就可以了。
我不想打开新创建的 CSV 或任何其他文件。
Sub COPYSelectedSheetsToCSV() ' ' Sheets("Moo Upload").Select Sheets("Moo Upload").Name = "Moo Upload" ActiveWorkbook.SaveAs Filename:="/Users/reginaho/Desktop/Upload/Moo Upload.csv", _ FileFormat:=xlCSV, CreateBackup:=False Sheets("Dodo Upload").Select ActiveWorkbook.SaveAs Filename:="/Users/reginaho/Desktop/Upload/Dodo Upload.csv", _ FileFormat:=xlCSV, CreateBackup:=False Sheets("Lulu Upload").Select ActiveWorkbook.SaveAs Filename:="/Users/reginaho/Desktop/Upload/Lulu Upload.csv", _ FileFormat:=xlCSV, CreateBackup:=False Sheets("Ahhh Upload").Select ActiveWorkbook.SaveAs Filename:="/Users/reginaho/Desktop/Upload/Ahhh Upload.csv", _ FileFormat:=xlCSV, CreateBackup:=False End Sub
答案1
Option Explicit
Public Sub COPYSelectedSheetsToCSV()
Const FLDR As String = "/Users/reginaho/Desktop/Upload/"
Const ASK_ As Boolean = True
Dim ws As Worksheet
Application.DisplayAlerts = ASK_
For Each ws In Worksheets
If InStr(LCase(ws.Name), "upload") > 0 Then ws.SaveAs FLDR & ws.Name, xlCSV
Next
Application.DisplayAlerts = Not ASK_
End Sub