Vlookup with the latest File in folder

LearnerDude1

New Member
Joined
Jul 9, 2019
Messages
1
Hello I am trying to create VBA code where it extract data from the most recent folder in a directory and uses it to drop data to another folder. I got the latest file part working but i'm having trouble with the Vlookup portion i.e calling out the latest file.

Here is my latest file code
Code:
  Dim MyPath As String
    Dim MyFile As String
    Dim LatestFile As String
    Dim LatestDate As Date
    Dim LMD As Date

    MyPath = "C:\Users\TAmon1\Desktop\OverSubscription Dash"
    If Right(MyPath, 1) <> "" Then MyPath = MyPath & ""
    MyFile = Dir(MyPath & "*.csv", vbNormal)
    If Len(MyFile) = 0 Then
        MsgBox "No files were found...", vbExclamation
        Exit Sub
    End If
    Do While Len(MyFile) > 0
        LMD = FileDateTime(MyPath & MyFile)
        If LMD > LatestDate Then
            LatestFile = MyFile
            LatestDate = LMD
        End If
        MyFile = Dir
    Loop


And here is the Vlookup portion
Code:
Range("N2").Select
ActiveCell.FormulaR1C1 =VLOOKUP(C[-13],'Workbooks.Open MyPath & LatestFile'!C1:C11,11,FALSE)"


How can I call out the latest file properly
 
Last edited by a moderator:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi LearnnerDude,

please when posting code, use code tags around your code as shown in red below.

Don't 'select' cells or sheets or anything in your code unless you really must. (See my handy guide, link below) Just set the value or formula of the cell. Far easier and a lot, lot quicker

Code:
    Range("N2").Formula = "=VLOOKUP(A2,[" & Mypath & LatestFile & "]'Sheetname with input data'!C1:C11,11,False)"
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

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