Hello All,
I have some workbooks open (manually), now I am trying to close 1 of them using VBA function Workbooks.Close().
Somewhere I found a way of doing it
Workbooks("FileName").Close
but somehow for me its not working and throwing error which says "Subscript out of Range".
The code that I am using is:
Sub Closing()
Dim DestFile As String
Dim wb As Workbook
DestFile = Application.GetOpenFilename()
ret = Isworkbookopen(DestFile)
If ret = False Then
'open file
Set wb = Workbooks.Open(DestFile)
Else
'Close the file
Workbooks(DestFile).Close SaveChanges:=True 'Here I am getting Subscript of of Range Error
End If
End Sub
Here I am selecting a file to open and if the file is already open then instead of opening I am closing the opened file.
The object for the workbooks are not available as they were opened manually, so I am not able to close a specific workbook.
Could anyone please let me know is there any other way for doing the same(closing the file) or am I missing something.
Thanks in advance.
I have some workbooks open (manually), now I am trying to close 1 of them using VBA function Workbooks.Close().
Somewhere I found a way of doing it
Workbooks("FileName").Close
but somehow for me its not working and throwing error which says "Subscript out of Range".
The code that I am using is:
Sub Closing()
Dim DestFile As String
Dim wb As Workbook
DestFile = Application.GetOpenFilename()
ret = Isworkbookopen(DestFile)
If ret = False Then
'open file
Set wb = Workbooks.Open(DestFile)
Else
'Close the file
Workbooks(DestFile).Close SaveChanges:=True 'Here I am getting Subscript of of Range Error
End If
End Sub
Here I am selecting a file to open and if the file is already open then instead of opening I am closing the opened file.
The object for the workbooks are not available as they were opened manually, so I am not able to close a specific workbook.
Could anyone please let me know is there any other way for doing the same(closing the file) or am I missing something.
Thanks in advance.