How to Add Date and Time To File Name Using VBA in Excel

前端 未结 3 1749
你的背包
你的背包 2020-12-21 06:16

Thanks to Siddharth Rout at this Post I learned how to save a sheet to a new Worksheet. Now my question is how I can add Date and Time of file creation like:

3条回答
  •  粉色の甜心
    2020-12-21 06:38

    Change

    FName = "C:\Users\somebody\Documents\TestSheet" & _
            Format(Range("E19"), "mmm-d-yyyy") & ".xlsm"
    

    to

    FName = "C:\Users\somebody\Documents\TestSheet_" & _
            Format(Date, "ddmmmyyyy") & ".xlsm"
    

    If you are picking the date from Range("E19") then ensure that the cell has a valid date.. In such a case the code becomes

    FName = "C:\Users\somebody\Documents\TestSheet_" & _
            Format(Range("E19"), "ddmmmyyyy") & ".xlsm"
    

提交回复
热议问题