Why am i getting runtime error '91' withblock not set?

Siyanna

Well-known Member
Joined
Nov 7, 2011
Messages
1,146
Hi,

Confused to why i am getting this error

I'm just trying to loop over all excel files and copy them but i get an error in the highlighted part in bold

Thank You

Code:
Dim fso As Scripting.filesystemobject
Dim NewFolderPath As String


Sub CreateFolder()


Dim fso As Scripting.filesystemobject
Set fso = New Scripting.filesystemobject


Dim OldFolderPath As String


NewFolderPath = "C:\Users\Helal\Desktop\Excel Test\New Folder"
OldFolderPath = "C:\Users\Helal\Desktop\Excel Test\Old Folder"


    If fso.FolderExists(NewFolderPath) Then
        MsgBox "It exists"
    Else
        fso.CreateFolder (NewFolderPath)


    End If


Call CopyExcelFiles(OldFolderPath)




End Sub


Sub CopyExcelFiles(StartFolderPath As String)


Dim fil As Scripting.File
Dim OldFolder As Scripting.Folder
Dim SubFol As Scripting.Folder
 
[U][B]Set OldFolder = fso.GetFolder(StartFolderPath)[/B][/U]


For Each fil In OldFolder.Files
    If Left(fso.GetExtensionName(fil.Path), 2) = "xl" Then
       If fso.FileExists(NewFolderPath & "\" & fil.Name) Then
          MsgBox "File Already Exists"
       Else
          fil.Copy NewFolderPath & "\" & fil.Name
       End If
    End If
Next


For Each SubFol In OldFolder.SubFolders
    Call CopyExcelFiles(SubFol.Path)
Next


End Sub
 
You could use something like:
Rich (BB code):
If Left(fso.GetExtensionName(fil.Path), 2) = "xl" And Left$(fil.name, 1) <> "~" Then
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Thank you - i thought i may need to look at the path also so it dont get mixed up with the same file name in the wrong directory

Many Many Many Many Thanks


Left$<< what does the $do after the left</pre>
 
Upvote 0
Last 1 :)


aint this line of code the same as below? Set wb = Workbooks(OldFolder.path & "\" & fil.Name)


Set wb = Workbooks(fil.Name)
On Error GoTo 0
If Not wb Is Nothing and wb.Path = OldFolder Then
MsgBox "workbook is open"
end if

i thought i could do in 1 line of code but obv that dont work
 
Upvote 0
The workbooks collection is only indexed by file name, not including the path.
 
Upvote 0
It's the String version of the Left function - it returns a String rather than a Variant.
 
Upvote 0

Thank you

ahhh so if I had this

dim myvar as string

myvar = Left$(fil.name, 1)

would I need $ as I have defined Myvar as string?

where can I get a list of all these shorthand dims
 
Upvote 0
You don't ever need to use Left$ rather than Left. It's not a Type declaration character like using:
Code:
Dim myvar$
 
Upvote 0
Oh ok but I guess it's good practice to get used to it so the left function knows its working with a string

where do I get a list of the shorthand type declarations?

So I guess to be extra, with all functions used, u could do
instr$ or right$ or len$ to show that you are working with a string ?
 
Upvote 0
Hi RoryA,

Can you please advise why this move method wont work, i am just trying to move OldFolder to the NewFolder Created

I added the line of code in Bold but it says it already exists in that folder when it does not

Code:
Dim fso As Scripting.filesystemobject
Dim NewFolderPath As String


Sub CreateFolder()


Set fso = New Scripting.filesystemobject


Dim OldFolderPath As String


NewFolderPath = "C:\Users\Helal\Desktop\Excel Test\New Folder"
OldFolderPath = "C:\Users\Helal\Desktop\Excel Test\Old Folder"




    If fso.FolderExists(NewFolderPath) Then
        MsgBox "It exists"
    Else
        fso.CreateFolder (NewFolderPath)
        


    End If


Call CopyExcelFiles(OldFolderPath)




End Sub


Sub CopyExcelFiles(StartFolderPath As String)


Dim fil As Scripting.File
Dim OldFolder As Scripting.Folder
Dim SubFol As Scripting.Folder
 
Set OldFolder = fso.GetFolder(StartFolderPath)
[U][B]OldFolder.Move NewFolderPath[/B][/U]


For Each fil In OldFolder.Files
    If Left(fso.GetExtensionName(fil.Path), 2) = "xl" Then
       If fso.FileExists(NewFolderPath & "\" & fil.Name) Then
          MsgBox "File Already Exists"
       Else
          Dim wb As Workbook
          On Error Resume Next
          Set wb = Workbooks(fil.Name)
          On Error GoTo 0
             If Not wb Is Nothing Then
                MsgBox "workbook is open"
             Else
                fil.Copy NewFolderPath & "\" & fil.Name
             End If
       End If
    End If
Next


For Each SubFol In OldFolder.SubFolders
    Call CopyExcelFiles(SubFol.Path)
Next


End Sub
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,215,358
Messages
6,124,487
Members
449,165
Latest member
ChipDude83

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