简体   繁体   English

使用Excel宏在文件名中保存具有上一个工作日日期的文件

[英]Using an Excel macro to save a file with the previous weekday date in the filename

I have created some code that currently works in saving a range in Excel and outputting it to word saving it with today's date minus 1 for the previous day in the filename. 我创建了一些代码,这些代码当前可用于在Excel中保存范围并将其输出为单词,将其保存为今天的日期减去前一天的文件名中的1。

However I'd like to know how I can change this code so on a Monday it will save it with the previous Friday's date, ultimately so that it will only use the current weekday minus 1 workday to save as the filename. 但是,我想知道如何更改此代码,因此在星期一将其与上一个星期五的日期一起保存,最终使其仅使用当前工作日减去1个工作日另存为文件名。

Dim ws As Worksheet
 Set ws = ActiveSheet

 Dim objWd As Object
 Set objWd = CreateObject("word.application")


Dim sPath As String

sPath = Environ("userprofile") & " "

objWd.Visible = True

Dim objDoc As Object
Set objDoc = objWd.Documents.Add

objDoc.PageSetup.Orientation = 0
 Application.ScreenUpdating = False

  Range("B218:G246").Select
    Selection.Copy

  objDoc.Content.Paste

Application.CutCopyMode = False

 Application.DisplayAlerts = False

 objDoc.SaveAs (sPath & "Report " & Format(Date - 1, "yyyy mm dd") & ".docx")

 Application.DisplayAlerts = True

 Application.ScreenUpdating = True

Replace the line 更换线

objDoc.SaveAs (sPath & "Report " & Format(Date - 1, "yyyy mm dd") & ".docx")

with

Dim d As Date  
d = Date
If Weekday(d) = vbMonday Then   
   d = d - 3
Else 
   d = d - 1
End If 

objDoc.SaveAs (sPath & "Report " & Format(d, "yyyy mm dd") & ".docx")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM