Dir function is not working

RaqSpin

New Member
Joined
Oct 10, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hello, My skills in vba are limited. I've had this macro forever and now that we moved our files to SharePoint, it is not working. The macro loops thru files and then extracts line items that meet a certain criteria, everything works except it stops at the Dir function with error of Run-Time error '52': Bad file name or number. Font is in red where it crashes. I would appreciate any help.


Rich (BB code):
Option Base 1
Dim aFiles() As String, iFile As Integer

Sub LoopFoldersandSubfolders()

Dim Counter As Integer
Dim DataA(1000) As String

Dim My_Range As Range
Dim My_Cell As Variant
Dim PropNum As String
Dim sSourcePath As String
Dim sDestinationPath As String
 

WB1 = ActiveWorkbook.Name
LastRow = Range("A6000").End(xlUp).Row

Worksheets("Report").Select
PLastRow = Range("A60000").End(xlUp).Row
If PLastRow > 5 Then
Rows("6:" & PLastRow).Select
Selection.ClearContents
End If

    Dim TargetFileName As String
    Dim ObjFolder As Object
    Dim DtMo As String
    Dim DtYe As String
  
    Workbooks(WB1).Activate
    Worksheets("Options").Select
    DtMo = Range("F7").Value
        If DtMo < 10 Then
        DtMo = 0 & DtMo
        End If
    DtYe = Range("F8").Value

        TargetFileName = "\\CompanyName/Accounting\Capital Review\" & DtYe & "\" & DtMo & "." & DtYe & " Capital Workbooks\"
  

'Calls ListFilesInDirectory procedure
    iFile = 0
    ListFilesInDirectory TargetFileName

'Loops through files and performs code
    For Counter = 1 To iFile
      
        Application.StatusBar = Round(((Counter / iFile) * 100), 0) & "%"
        sName = aFiles(Counter)
        fname = Right(sName, Len(sName) - Len(TargetFileName))
      
               
        On Error Resume Next
        Set bk = Workbooks.Open(sName, ReadOnly:=False, WriteResPassword:="xxxx", UpdateLinks:=False, ignorereadonlyrecommended:=True)
        If Err.Number <> 0 Then
           MsgBox "Problems with " & sName
           Else
        On Error GoTo 0
        WB2 = ActiveWorkbook.Name
        WB2Path = ActiveWorkbook.FullName
      
      
        '****************Code Goes Here****************
            StartRow = Range("L1").End(xlDown).Row
            LastRow = Range("L60000").End(xlUp).Row
            Cells(StartRow + 1, 13).Select
            Do Until ActiveCell.Row = LastRow + 1
                If ActiveCell.Value <> "" Then

                    ActiveCell.EntireRow.Copy
                    Workbooks(WB1).Activate
                    Sheets("Report").Select
                    LastRow2 = Range("L60000").End(xlUp).Row
                    Cells(LastRow2 + 1, 1).Select
                    ActiveCell.EntireRow.PasteSpecial xlValues
                    Workbooks(WB2).Activate
                    Application.CutCopyMode = False
                  
                End If
                ActiveCell.Offset(1, 0).Select
            Loop
          
            ActiveWorkbook.Close SaveChanges:=False
          
 


        End If
        On Error GoTo 0

    Next

Exit Sub


End Sub

Private Sub ListFilesInDirectory(Directory As String)
Dim aDirs() As String, iDir As Integer, stFile As String

' Uses Dir function to find files and folders in selected Directory
' Looks for directories and builds a separate array of them
' Note that Dir returns files as well as directories when vbDirectory is specified

iDir = 0
stFile = Directory & Dir(Directory & "*.*", vbDirectory)

    Do While stFile <> Directory
        If Right(stFile, 2) = "\." Or Right(stFile, 3) = "\.." Then
     
        Else
          'Adds to global array of files if it is an Excel workbook
         If InStr(1, stFile, ".xls", vbTextCompare) > 0 Then
          iFile = iFile + 1
          ReDim Preserve aFiles(iFile)
          aFiles(iFile) = stFile
         End If
        End If
        stFile = Directory & Dir()
    Loop


'For any directories in aDirs calls self recursively
    If iDir > 0 Then
        For iDir = 1 To UBound(aDirs)
          ListFilesInDirectory aDirs(iDir) & Application.PathSeparator
        Next iDir
    End If
  
  
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Are you referring to this path?
TargetFileName = "\\CompanyName/Accounting\Capital Review\" & DtYe & "\" & DtMo & "." & DtYe & " Capital Workbooks\"

I was trying to make it generic, but it should be all "\"
 
Upvote 0
I am having the same problem with the DIR function in VBA. I have code that worked when I last edited it on 6/29/22. And now as in this example above shows, it doesn't work now. I have tried to notify Microsoft about it, but nothing seems to get if fixed.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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