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.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
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
75,362
Office Version
365
Platform
Windows
That won't get you the filename, try this.
Code:
    Filename = Split(DestFile, "\")(UBound(Split(DestFile, "\")))
 

Forum statistics

Threads
1,081,793
Messages
5,361,316
Members
400,625
Latest member
Asraful Alam

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top