【问题标题】:VBA: Remove a formatted date from the end of a file everyday and resaveVBA:每天从文件末尾删除格式化日期并重新保存
【发布时间】:2018-09-12 16:11:24
【问题描述】:

每天我都会收到一个以 DailyFile 09-12-18.xlsx 格式保存的自动文件。

我编写了一个 VBA 模块来查找一个名为(在本例中)DailyFile 的文件,并使用新的每日数据更新我必要的电子表格。

但是,我需要一种方法来从每日文件中删除日期部分,以便我的模块可以识别它,但我还需要为我的每个每日文件(在文件名中包含日期)保留一份副本记录。

换句话说,我的主电子表格需要从每日文件中提取最新数据(可以从昨天开始覆盖),但我还想保存一份原始数据。

这是我到目前为止所拥有的......它不起作用:

Option Explicit


Sub changefilename()

Dim tdate As Variant
Dim ofile As Workbook
Dim TestFile As String



tdate = Date
tdate.NumberFormat = "mm-dd-yy"

Set ofile = Workbooks.Open("C:\Users\Research\Documents\Daily File " & tdate & ".xlsx")
ofile.SaveAs Filename:=TestFile


End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:
     Option Explicit 
    
        Sub changefilename()
    
     Dim ofile As Workbook
     Dim TestFile As String 
    
    
        '##What we want the new save file to be called, with path:
    TestFile = "C:\Users\Research\Documents\TEST.xlsx"
    
    
        '##Searches for a file that concats the file name with today's date.
        '##Use format(Date,____) for how your date is formatted. No need to assign a variable to the Date.
    Set ofile = Workbooks.Open("C:\Users\Research\Documents\DailyFile" & Format(Date, "mm-dd-yy") & ".xlsx")
    
           '##Saves the old file in the desired path!
    ofile.SaveAs Filename:=TestFile
    
    
        End Sub
    

    【讨论】:

      【解决方案2】:

      .NumberFormat 是一个单元格属性。 tdate 不是单元格,而是 Variant,因此没有该属性。

      改用Format 函数对其进行格式化:

      Option Explicit
      
      Sub changefilename()
      
          Dim tdate As Variant
          Dim ofile As Workbook
          Dim TestFile As String
      
          tdate = Format(Date, "mm-dd-yy")
      
          Set ofile = Workbooks.Open("C:\Users\Research\Documents\Daily File " & tdate & ".xlsx")
          ofile.SaveAs Filename:=TestFile
      
      End Sub
      

      【讨论】:

      • 自己想出了一个类似的方法。没有意识到 .NumberFormat 是如何工作的。使用过的 Format()... 就像一个魅力。查看我的解决方案。
      • @user10354299 注意这个答案是如何解释什么和为什么,而不是简单地分发代码转储。像这样的答案对于人们在谷歌上搜索类似问题的解决方案更有用,这最终是这个网站的全部内容;考虑通过单击空心复选标记将此答案标记为“已接受”。
      • @Marcucciboy2 我故意将tdate 保留为一个变体,因为我尝试尽可能少地进行更改,以便于追查真正导致错误的原因。如果我要重写它,我可能会完全省略变量并直接连接它。为什么TestFile 没有价值也是如此。
      • @MathieuGuindon 是的,我经常访问该网站寻求帮助。非常感谢您的解释-我知道对于有类似问题的人来说有多远。只是为自己解决它而感到自豪......想分享。
      • @user10354299 自我回答完全没有害处,甚至鼓励!如果您的回答解释了发生了什么,我绝对会给它一个赞成票 =) 继续努力!
      【解决方案3】:
      Sub changefilename()
      
          Dim tdate As String
          Dim ofile As Workbook
          Dim ofile_Name As String
      
          tdate = Format(Date, "MM-DD-YY")
      
          Set ofile = Workbooks.Open("C:\Users\Research\Documents\Daily File " & tdate & ".xlsx")
      
          'change the character 9 to whatever the length of the file name
          ofile_Name = Left(ofile.Name, 9)
      
          ofile.SaveAs Filename:=ofile_Name   
      
      End Sub
      

      【讨论】:

      • 我想你的意思是ofile.SaveAs Filename:=ofile_Name
      • 就我个人而言,我认为我会使用ofile_Name = ofile.path & "\Daily File.xlsx" 而不是Left(...)
      猜你喜欢
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      相关资源
      最近更新 更多