Excel VBA requiring file extension for workbook reference in some systems

前端 未结 1 674
长情又很酷
长情又很酷 2020-12-10 22:29

I have a simple excel VBA that is referring multiple files and copying information across to a master before processing. While building this on my own system a workbook refe

相关标签:
1条回答
  • 2020-12-10 22:54

    If the key.xlsx file is saved on both systems, including the file extension when referring to the Workbook objects is the safer option because of Windows hide extensions setting:


    The Workbooks Collection Object

    If the hide extensions setting is not in effect (meaning that extensions are indeed displayed in Windows), you must include the xls extension when you reference a workbook in the Workbooks collection. For example, if you have open a workbook named Book1.xls, you must use

    Workbooks("Book1.xls").Activate

    rather than

    Workbooks("Book1").Activate

    to refer to the Book1 workbook. The second line of code above, without the xls extension, will fail with an error 9, Subscript Out Of Range, because there is no workbook with the name Book1. If the hide extensions setting is effect, you can omit the xls extension and use either of the following lines of code.

    Workbooks("Book1").Activate

    Workbooks("Book1.xls").Activate

    These lines of code assume that you do not have open both an unsaved workbook with the name Book1 and a saved workbook with the name Book1.xls. With the hide extensions setting enabled (so that extensions are hidden in Windows), the two lines of code above are functionally equivalent. As a matter of good programming practice, you should always include the xls extension in a workbook name. This ensures that you reference the correct workbook regardless of the value of the hide extensions property.



    More details from cpearson.com for File Extensions And Their Implications In VBA Coding

    0 讨论(0)
提交回复
热议问题