Runtime error 9 Subscript out of range

D Willett
SilverLounger
Posts: 1629
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Runtime error 9 Subscript out of range

Post by D Willett »

Hello... it's been a while.

It seems I have a dodgy file in one of my folders and I need to locate it from thousands of pdf's (needle in a haystack.

This following code Lists Directories:

Code: Select all

Function ListDirectories(Path)
'On Error Resume Next


    Dir1.Path = Path
    For i = 0 To Dir1.ListCount - 1
        Dir1.ListIndex = i
        Categories = Dir1.List(i)
        If Len(Dir(Categories, vbDirectory)) > 1 Then    ' Hidden, Read-Only, System files ommited
                
            a(j) = Categories
            TreeView1.Nodes.Add "x" & Counter, tvwChild, "x" & j, Dir(a(j), vbDirectory)
            j = j + 1
        End If
    Next i
    Counter = Counter + 1
    If a(Counter) = "" Then
        'MsgBox ("Over")
    Else
        ListDirectories (a(Counter))
    End If
End Function
And bombs out because I either have a file with a character vb dowsn't like or something else. I need to locate that file via code rather than inspecting each file. Can someone add error coding to this routine to help me to find the corruptio?

Greatly Appreciated
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 69475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Runtime error 9 Subscript out of range

Post by HansV »

Hi Dave,

That code only lists folders, not files. How would you use it to find a file?

What is Dir1?
Regards,
Hans

D Willett
SilverLounger
Posts: 1629
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Runtime error 9 Subscript out of range

Post by D Willett »

Hi Hans, I hope you are well :-)

Sorry, it does list folders and Dir1 is a DirListBox and I'm assuming within one of the folders is a corrupt 'File'. All folders are shown in a Treeview1 with nodes and branches.
Does this help?
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 69475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Runtime error 9 Subscript out of range

Post by HansV »

Hi Dave, I'm fine; I hope you are well too.

Which line of code causes the error? Does hovering the mouse pointer over variables after clicking Debug provide useful information?
Regards,
Hans

D Willett
SilverLounger
Posts: 1629
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Runtime error 9 Subscript out of range

Post by D Willett »

I'm really good thanks Hans.

I'm just stepping through and watching the output in the immediate window.

It's bombed and the last line written in the immediate window is:
L:\mmpdf\MMDocs\Longton - Staff - Tools - Training\Staff\Staff No Longer Employed\Burgess J\General
There's nothing odd in that folder.

If I hover over 'Categories' in the code it shows:
L:\mmpdf\MMDocs\Menu Pricing and Contractual Information\Viz...
And I can't see anymore than that as the length of the location is too long.
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 69475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Runtime error 9 Subscript out of range

Post by HansV »

When the code is paused, activate the Immediate window (Ctrl+G), type

Code: Select all

? Categories
and press Enter. That should give you the full path. Does that provide a clue?
Regards,
Hans

D Willett
SilverLounger
Posts: 1629
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Runtime error 9 Subscript out of range

Post by D Willett »

It seems the error was in :

L:\mmpdf\MMDocs\Menu Pricing and Contractual Information\Vizion\Old Docs\

As the files in there are for posterity I have deleted them and I now have no Error 9 Subscript.

I really don't know what caused this as the documents in that folder are years old.
I'll monitor this going forward and see how things work out.

As ever, very greatful Hans, glad you're there when help is needed :clapping:
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 69475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Runtime error 9 Subscript out of range

Post by HansV »

I'm not going anywhere these days... :grin:
Regards,
Hans

D Willett
SilverLounger
Posts: 1629
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Runtime error 9 Subscript out of range

Post by D Willett »

This has happened again today Hans, I've looked at the return for ? Categories and checked both the folder and its contents, I can't see anything untowards??
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 69475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Runtime error 9 Subscript out of range

Post by HansV »

What was the line that caused the error?
Regards,
Hans

D Willett
SilverLounger
Posts: 1629
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Runtime error 9 Subscript out of range

Post by D Willett »

a(j) = Categories
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 69475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Runtime error 9 Subscript out of range

Post by HansV »

The error suggests that j has become larger than the upper bound of the array a.
How has a been defined?
Regards,
Hans

D Willett
SilverLounger
Posts: 1629
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Runtime error 9 Subscript out of range

Post by D Willett »

Private WithEvents m_WebControl As VBControlExtender

Dim a(1000) As String
Public j As Integer
Public Counter As Integer
Public FolderName As String

An j is returning 1001

:grin:
Cheers ...

Dave.

D Willett
SilverLounger
Posts: 1629
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Runtime error 9 Subscript out of range

Post by D Willett »

Should I just add an extra 0
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 69475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Runtime error 9 Subscript out of range

Post by HansV »

For example, yes!
Regards,
Hans

D Willett
SilverLounger
Posts: 1629
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Runtime error 9 Subscript out of range

Post by D Willett »

It works... It's surprising how many documents we keep for business which is well over 1000, however it is the folders causing the issue which seems we have reached 1000.

j exceeded a by 1 and set the whole problem off. I am pretty certain we will never reach 10,000, not in my lifetime anyway ... :rofl:

Thans again Hans.. :thumbup:
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 69475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Runtime error 9 Subscript out of range

Post by HansV »

Make sure to monitor the maximum value of j from time to time - you might be surprised how fast it goes up.
Regards,
Hans