打开只读模板会更改文件名

问题描述 投票:0回答:1

当我使用下面的子程序时,它会打开 DAILY OBSERVATIONS.xltm 工作簿,但会在末尾附加一个数字。我需要它作为原始文件名打开,因为我将新文件中的工作表复制回模板中。

Sub SaveWithDate()
Dim fname As String
fname = ActiveWorkbook.Name
Const SubName As String = "SaveWithDate"   'The name of this routine
Const SheetName As String = "OBSERVATIONS"      'The name of the sheet that can use this sub
If ActiveSheet.Name <> SheetName Then
  MsgBox "This macro can only be called from '" & SheetName & "'", vbOKOnly, SubName
  Exit Sub
End If
If ActiveWorkbook.Name <> "DAILY OBSERVATIONS.xltm" Then
    MsgBox "This macro can only be called from 'DAILY OBSERVATIONS.xltm. You already renamed this workbook.'", vbOKOnly, SubName
    Exit Sub
End If

Dim fileName As String
Dim currentDate As String

' Get the current date in the format YYYYMMDD
currentDate = Format(Now, "yyyymmdd")

' Specify the file name with the current date
fileName = "L:\DAILY OBSERVATIONS\" & currentDate & " DAILY OBSERVATIONS"

' Save the workbook with the specified file name
' ThisWorkbook.SaveAs fileName
ThisWorkbook.SaveAs fileName:=fileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
Workbooks.Open ("L:\DAILY OBSERVATIONS\DAILY OBSERVATIONS.xltm")


End Sub
excel
1个回答
0
投票

我最终只是录制了一个宏并调整它以按照我的需要工作。

Dim currentDate As String
currentDate = Format(Now, "yyyymmdd")
    Workbooks.Open fileName:="L:\DAILY OBSERVATIONS\DAILY OBSERVATIONS.xltm", _
        Editable:=True
    Windows(currentDate & " DAILY OBSERVATIONS.xlsm").Activate
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.