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
 
The code is attempting to move the oldfolder path to become the new folder path - you need a backslash on the end of the destination:
Code:
OldFolder.Move NewFolderPath & "\"
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,215,364
Messages
6,124,507
Members
449,166
Latest member
hokjock

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