Subscript out of range — Unable to set open workbook as active workbook

后端 未结 1 1443
臣服心动
臣服心动 2021-01-27 02:35

I\'m trying to copy two cells B2 & C2 from the Results worksheet on every single workbook within a folder and then paste cells in a Master workboo

1条回答
  •  礼貌的吻别
    2021-01-27 02:50

    Workbooks.Open is a function, that returns a reference to the Workbook object that was opened - and you're discarding it.

    Declare a Workbook variable.

    Dim wb As Workbook
    

    Then assign it to the result of the Workbooks.Open call:

    Set wb = Workbooks.Open(Filepath & MyFile)
    

    Now wb is the workbook object you work with - whether it's active or not, doesn't matter anymore.

    wb.Worksheets("Results").Range("B2:C2").Copy
    
    'NOTE: paste to destination BEFORE closing the workbook
    
    wb.Close SaveChanges:=False
    

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