I have an error mentioned in the title. To me it looks ok especially when I debug.print it seems ok and opens that file as well.




Sub TensionTime()
Dim sPath As String
Dim sFile As String
Dim sDir As String
Dim oLB As Workbook
Dim namee As String
Dim aveht As Range
Dim avest1 As Range
Dim avest2 As Range
Dim filedate As Date
 
sPath = "J:\WHP Facility Folder\Arch 3 a - 909 - Projects Raw Data\Winding\"
sDir = FileSystem.Dir(sPath & "*" & namee & "*" & ".csv", vbNormal)

Do Until LenB(sDir) = 0
   Debug.Print sDir
    Set oLB = Workbooks.Open(sPath & sDir)
    
    ' -- Do Stuff
    Workbooks(sDir).Worksheets(1).Activate
   
        Range("F2").Select
    Set aveht = Range(Selection, Selection.End(xlDown))
    
    
    Workbooks("Date vs Spool Tension").Worksheets(1).Activate
    
    Range("D16") = WorksheetFunction.AverageIf(aveht, ">0")
    filedate = FileDateTime(sDir)
    
     Exit Sub
    
    
    '=AVERAGEIFS(F5:AM5,F5:AM5,"> 0",F$2:AM$2,"total")
    '
    If oLB.Saved = False Then oLB.Save
    oLB.Close False
    sDir = Dir$
Loop





End Sub