Getting "Subscript out of Range" error while trying to open an random workbook which is already open

Koranga

New Member
Joined
Mar 30, 2017
Messages
7
Hello All,

I am new VBA and Macros. I am trying to open a workbook and if it is already open just activating that workbook. But I am getting "Subscript out of Range" error again. Below is the snippet of my code.

Sub Activating()

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
'Just Activate the workbook
Workbooks(DestFile).Activate 'Here I am getting Subscript of of Range Error
End If
End Sub

Function Isworkbookopen(Filename As String)

Dim ff As Long, ErrNo As Long
Dim wkb As Workbook
Dim nam As String
wbname = Filename
On Error Resume Next


ff = FreeFile()
Open Filename For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0
Select Case ErrNo
Case 0: Isworkbookopen = False
Case 70: Isworkbookopen = True
Case Else: Error ErrNo
End Select


End Function

Here the file that I am trying to open is already open, name of the file can vary so it is also difficult to create a object of workbook class for that open workbook.

It would be great if anyone could help me out, as I had tried a lot and finally posting this question.
Thanks in advance.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You'll need to remove the file path from DestFile.
 
Upvote 0
I tried that also.

Filename = Split(DestFile, "")(UBound(Split(DestFile, "")))
Workbook(Filename).Close

But still getting the same error.
 
Upvote 0
That won't get you the filename, try this.
Code:
    Filename = Split(DestFile, "\")(UBound(Split(DestFile, "\")))
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top