gpt4 book ai didi

Way for VBA script to find last CSV file in download folder?(VBA脚本如何找到下载文件夹中的最后一个CSV文件?)

转载 作者:bug小助手 更新时间:2023-10-25 19:25:26 25 4
gpt4 key购买 nike



Is it possible for a VBA macro to loop through files in the download folder in order to import the last (most recent) CSV ?

VBA宏是否可以遍历下载文件夹中的文件以导入上一个(最新的)CSV?


ChatGPT has suggested the following.

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:\Users\AlexandreBrandt\Downloads\"
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

如果FileDateTime(strRawPath&strFile)>FileDateTime(strRawPath&strPath),则VBE调试器不接受


Is there a better way to write the loop through the files?

有没有更好的方法来编写遍历文件的循环?


更多回答

strRawPath & strPath makes no sense, it contains the path twice. What do you want to compare to? If you need the last file by date, you would have to store the date and check each file against the stored date.

StrRawPath&strPath没有任何意义,它包含两次路径。你想拿什么来比较呢?如果您需要按日期存储最后一个文件,则必须存储该日期,并对照存储的日期检查每个文件。

Hey Paul. In fact it needs to compare against todays date. Today's date and time would be the most resent.

嘿,保罗。事实上,它需要与今天的日期进行比较。今天的日期和时间将是最令人反感的。

This is way easier using Data, Get Data, From File, From Folder. Filter the filelist the correct path (by default PQ looks in all sub-folders too), filter for CSV extension, sort Descending on file date and then "Keep first row only" and expand the data. Done.

使用数据、获取数据、从文件、从文件夹获取数据要容易得多。以正确的路径过滤文件列表(默认情况下,PQ也会在所有子文件夹中查找),过滤CSV扩展名,按文件日期降序排序,然后“只保留第一行”并展开数据。好了。

优秀答案推荐

Import the Latest CSV File


Sub ImportLatestCSVFile()

' Define constants.

Const SRC_ROOT_FOLDER_PATH As String = "C:\Users\AlexandreBrandt\Downloads\"
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


You will just need to store in a variable the latest date and in another variable the latest file. Whenever you find a newer file, just override them:

您只需将最新日期存储在一个变量中,并将最新文件存储在另一个变量中。每当您找到较新的文件时,只需覆盖它们:


    strFile = Dir(strPath, vbNormal)
Dim strLatestFile As String = ""
Dim currentDate As DateTime
Dim latestDate As DateTime

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

Now, you can use strLatestFile however you please.

现在,您可以随心所欲地使用strLatestFile。



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

很近..。将初始代码块(一直到Wend)替换为:


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:\Users\AlexandreBrandt\Downloads\"
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
Debug.Print "Latest file set to : " & strFile
End If
strFile = Dir() ' Get next file
Wend
strFile = strLatestFile

更多回答

Hello there Thanks for the suggestion here. I'm wondering why would their be a section to delete the destination worksheet. Is this to delete it or rather to prevent it be overwritten with the the new CSV import?

你好,谢谢你的建议。我想知道为什么他们会是一个删除目标工作表的部分。这是为了删除它,还是更确切地说,是为了防止它被新的CSV导入覆盖?

The idea is that if you want the imported worksheet to be called Import but another (previously imported?) worksheet is already named Import, then you want to delete the existing worksheet and copy the new worksheet instead. Your posted code deletes a worksheet at the bottom. If that's not the functionality you're looking for, add more detail to your post.

其想法是,如果您希望将导入的工作表命名为导入,而将另一个工作表命名为(以前导入的?)工作表已命名为导入,然后您希望删除现有工作表并复制新工作表。您发布的代码将删除底部的工作表。如果这不是你想要的功能,请在你的帖子中添加更多细节。

Hey, ok I see! Yes I wanted to name it as Import because the excel tabs are limited in their names length and so using the imported CSV full name could lead to issues. I imagine that Excel does truncate the name when the user imports via the Data -> From Text/CSV but that would be something that the VBA would need to specify.

嘿,好的,我明白了!是的,我想将其命名为导入,因为Excel选项卡的名称长度有限,因此使用导入的CSV全名可能会导致问题。我想,当用户通过Data->from Text/CSV导入时,Excel确实会截断名称,但这将是VBA需要指定的内容。

Hey there. Thanks for the suggestion. So this codes runs smooth but nothing actually happens. When going in break mode, I see the VBA is looping indefinitely here ' 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 So it does not get out of that. I guess its not finding the file according to those parameters.

嘿你好啊。谢谢你的建议。因此,这些代码运行得很流畅,但实际上什么都没有发生。当进入中断模式时,我看到VBA无限循环在这里‘查找最后一个CSV文件,而LEN(StrFile)<>0 if FileDateTime(strRawPath&strFile)>FileDateTime(strRawPath&strLatestFile)Then strLatestFile=strFileEnd if strFile=Dir()’获取下一个文件Wend,这样它就不会退出。我猜它不是根据这些参数找到文件的。

I'm not sure how that gets stuck in a loop. It works fine on my machine, reading every CSV file and storing the latest in strFile. You just need to (presumably) add some code to open it?

我不确定这是怎么陷入循环的。它在我的机器上运行得很好,可以读取每个CSV文件,并将最新的文件存储在strFile中。你只需要(大概)添加一些代码来打开它?

Hello. Ah, I see how this is working. Its looping though every CSV in the downloads folder. But will only store one value in strFile? Is there a way in the VBE to what values has been stored into the variables to see if this is getting stored correctly. The files do not have any passwords, they can be opened.

你好。啊,我知道这是怎么回事了。它循环通过下载文件夹中的每个CSV。但是否只会在strFile中存储一个值?在VBE中,有没有一种方法可以知道变量中存储了哪些值,以查看这些值是否被正确存储。这些文件没有任何密码,可以打开。

You can use the Watch Window, drag a variable into it from the code. Or you can Debug.Print strFile in the code to see it output to your Immediate Window during runtime. I've added a Debug.Print to the code as an example.

您可以使用“监视”窗口,将一个变量从代码拖到其中。或者,您可以在代码中Debug.Print strFile以查看它在运行时输出到您的即时窗口。我在代码中添加了一个Debug.Print作为示例。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com