Reference an excel sheet from another workbook without copying the sheet

后端 未结 2 1422
时光说笑
时光说笑 2020-12-11 12:00

Im wondering if it\'s possible to reference an excel sheet from another work book without making a copy of that sheet?

The situation : I have some very large workshe

2条回答
  •  生来不讨喜
    2020-12-11 12:20

    Yes, it is possible.

    You need to add those lines to your code:

    Dim wkb As Excel.Workbook
    Dim wks As Excel.Worksheet
    
    Set wkb = Excel.Workbooks("name_of_workbook.xlsx")
    Set wks = wkb.Worksheets("Example_1")
    

    Now, every time you want to refer to a range from this other workbook, you need to add wks. before, i.e.:

    'Printing value in cell.
    wks.Range("A1") = "x"
    
    'Selecting range.
    call wks.Range(wks.Cells(1,1), wks.Cells(2,2)).Select
    

提交回复
热议问题