I have a VBA sub which checks if a workbook is open, and then copy and paste data from another workbook into it. This is done automatically in an application.ontime loop. I usually leave it minimized while I work on other things and check back on the Excel data copy/pasting occasionally. From time to time Excel will pop up a box saying that the destination workbook is open, and asks if I will want to reopen it. Many sub runs were missed between the time it pops up and the time that I check the workbook.
Code on where the problem occurs:
If dataset Is Nothing Then
Set dataset = Workbooks.Open("C:\Users\Ken\Desktop\Df.xlsx")
Else
Set dataset = Workbooks("Df.xlsx")
End If
What can I do to have Excel automatically handle this situation?
Secondary question: I'm suspecting that it may have something to do with the fact that I may have touched the workbook between runs without saving it, causing Excel to think there are changes since the last save. Can someone comment if this could be a reason?
first check if workbook is already opened. If not open try to open it. This can be done in below code. Refer the link