我有代码可以让我将工作表保存为 PDF、将其保存到文件夹以及将其附加到电子邮件中。 在代码中,我有一个“调用”来运行DeleteSomeFormulas,它会删除给定范围内的任何公式,将数据保留在适当的位置。
如果我从“开发人员”选项卡手动运行此代码,代码可以完美运行,但如果我将其应用于按钮,它会执行除删除公式之外的所有操作。
Sub Email_PDF()
Application.ScreenUpdating = False
Call DeleteSomeFormulas
Dim Path As String, File As String
Path = "Z:\Team\Staff Communications\Staff Briefings\Staff Briefings 2024" & "\"
With ActiveWorkbook: File = Left(.Name, InStr(.Name, ".") - 1): End With
With ActiveSheet
File = "QF28 - Staff Information - Weekly Briefing week commencing " & .Name
With .PageSetup
.Zoom = False
.FitToPagesTall = False
.FitToPagesWide = 1
.CenterHorizontally = True
.CenterVertically = False
End With
.Range("A1:F110").ExportAsFixedFormat xlTypePDF, Path & File
End With
With CreateObject("Outlook.Application").CreateItem(0)
.Display
.To = "Technicians"
.Subject = "Briefing Sheet"
.Body = "Please find attached the latest Briefing Sheet."
.Attachments.Add Path & File & ".pdf"
End With
End Sub
删除一些公式是:
Sub DeleteSomeFormulas()
With ActiveSheet
Call fileProtection(False)
Range("A2:F9").Copy
Range("A2:F9").PasteSpecial xlPasteValues
Call fileProtection(True)
End With
End Sub
如果手动运行,这两段代码都可以完美运行,并且 Email_PDF 从按钮运行,但从按钮运行时,DeleteSomeFormulas 不会作为调用运行。
DeleteSomeFormulas 更改为
Sub DeleteSomeFormulas(ws As Worksheet)
Application.ScreenUpdating = False
Call fileProtection(False)
Debug.Print ActiveSheet.Name
With ws.Range("A2:F9")
.Value2 = .Value2
End With
Call fileProtection(True)
Application.ScreenUpdating = True
End Sub
然后由
调用Call DeleteSomeFormulas(ActiveSheet)