+ Reply to Thread
Results 1 to 4 of 4

VBA code to get the latest number

  1. #1
    Registered User
    Join Date
    02-01-2016
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2007
    Posts
    70

    VBA code to get the latest number

    Hi,

    I am trying to open most latest & greatest .pdf file from a folder that has lets say, 123456AA.pdf, 123456AB.pdf, 123456AC.pdf, 123456AD,......123456BW.pdf

    I need to open the 123456BW.pdf. I have VBA code as follows.



    Sub OpenPDFdoc()

    Dim Folder As String

    Folder = Left(ActiveCell.Value, 3)

    On Error GoTo 1

    ActiveWorkbook.FollowHyperlink "U:\Engineering\Design\DWG\" & Folder & "\" & ActiveCell.Value & ".pdf", NewWindow:=True

    Exit Sub

    1: MsgBox "File Cannot Be Opened"

    End Sub



    With this code I can open a specific folder. But I want to open the latest file and open it.

    Any help on vba would be highly appreciated.

    Thanks.

    Mashiul

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: VBA code to get the latest number

    You will notice that a lot of this code is commented out. DIR gets the specified files from a directory (in this case, anything ending in .pdf). What's more, it does it alphabetically. so the last file found is the last in the directory alphabetically. Therefore the comparison isn't necessary, Whatever the value of FileName is when the loop is done is the last filename with a .pdf extension.
    Please Login or Register  to view this content.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: VBA code to get the latest number

    I'm going to take that back. When the loop is done, FileName = "". So uncomment the code, it's still overkill since all you need to do is record filename before the last dir.

  4. #4
    Registered User
    Join Date
    02-01-2016
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2007
    Posts
    70

    Re: VBA code to get the latest number

    Hi dflak,

    Thanks very much for your code. It worked perfectly.

    Mashiul

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VB code Get 2 latest file names from Directory
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 58
    Last Post: 01-10-2015, 06:20 PM
  2. [SOLVED] Counting the latest row of a number?
    By figeroa in forum Excel General
    Replies: 6
    Last Post: 10-19-2014, 01:38 PM
  3. Replies: 9
    Last Post: 11-28-2013, 05:20 PM
  4. [SOLVED] Adding up the latest 5 number automatically
    By kleevin in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-25-2013, 10:25 PM
  5. Lookup Latest number
    By Abrielle in forum Excel General
    Replies: 4
    Last Post: 04-04-2012, 02:58 PM
  6. How to pick up latest number from spread sheet
    By vikkam in forum Excel General
    Replies: 4
    Last Post: 09-16-2009, 02:19 PM
  7. [SOLVED] code not unique find latest date
    By Barbara Wiseman in forum Excel General
    Replies: 3
    Last Post: 12-11-2005, 04:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1