Add filter for most recent document in VBA

annefeite

New Member
Joined
Feb 3, 2017
Messages
1
I am trying to add a filter to only select the most recent file to import, but am stuck. Does anyone know how to achieve this?

Sub opname1()
Const toFind = ".log"
Dim vpath As String
Dim vfile As String
Dim curTxt As Workbook

vpath = "C:\Users\test\Desktop\test"


vfile = Dir(vpath & "*" & toFind)

Do While vfile <> ""
Set curTxt = Workbooks.Open(vpath & vfile)
curTxt.Worksheets(1).Columns(1).TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2), Array(5, 2))
With curTxt.Worksheets(1)
.Range("f1").Formula = "=b1&"";""&c1"
.Range("f1").AutoFill Destination:=.Range("F1").Resize(WorksheetFunction.CountA(.Range("A:A")), 1)
.Range("F:F").Copy
.Range("B:B").PasteSpecial xlPasteValues
.Range("C:C").Delete
.Range("E:E").Delete
End With
curTxt.Worksheets(1).Move after:=ThisWorkbook.Worksheets(1)
vfile = Dir
Loop
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top