我有一个代码,我想要:
我遇到的问题是,即使我尝试激活已编辑的Excel文件,代码也会将PDF文件保存为原始主文件。这里有什么帮助?非常感谢任何建议!代码如下:
ActiveWorkbook.Save
Sheets("Inventory").Select
Cells.Select
Selection.Copy
Cells.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.DisplayAlerts = False
Sheets("May").Select
Cells.Select
Selection.Copy
Cells.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Sheets("Macro").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = False
Sheets("Oct").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = False
Sheets("Inventory").Select
Range("A1").Select
Sheets("Inventory").Cells.Interior.ColorIndex = 0
ChDir "G:\9Fixed\Posi\2016\Inventory"
ActiveWorkbook.SaveAs Filename:= _
"G:\9Fixed\Posi\2016\Inventory\Asia Fixed - " & Format(Date, "dd mmm") & ".xls", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
Application.DisplayAlerts = False
'ActiveWorkbook.ExclusiveAccess
Application.DisplayAlerts = True
Workbooks("Asia - " & Format(Date, "dd mmm") & ".xls").Activate
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"G:\9Fixed Income\Positions\2016\Inventory\Asia Fixed Income - " & Format(Date, "dd mmm") & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
答案 0 :(得分:0)
我无法重现您遇到的问题。
这是我的'测试'代码,使用4张工作簿和单元格(1,1)中的信息,一张名为“May”的工作表---已删除,新文件没有“May”并且pdf也没有。
我将saveas代码移到了文件的顶部。也许这会解决你的问题,但我不相信会这样。
Option Explicit
Sub SaveCopies()
Dim c_OUTPUTDIR As String
Dim sFileName As String
Dim fso As Object ' Used to handle paths, filenames, etc.
Set fso = CreateObject("Scripting.FileSystemObject")
c_OUTPUTDIR = "C:\temp\"
' Save the master copy.
ActiveWorkbook.Save
' Generate new name for file.
sFileName = fso.GetBaseName(ActiveWorkbook.FullName) & "_" & Format(Date, "dd mmm")
' Save new working file.
ActiveWorkbook.SaveAs Filename:= _
c_OUTPUTDIR & sFileName & ".xls", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
' Make changes to working file.
Application.DisplayAlerts = False
ActiveWorkbook.Sheets("May").Delete
Application.DisplayAlerts = True
' Save the changes.
ActiveWorkbook.Save
' Save a PDF of the file.
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
c_OUTPUTDIR & sFileName & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub