Sub or Function not defined

H

hurlbut777

I am trying to run the following code but get this error:

Compile Error: Sub or Function not defined.

Any ideas on how to fix this or why it is occurring would be greatly
appreciated.

Sub DeleteABC()
Dim FSO As Scripting.FileSystemObject
Dim FF As Scripting.Folder
Set FSO = New Scripting.FileSystemObject
Set FF = FSO.GetFolder("C:\Test") '<<< Change Folder

DoOneFolder FF, FSO
End Sub

Sub DoOneFolder(FF As Scripting.Folder, FSO As
Scripting.FileSystemObject)
Dim SubF As Scripting.Folder
Dim F As Scripting.File
For Each F In FF.Files
If StrComp(Left(F.Name, 3), "ABC", vbTextCompare) = 0 Then
Kill F.Path
End If
Next F
For Each SubF In FF.SubFolders
DoOneFolder SubF, FSO
Next SubF
End Sub
 
J

Jim Rech

Do you have a reference set to the Microsoft Scripting Runtime under Tools,
references in the VBE?

--
Jim
|I am trying to run the following code but get this error:
|
| Compile Error: Sub or Function not defined.
|
| Any ideas on how to fix this or why it is occurring would be greatly
| appreciated.
|
| Sub DeleteABC()
| Dim FSO As Scripting.FileSystemObject
| Dim FF As Scripting.Folder
| Set FSO = New Scripting.FileSystemObject
| Set FF = FSO.GetFolder("C:\Test") '<<< Change Folder
|
| DoOneFolder FF, FSO
| End Sub
|
| Sub DoOneFolder(FF As Scripting.Folder, FSO As
| Scripting.FileSystemObject)
| Dim SubF As Scripting.Folder
| Dim F As Scripting.File
| For Each F In FF.Files
| If StrComp(Left(F.Name, 3), "ABC", vbTextCompare) = 0 Then
| Kill F.Path
| End If
| Next F
| For Each SubF In FF.SubFolders
| DoOneFolder SubF, FSO
| Next SubF
| End Sub
|
|
 
H

hurlbut777

Jim, I believe so. I went to tools>references and the Microsoft Scripting
Runtime box is checked.
 
B

Bob Phillips

Change it to late bound

Sub DeleteABC()
Dim FSO As Object
Dim FF As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FF = FSO.GetFolder("C:\Test") '<<< Change Folder

DoOneFolder FF, FSO
End Sub

Sub DoOneFolder(FF As Object, FSO As Object)
Dim SubF As Object
Dim F As Object
For Each F In FF.Files
If StrComp(Left(F.Name, 3), "ABC", vbTextCompare) = 0 Then
Kill F.Path
End If
Next F
For Each SubF In FF.SubFolders
DoOneFolder SubF, FSO
Next SubF
End Sub
 
H

hurlbut777

Bob, made changes and code worked. For my own benefit, what is late bound
and why did it make a difference?
 
J

Jim Rech

It shouldn't have made any difference. Your original code (with the
reference added) worked fine for me.

--
Jim
| Bob, made changes and code worked. For my own benefit, what is late bound
| and why did it make a difference?
|
| "Bob Phillips" wrote:
|
| > Change it to late bound
| >
| > Sub DeleteABC()
| > Dim FSO As Object
| > Dim FF As Object
| > Set FSO = CreateObject("Scripting.FileSystemObject")
| > Set FF = FSO.GetFolder("C:\Test") '<<< Change Folder
| >
| > DoOneFolder FF, FSO
| > End Sub
| >
| > Sub DoOneFolder(FF As Object, FSO As Object)
| > Dim SubF As Object
| > Dim F As Object
| > For Each F In FF.Files
| > If StrComp(Left(F.Name, 3), "ABC", vbTextCompare) = 0 Then
| > Kill F.Path
| > End If
| > Next F
| > For Each SubF In FF.SubFolders
| > DoOneFolder SubF, FSO
| > Next SubF
| > End Sub
| >
| >
| > --
| > __________________________________
| > HTH
| >
| > Bob
| >
| > | > >I am trying to run the following code but get this error:
| > >
| > > Compile Error: Sub or Function not defined.
| > >
| > > Any ideas on how to fix this or why it is occurring would be greatly
| > > appreciated.
| > >
| > > Sub DeleteABC()
| > > Dim FSO As Scripting.FileSystemObject
| > > Dim FF As Scripting.Folder
| > > Set FSO = New Scripting.FileSystemObject
| > > Set FF = FSO.GetFolder("C:\Test") '<<< Change Folder
| > >
| > > DoOneFolder FF, FSO
| > > End Sub
| > >
| > > Sub DoOneFolder(FF As Scripting.Folder, FSO As
| > > Scripting.FileSystemObject)
| > > Dim SubF As Scripting.Folder
| > > Dim F As Scripting.File
| > > For Each F In FF.Files
| > > If StrComp(Left(F.Name, 3), "ABC", vbTextCompare) = 0 Then
| > > Kill F.Path
| > > End If
| > > Next F
| > > For Each SubF In FF.SubFolders
| > > DoOneFolder SubF, FSO
| > > Next SubF
| > > End Sub
| > >
| > >
| >
| >
| >
 
B

Bob Phillips

Late bound just means that the code library is interrogated at run time
(each and every time it uses a method or property from that library), rather
than setting up the entry points at compile time. So it is inherently less
efficient, but it saves having to set references, and more importantly,
caters for different versions of the code libraries.

But as Jim said, if you had the reference set, it shouldn't have made any
difference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top