+ Reply to Thread
Results 1 to 23 of 23

Opening latest version of file and then doing vlookup in that file

  1. #1
    Registered User
    Join Date
    02-03-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    41

    Opening latest version of file and then doing vlookup in that file

    Hi All,

    I want to open the latest version of a file and then do a vlookup in that latest version.

    I know how to open the latest file, but once it is open, how do I do a vlookup without specifying that file name? So that I don't have to change the filename in the vlookup all the time?

    eg. if VS_10.12.2012.xlsx is the latest version, I want to vlookup in range C10:C20 on tab VS_1.

    A new version of the file will be created every week, but the tabs and cells will all remain as they are.

    Your help much appreciated!!!

    Regards

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    You will need to change the VLOOKUP for the new workbook/worksheet.

    I suppose you could use code for that, or even use code to grab the values you want from the file.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    02-03-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Opening latest version of file and then doing vlookup in that file

    Thanks Norie,

    Surely if one can ask Excel to open the latest version, you could allocate a reference in the code to that file that was just opened to let Excel identify that file as the one to look in?

    And what would the code you referred to look like?

    Thanks!

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Yes but you would still need the filename for any formulas.

    What would the code look like?

    I don't really know exactly what you are doing and there's not too many details you go on, so kind of hard to tell.

  5. #5
    Registered User
    Join Date
    02-03-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Opening latest version of file and then doing vlookup in that file

    Hi Norie,

    Here is the code I wrote to open the latest file:

    Dim FileSys As FileSystemObject
    Dim objFile As File
    Dim myFolder
    Dim strFilename As String
    Dim dteFile As Date

    'set path for files - change for your folder
    Const myDir As String = "H:\Trackers\2012\VS\"

    'set up filesys objects
    Set FileSys = New FileSystemObject
    Set myFolder = FileSys.GetFolder(myDir)


    'loop through each file and get date last modified. If largest date then store Filename
    dteFile = DateSerial(1900, 1, 1)
    For Each objFile In myFolder.Files
    If objFile.DateLastModified > dteFile Then
    dteFile = objFile.DateLastModified
    strFilename = objFile.Name
    End If
    Next objFile
    Workbooks.Open myDir & "\" & strFilename

    Set FileSys = Nothing
    Set myFolder = Nothing

    From there I need to tell Excel to look in this file and do the vlookup which is currently:

    =INDEX('[2012 VS_07.11.2012.xlsx]VS UK'!$C$27:$C$39,MATCH(VLOOKUP(VLOOKUP(D12&E12,'2013'!$G$3:$J$119,4,FALSE),'[2012 VS_07.11.2012.xlsx]VS UK'!$C$27:$C$39,1),'[2012 VS_07.11.2012.xlsx]VS UK'!$C$27:$C$39,0)+1)

    The bold text is the vlookup I need to look into whatever the latest file version opened is

    Thanks!
    Last edited by [email protected]; 12-11-2012 at 08:00 AM.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Is it just one formula?

    What is it you're looking up?

  7. #7
    Registered User
    Join Date
    02-03-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Opening latest version of file and then doing vlookup in that file

    Yeah it's just that last formula. I'm looking at the lower and higher value next up in a table.

    eg. if the current value is 75% the lower value could be 70% and the higher value 80% in a table in the latest file

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Opening latest version of file and then doing vlookup in that file

    Try something like this, change the destination of the formula as required.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    02-03-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Opening latest version of file and then doing vlookup in that file

    Thanks Norie,

    That seems to work - but how do I place the formula in the cell I want? ie. what do I put in place of ThisWorkbook.?

    Regards

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Opening latest version of file and then doing vlookup in that file

    ThisWorkbook refers to the workbook the code is in, if that's not correct it change it to Workbooks(<nameofWB>).

    Change the sheet name and range similarly.

  11. #11
    Registered User
    Join Date
    02-03-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Opening latest version of file and then doing vlookup in that file

    Have tried:

    strFormula = "=INDEX('[" & wbVS.Name & "]VS UK'!$C$27:$C$39,MATCH(VLOOKUP(VLOOKUP(D12&E12,'2013'!$G$3:$J$119,4,FALSE),'" & wbVS.Name & "]VS UK'!$C$27:$C$39,1),'" & wbVS.Name & "]VS UK'!$C$27:$C$39,0)+1)"

    Workbooks(<2013 Shareshift_Yield Preferences.xlsm>).Worksheets("UK").Range("G12").Formula = strFormula

    And it doesn't seem to work..

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Opening latest version of file and then doing vlookup in that file

    I didn't really mean to use < and >.

    <nameofWB> is a 'placeholder'.

    Anyway, try this.
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    02-03-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Opening latest version of file and then doing vlookup in that file

    Tried that, unfortunately doesn't work ;(

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Opening latest version of file and then doing vlookup in that file

    'doesn't work'?

    In what way?

    Incorrect result?

    Code errors?

    Worksheet errors?

    By the way is the workbook 2013 Shareshift_Yield Preferences.xlsm open?

  15. #15
    Registered User
    Join Date
    02-03-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Opening latest version of file and then doing vlookup in that file

    It says " Runtime Error '1004' "

    Here is the total coding I have thus far, all works okay until the last line:

    Sub Update2()


    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    Msg = "This Macro Updates Information in Your File."
    Msg = Msg & vbCrLf & vbCrLf & "Are you Ready to Continue?"
    DialogStyle = vbYesNo + vbExclamation + vbDefaultButton2
    Title = "File Update"
    Response = MsgBox(Msg, DialogStyle, Title)
    If Response = vbYes Then
    Else
    End
    End If



    'Sets the MacroFrance file as MacroFile to be later activated throughout the macro
    Set MacroFile = ActiveWorkbook


    Dim FileSys As FileSystemObject
    Dim objFile As File
    Dim myFolder
    Dim strFilename As String
    Dim dteFile As Date

    'set path for files - change for your folder
    Const myDir As String = "H:\Trackers\2012\VS\"

    'set up filesys objects
    Set FileSys = New FileSystemObject
    Set myFolder = FileSys.GetFolder(myDir)


    'loop through each file and get date last modified. If largest date then store Filename
    dteFile = DateSerial(1900, 1, 1)
    For Each objFile In myFolder.Files
    If objFile.DateLastModified > dteFile Then
    dteFile = objFile.DateLastModified
    strFilename = objFile.Name
    End If
    Next objFile
    Workbooks.Open myDir & "\" & strFilename

    Set FileSys = Nothing
    Set myFolder = Nothing

    'set latest file to use formula
    Set wbVS = Workbooks.Open(myDir & "\" & strFilename)

    strFormula = "=INDEX('[" & wbVS.Name & "]VS UK'!$C$27:$C$39,MATCH(VLOOKUP(VLOOKUP(D12&E12,'2013'!$G$3:$J$119,4,FALSE),'" & wbVS.Name & "]VS UK'!$C$27:$C$39,1),'" & wbVS.Name & "]VS UK'!$C$27:$C$39,0)+1)"

    Workbooks("2013 Shareshift_Yield Preferences.xlsm").Worksheets("UK").Range("G12").Formula = strFormula

    Yes the workbook is open

    Thanks again

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Opening latest version of file and then doing vlookup in that file

    What's the name of the file when you get the error?

    PS You don't need this:
    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    02-03-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Opening latest version of file and then doing vlookup in that file

    Okay - it's working - there were some characters missing in the formula (a [ and a ')

    My NEXT question is:

    How to remove the static reference "2013 Shareshift......xlsx" so that if someone saves it as another filename or date it will still work?

  18. #18
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Opening latest version of file and then doing vlookup in that file

    Characters missing?

    It worked for me.

    Anyway, do you mean you want to replace the formula with it's result?

  19. #19
    Registered User
    Join Date
    02-03-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Opening latest version of file and then doing vlookup in that file

    I want to remove the reference "2013 Shareshift...etc.") and do similar as we have done below with the "VS....etc." file:

    Set wbVS = Workbooks.Open(myDir & "\" & strFilename)

    So...when I open the 2013 Shareshift file, I would like it to refer to itself no matter what the file name is saved as (ie. if saved to "2013 Shareshift Latest" or something) and then reopened, the formulas will still work



    Replace

  20. #20
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Opening latest version of file and then doing vlookup in that file

    You've lost me.

    What file does the formula reference and which file is it going in?

  21. #21
    Registered User
    Join Date
    02-03-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Opening latest version of file and then doing vlookup in that file

    Haha! Sorry!

    So, this is the working code thus far:

    Sub Update2()


    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    Msg = "This Macro Updates Information in Your File."
    Msg = Msg & vbCrLf & vbCrLf & "Are you Ready to Continue?"
    DialogStyle = vbYesNo + vbExclamation + vbDefaultButton2
    Title = "File Update"
    Response = MsgBox(Msg, DialogStyle, Title)
    If Response = vbYes Then
    Else
    End
    End If



    Dim FileSys As FileSystemObject
    Dim objFile As File
    Dim myFolder
    Dim strFilename As String
    Dim dteFile As Date

    'set path for files - change for your folder
    Const myDir As String = "H:\Trackers\2012\VS\"

    'set up filesys objects
    Set FileSys = New FileSystemObject
    Set myFolder = FileSys.GetFolder(myDir)


    'loop through each file and get date last modified. If largest date then store Filename
    dteFile = DateSerial(1900, 1, 1)
    For Each objFile In myFolder.Files
    If objFile.DateLastModified > dteFile Then
    dteFile = objFile.DateLastModified
    strFilename = objFile.Name
    End If
    Next objFile
    Workbooks.Open myDir & "\" & strFilename

    Set FileSys = Nothing
    Set myFolder = Nothing

    'set latest file to use formula
    Set wbVS = Workbooks.Open(myDir & "\" & strFilename)

    strFormula = "=INDEX('[" & wbVS.Name & "]VS UK'!$C$27:$C$39,MATCH(VLOOKUP(VLOOKUP(D12&E12,'2013'!$G$3:$J$119,4,FALSE),'" & wbVS.Name & "]VS UK'!$C$27:$C$39,1),'" & wbVS.Name & "]VS UK'!$C$27:$C$39,0)+1)"

    Workbooks("2013 Shareshift_Yield Preferences.xlsm").Worksheets("UK").Range("G12").Formula = strFormula

    The code in bold is what I need to change similarly to that in italics:

    - The bold workbook is the one that will be opened first (and saved as a new name possibly), so I would like it to rund the code even if the filename changes

    Hope that was clear

  22. #22
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Opening latest version of file and then doing vlookup in that file

    If you are opening a workbook and placing a formula in that workbook the only references that workbook you don't need the workbook name in the formula.

  23. #23
    Registered User
    Join Date
    02-03-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Opening latest version of file and then doing vlookup in that file

    Aha - changed it thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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