【问题标题】:VBA - How to identify max date in a column and save file with current year and month if the max date is in the current monthVBA - 如果最大日期在当前月份,如何识别列中的最大日期并使用当前年份和月份保存文件
【发布时间】:2021-03-09 15:12:59
【问题描述】:

这是我试图解决的两部分问题。首先,我想确定列中的最大日期。 如果最大日期在当前月份,那么我想使用当前年份和月份(文件名 YYYYMM)以当前格式保存文件。如果日期小于当前月份,我想保存当前年份和上个月的文件。

这是我开始用于识别最大日期的代码,但我的 MsgBox 显示的是时间而不是日期,所以我无法确认它是否真的有效。

Dim Max_date As Date
xl.Sheets("Data").Visible = True
xl.Sheets("Data").Select
xl.Range("I:I").Select
Max_date = Application.WorksheetFunction.Max(xl.Range("I:I"))
MsgBox Max_date

【问题讨论】:

  • 什么是xlxl.Sheets("Data") 然后 xl.Range("I:I")... 看起来很可疑。

标签: excel vba


【解决方案1】:

请花时间检查代码并配置您需要的一切。祝你好运

'This es the configurable part
Sub Export()
    Dim filePath As String, fileName As String
    filePath = OpenFileExplorer(msoFileDialogFolderPicker) '"" 'define your save path         or use -->'OpenFileExplorer(msoFileDialogFolderPicker)
    fileName = "eg. 09_03_2021" 'here you code max, remember dont use slash
    Copy_Save_Worksheet_As_Workbook "your Sheet to save", fileName, filePath
End Sub

'Starting a procedure to save a worksheet as new workbook
Public Sub Copy_Save_Worksheet_As_Workbook(SheetNameToCopy_ As String, saveAs__ As     String, path_ As String)
    Application.ScreenUpdating = False
    Dim wkb As Workbook
    'Check this line too
    Const fileExtencion = ".xlsx"
    'this is to check if already book open with same name
    For Each wkb In Workbooks
        If wkb.Name = saveAs__ & fileExtencion Then
            Workbooks(wkb.Name).Close False
        End If
    Next
   
    Dim finalPath As String
    finalPath = path_ & "\" & saveAs__
    ThisWorkbook.Sheets(SheetNameToCopy_).Select
    ActiveSheet.Copy
    ActiveSheet.SaveAs fileName:=finalPath & ".xlsx"
    Application.ScreenUpdating = True
 End Sub

Public Function OpenFileExplorer(t_ As MsoFileDialogType) As String
    Dim fd As Office.FileDialog
    Set fd = Application.FileDialog(t_)
   With fd
      .AllowMultiSelect = False

      ' Set the title of the dialog box.
      .Title = "Where to save"

      ' Clear out the current filters, and add our own.
      .Filters.Clear
     If t_ = msoFileDialogFilePicker Then
          .Filters.Add "All Files", "*.*"
     End If
     If .Show = True Then
         OpenFileExplorer = .SelectedItems(1)
     Else
        OpenFileExplorer = ""
     End If
   End With
End Function

【讨论】:

    猜你喜欢
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多