Vlook is not working using VBScript

  • Hello,


    I need some help in Vlookup using two excel sheets. The code works fine if I copy the excel files on the same location of the vbScript but if I put the excel sheets to a different location, it does not work. Looks like I'm failing to specify the path in the lookup function and hence it is failing to compare. Here is the code:


    '################################################
    On error resume next



    Set obj = CreateObject("wscript.shell")
    strFilePath1 = obj.CurrentDirectory & "\Temp\ALL Users1.xlsx"
    strFilePath2 = obj.CurrentDirectory & "\Output\ALL Users2.xlsx"



    Set oXL = CreateObject("Excel.Application")
    Set oWB1 = oXL.Workbooks.Open(strFilePath1)
    Set oWB2 = oXL.Workbooks.Open(strFilePath2)

    oXL.Visible = false

    Set oS2 = oWB2.Worksheets("Great") ' using sheet # 2 named as Great
    Set oS1 = oWB1.Worksheets("Cool") ' using sheet # 2 named as Cool

    oS2.Columns(6).Insert
    oS2.Cells(1,6) = "Number"
    oS2.Cells(1,6).Font.Bold = True



    oS2.Range("F2:F" & oS2.UsedRange.Rows.Count).Formula = "=VLOOKUP(C2, ' [strFilePath1] oS1 ' ! $A:$B,2,FALSE) "
    ' it cannot recognize strFilePath1 as path and oS1 as worksheet as above


    oS2.Range("F2:F" & oS2.UsedRange.Rows.Count).Formula = oS2.Range("F2:F" & oS2.UsedRange.Rows.Count).Value


    oWB1.Close
    oWB2.Save
    oWB2.Close
    oXL.Quit


    Basically need a way so that the lookup function can understand the path to do the comparing. Would appreciate your help.

    [ATTACH=JSON]{"data-align":"none","data-size":"full","title":"Excel.PNG","data-attachmentid":1208922}[/ATTACH]

  • Hello,


    Basically you are using a couple of workbooks ... for which you do need to specify the Full Name ...


    which means something like : "C:\MyDocuments\Excel\Work"& "\Temp\ALL Users1.xlsx"


    This will allow you to correctly define both variables : strFilePath1 and strFilePath2


    Hope this will help

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Hi Carim,


    You are correct. If I manually define the exact path it works. But the script will go to different users (C:\Users\%username%\Desktop\ScriptFolderName) and based on the folder location \Temp & \Output will be inside the parent folder. Problem seems to be in =VLOOKUP(C2, ' [strFilePath1] oS1, it is not detecting strFilePath1 & oS1 as fileObject.


    Hence need some assistance how to call this so that the vlookup knows this is a file not a path of a file.


    Make sense?

  • Hello,


    Have you defined, in your macro, both your variables : strFilePath1 and strFilePath2 As String ?

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • <moved to Excel General>

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Hello,


    You need to concatenate all your Strings ...


    Code
    oS2.Range("F2:F" & oS2.UsedRange.Rows.Count).Formula = "=VLOOKUP(C2, '"& strFilePath1 & oS1 &" ' ! $A:$B,2,FALSE) "


    Hope this will help

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Hello,


    In these circumstances, the only real solution is Testing ...


    And given the fact you are dealing with many different workbooks, you have to carry out several tests to find out how to fix your formula ...

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!