English 中文(简体)
如何用VBA的文字查找最后一批CSV的下载文件?
原标题:Way for VBA script to find last CSV file in download folder?
  • 时间:2023-09-08 12:29:24
  •  标签:
  • excel
  • vba

能否让VBA宏观公司通过下载文件,以便进口最后(最近的)CSV ?

ChatGPT建议如下。

Sub ImportLastCSVFile()    
    Dim strPath As String
    Dim strFile As String
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim strRawPath As String

      Replace with the correct download directory
    strRawPath = "C:UsersAlexandreBrandtDownloads"
    strPath = strRawPath & "*.csv"
    strFile = Dir(strPath, vbNormal)

      Find the last CSV file
    While Len(strFile) <> 0
        If FileDateTime(strRawPath & strFile) > FileDateTime(strRawPath & strPath) Then
            strPath = strFile
        End If
        strFile = Dir()   Get next file
    Wend

      Set the workbook and worksheet
    Set wb = ThisWorkbook
    On Error Resume Next
    Set ws = wb.Sheets("Import")
    If Not ws Is Nothing Then
        Application.DisplayAlerts = False
        ws.Delete
        Application.DisplayAlerts = True
    End If

The VBE debugger is not accepting If FileDateTime(strRawPath & strFile) > FileDateTime(strRawPath & strPath) Then

是否有更好的办法通过档案来写假?

问题回答

使用数据,Get Data, From file, From Folder,是比较容易的。 将正确的道路(即,PQ也从所有分文件夹中找到)、CSV延伸过滤器、在档案日对电离层进行分类、然后“只靠一流”和扩大数据。 Done。

It s close.. Replace the initial chunk of code (down to Wend) with:

Dim strPath As String
Dim strFile As String
Dim strLatestFile As String
Dim wb As Workbook
Dim ws As Worksheet
Dim strRawPath As String

  Replace with the correct download directory
strRawPath = "C:UsersAlexandreBrandtDownloads"
strPath = strRawPath & "*.csv"
strFile = Dir(strPath, vbNormal)
strLatestFile = strFile

  Find the last CSV file
While Len(strFile) <> 0
    If FileDateTime(strRawPath & strFile) > FileDateTime(strRawPath & strLatestFile) Then
        strLatestFile = strFile
    End If
    strFile = Dir()   Get next file
Wend
strFile = strLatestFile

Import the Latest CSV File

Sub ImportLatestCSVFile()
    
      Define constants.
    
    Const SRC_ROOT_FOLDER_PATH As String = "C:UsersAlexandreBrandtDownloads"
    Const SRC_FILE_PATTERN As String = "*.csv"
    Const DST_SHEET_NAME As String = "Import"
    
    Dim dwb As Workbook: Set dwb = ThisWorkbook   workbook containing this code
    
      Attempt to store the first file name in a variable ( CurrentFileName ).
    
    Dim DirPath As String: DirPath = SRC_ROOT_FOLDER_PATH & SRC_FILE_PATTERN
    
    Dim CurrentFileName As String: CurrentFileName = Dir(DirPath)
    
    If Len(CurrentFileName) = 0 Then
        MsgBox "No files found.", vbCritical
        Exit Sub
    End If
    
      Store the path of the latest file in a variable ( LatestFilePath ).
    
      Declare additional variables used in the loop.
    Dim CurrentFilePath As String, LatestFilePath As String
    Dim CurrentDateTime As Date, LatestDateTime As Date
    
      Loop through all the files applying the logic...
    Do While Len(CurrentFileName) > 0
        CurrentFilePath = SRC_ROOT_FOLDER_PATH & CurrentFileName
        CurrentDateTime = FileDateTime(CurrentFilePath)
        If CurrentDateTime >= LatestDateTime Then   greater than or equal to max
            LatestDateTime = CurrentDateTime
            LatestFilePath = CurrentFilePath
         Else   less than max; do nothing
        End If
        CurrentFileName = Dir   next file
    Loop

      Attempt to delete the destination worksheet ( dws ).
    
    Dim dws As Worksheet
    
    On Error Resume Next
        Set dws = dwb.Sheets(DST_SHEET_NAME)
    On Error GoTo 0
    
    If Not dws Is Nothing Then   the worksheet exists
        Application.DisplayAlerts = False   delete without confirmation
            dws.Delete
        Application.DisplayAlerts = True
     Else   the worksheet doesn t exist; do nothing
    End If

      Continue, e.g.:
    
       Copy the one and only worksheet in the source file ( .csv )
       as the last sheet in the destination workbook.
 
     Dim swb As Workbook: Set swb = Workbooks.Open(LatestFilePath)
     Dim sws As Worksheet: Set sws = swb.Sheets(1)
 
     sws.Copy After:=dwb.Sheets(dwb.Sheets.Count)   copy as last sheet
     swb.Close SaveChanges:=False   close without saving changes
 
       Reference and rename the destination worksheet and save
       the destination workbook.
 
     Set dws = dwb.Sheets(dwb.Sheets.Count)
     dws.Name = DST_SHEET_NAME
      dwb.Save
     
       Inform.
     
     MsgBox "Latest file imported.", vbInformation        

End Sub




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签