My loop through folder code in excel macro is giving an error of file not found

Rishabh Jangada

New Member
Joined
Jan 6, 2020
Messages
1
Office Version
  1. 2016
  2. 2013
  3. 2011
Platform
  1. Windows
I am collating or appending data one below other using the below code to copy paste data in a sheet from different excels with same format stored in a folder

Sub LoopThroughFolder()

Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
Dim Rws As Long, Rng As Range
Set Wb = ThisWorkbook

MyDir = "Z:\MIS & ANALYTICS\RPA\"
MyFile = Dir(MyDir)
ChDir MyDir
Application.ScreenUpdating = 0
Application.DisplayAlerts = 0

Do While MyFile <> ""
Workbooks.Open (MyFile)
With Worksheets("Sheet1")
Rws = Cells(Rows.Count, "A").End(x1Up).Row
Set Rng = Range(Cells(2, 1), .Cells(Rws, 27))
Rng.Copy Wb.Worksheets("Sheets1").Cells(Rows.Count, "A").End(x1Up).Offset(1, 0)
ActiveWorkbook.Close True
End With
MyFile = Dir()
Loop

End Sub
Here MyFile = Dir(MyDir) is searching file name as "xyz.xls" whereas in folder its only "xyz" hence its giving me an error stating file not found
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,213,501
Messages
6,114,010
Members
448,543
Latest member
MartinLarkin

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