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

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,278
Office Version
  1. 365
Platform
  1. Windows
You'll need to remove the file path from DestFile.
 

Koranga

New Member
Joined
Mar 30, 2017
Messages
7
I tried that also.

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

But still getting the same error.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,278
Office Version
  1. 365
Platform
  1. Windows
That won't get you the filename, try this.
Code:
    Filename = Split(DestFile, "\")(UBound(Split(DestFile, "\")))
 

Watch MrExcel Video

Forum statistics

Threads
1,129,366
Messages
5,635,847
Members
416,886
Latest member
coreyalaurence37

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
Top