我有一个VBA宏,它允许我以CSV格式(使用逗号作为分隔符)导出一些excel文件,在这种情况下,前7页。我有以下问题:
代码允许导出前1-n张,但我想把代码放到名称中选择工作表。在这种情况下,我还可以导出名为“MILANO”的工作表1和名为“ROME”的工作表5.
我无法找到将源文件自动保存在源excel文件的同一文件夹中的方法。我使用了ActiveWorkbook.Path或ThisWorkbook.Path,但我想我错了
我不能只导出每张纸的行,而不是像CSV一样导出数百行,包括,,,,,,,,
这里是宏:
Sub CreateCSV()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'-----------------------------
'DECLARE AND SET VARIABLES
Dim wb1 As Workbook, ws1 As Worksheet
Dim wbname As String, I As Integer
Set wb1 = ThisWorkbook
'-----------------------------
'CYCLE THROUGH SHEETS AND MATCH UPLOAD
For I = 1 To 7
wbname = Worksheets(I).Name
'-----------------------------
'COPY SHEET INTO NEW CSV FILE
Worksheets(I).Copy
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "/" & wbname & "/.csv", _
FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close
wb1.Activate
Next I
'-----------------------------
'CLEANUP
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
谢谢!
答案 0 :(得分:0)
试试这点2
ActiveWorkbook.SaveAs文件名:= wb1.Path& " \" &安培; wbname& " .csv",_
答案 1 :(得分:0)
关于按名称访问工作表,您可以执行此操作,
set sh = ThisWorkBook.Sheets("MILANO")
但是既然你想要遍历你的工作表,你需要一个包含这样的工作表名称的数组,
Dim mySheets as Variant
Dim sh as WorkSheet
Dim I as Long
mySheets=Array("MILANO" , "MONACO", "ROME")
For I = 0 to UBound(mySheets)
Set sh = ThisWorkBook.Sheets(mySheets(I))
sh.SaveAs FileName:=ThisWorkBook.Path & "\" & mySheets(I), _
FileFormat:=xlCSV
Next I
所以你需要使用WorkSheet.SaveAs而不是WorkBook.SaveAs
至于"我不能只导出每张纸的行,而不是像CSV一样,我看到数百行有,,,,,,,,,"也许你需要先清理工作表