I have written macro for looping through the files (Excels) in one folder and copy specific cells from it.
My macro is working as it should but I have a small problem. Macro is looping files according to save date but I need to loop them according to file name. Is there any way how to make this in macro?
Public Sub Data_copy()
'DECLARE AND SET VARIABLES
Dim wbk As Workbook
Dim Filename As String
Dim Path As String
Path = "U:\KST\Antrag\" 'PATH
Filename = Dir(Path & "*.xlsm")
'--------------------------------------------
'OPEN EXCEL FILES
Do While Len(Filename) > 0 'IF NEXT FILE EXISTS THEN
Set wbk = Workbooks.Open(Path & Filename)
With ActiveWorkbook
Sheets("Form").Select
Range("O4:W4").Select
End With
Selection.Copy
Windows("Seznam_KST.xlsm").Activate
Sheets("List1").Select
Range("H" & ActiveCell.Row + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
wbk.Close True
Filename = Dir
Loop
End Sub
Here is a helper function to stuff and sort a variant array of filenames retrieved with VBA's Dir function.
Passing the optional second parameter in as True will produce an alphabetic descending order. Alternately you could simply flip the For ... Next and make it a Step -1.