excelms-accessvbaole-automation

Copy an Excel Workbook Variable in VBA


Suppose you have 2 Excel.Workbook variables wbA and wbB. The first variable is a workbook you have assigned with something like Excel.Workbooks.Open.

Now my Question: How can you assign wbA to wbB so that wbB is a copy of wbA and not just a reference to it?

I tried a simple wbB = wbA but that just seems to reference wbB to wbA.

Many thanks!


Solution

  • wbA points to a workbook object in memory, it isn't an actual workbook. In the same way if you do

    Set wbB = wbA
    

    You are just creating another pointer to the same workbook that's in memory, albeit via another pointer.

    If you actually want a real copy of the workbook then you have to do just that - copy the actual workbook and set wbB to that instance instead.