Excelexcel86
Board Regular
- Joined
- Feb 28, 2023
- Messages
- 99
- Office Version
- 365
- Platform
- Windows
- Mobile
Hi guys with the code below can this be modified where I can get only the most recent csv from different share point folders .
Sub openalllatestfiles()
Dim mypath As String
Dim myfile As String
Dim latestfile As String
Dim latestdate As Date
Dim wb As Workbook, ws As Worksheet '''''' new dims
Dim wsD As Worksheet ''''''''''' new dims
Dim rCell As Range, wsFiles As Worksheet
Dim land As Date
Set wsD = Sheets("Run All") '''''' Sheet to write values to (***Create this sheet***)
Set wsFiles = Sheets("Files") '''''' list of paths (column A) (***Create this sheet***)
For Each rCell In wsFiles.Range("A2:A" & wsFiles.Range("A" & Rows.Count).End(xlUp).Row).Cells
mypath = rCell.Value
If Right(mypath, 1) <> "\" Then mypath = mypath & "\"
myfile = Dir(mypath & "*.csv", vbNormal)
If Len(myfile) = 0 Then
Exit Sub
End If
Do While Len(myfile) > 0
land = Format(FileDateTime(mypath & myfile), "dd/mm/yyyy hh:mm:ss")
If land > latestdate Then
latestfile = myfile
latestdate = Format(land, "dd/mm/yyyy hh:mm:ss")
End If
myfile = Dir
Loop
latestdate = 0
Set wb = Workbooks.Open(mypath & latestfile) ' capture opened workbook
Set ws = wb.Sheets(1) ' capture sheet1 of opened workbook
ws.UsedRange.Offset(1).Copy ' copy the range from the opened workbook
wsD.Range("A" & wsD.Range("A" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues ' paste data into Run All sheet
Application.CutCopyMode = False
wb.Close False ' close the opned workbook without save
Next rCell
End Sub
Sub openalllatestfiles()
Dim mypath As String
Dim myfile As String
Dim latestfile As String
Dim latestdate As Date
Dim wb As Workbook, ws As Worksheet '''''' new dims
Dim wsD As Worksheet ''''''''''' new dims
Dim rCell As Range, wsFiles As Worksheet
Dim land As Date
Set wsD = Sheets("Run All") '''''' Sheet to write values to (***Create this sheet***)
Set wsFiles = Sheets("Files") '''''' list of paths (column A) (***Create this sheet***)
For Each rCell In wsFiles.Range("A2:A" & wsFiles.Range("A" & Rows.Count).End(xlUp).Row).Cells
mypath = rCell.Value
If Right(mypath, 1) <> "\" Then mypath = mypath & "\"
myfile = Dir(mypath & "*.csv", vbNormal)
If Len(myfile) = 0 Then
Exit Sub
End If
Do While Len(myfile) > 0
land = Format(FileDateTime(mypath & myfile), "dd/mm/yyyy hh:mm:ss")
If land > latestdate Then
latestfile = myfile
latestdate = Format(land, "dd/mm/yyyy hh:mm:ss")
End If
myfile = Dir
Loop
latestdate = 0
Set wb = Workbooks.Open(mypath & latestfile) ' capture opened workbook
Set ws = wb.Sheets(1) ' capture sheet1 of opened workbook
ws.UsedRange.Offset(1).Copy ' copy the range from the opened workbook
wsD.Range("A" & wsD.Range("A" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues ' paste data into Run All sheet
Application.CutCopyMode = False
wb.Close False ' close the opned workbook without save
Next rCell
End Sub