How to access a closed Excel Workbook using vlookup vba

后端 未结 2 1163
谎友^
谎友^ 2021-01-23 14:44

I\'m trying to create a Excel VBA macro that uses VLOOKUP to access a range of cells in a closed workbook. I\'m not too good at using the VBA editor, but it doesn\'t seem to sho

2条回答
  •  灰色年华
    2021-01-23 15:25

    Reference from Siddharth Rout's comments.

    The main problem in your code is this line:

    strFormula = "=VLOOKUP(currentWs.Range("B2"),'Macintosh HD:Users:myself:Documents:l[Master_Terms_Users.xlsm]Master_Terms_Users.csv'!A1:B222,2,false)"
    

    because of this code currentWs.Range("B2"). We know that you want to indicate Range("B2") of Current Sheet(same sheet). So, you can use as follow:

    strFormula = "=VLOOKUP(B2,'Macintosh HD:Users:myself:Documents:l[Master_Terms_Users.xlsm]Master_Terms_Users.csv'!A1:B‌​222,2,false)"
    

    Why? It can use just B2 because you set formula to a cell which is in the same sheet. So, it is not need to indicate the Sheet Name.

    And If you want to set a cell which is from other sheet, you need to indicate Sheet Name in that case. So, should use as follow:

    strFormula = "=VLOOKUP(" & currentWs.name & "!B2,'Macintosh HD:Users:myself:Documents:l[Master_Terms_Users.xlsm]Master_Terms_Users.csv'!A1:B222,2,false)"
    

提交回复
热议问题