jordanburch
Active Member
- Joined
- Jun 10, 2016
- Messages
- 443
- Office Version
- 2016
Hey all,
I have the following
Sub LoopThroughFolder()
Dim folderPath As String
Dim filename As String
Dim WB As Workbook
folderPath = "K:\SHARED\TRANSFER\Enterprise Wide Suspense Initiative\Monthly Suspense Recon\"
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
filename = Dir(folderPath & "*.xls*")
Do While filename <> ""
Set WB = Workbooks.Open(folderPath & filename)
'Call a subroutine here to operate on the just-opened workbook
Call Clearedto
WB.Close False
filename = Dir
Loop
End Sub
Sub Clearedto()
Dim myfile As String
Dim myfile2 As String
Dim erow As Long
Dim wb1 As Workbook
Dim ShtName1 As String
Dim ShtName2 As String
Dim ShtName3 As String
ShtName1 = "Cleared - Cleared To"
ShtName2 = "Detail"
ShtName3 = "Detail -"
Application.ScreenUpdating = False
Set wb1 = ThisWorkbook
Dim strFileName As String
Dim strFileExists As String
strFileName = filepath & myfile
strFileExists = Dir(strFileName)
erow = wb1.Sheets("Cleared - Cleared To").Cells(Rows.Count, 14).End(xlUp).Offset(1, 0).Row
Dim ShtName As String
ShtName = "Cleared - Cleared To"
If Evaluate("isref('" & ShtName & "'!A1)") Then
With WB
Sheets("Cleared - Cleared To").Select
With ActiveSheet
If .AutoFilterMode Then
If .FilterMode Then
.ShowAllData
End If
Else
If .FilterMode Then
.ShowAllData
End If
End If
End With
If Evaluate("isref('" & ShtName1 & "'!A1)") Then
' wb2.Sheets("Cleared - Cleared To").Range("q2:q1000").Value = myfile
.Sheets("Cleared - Cleared To").Range("a2:AU20000").Copy Destination:=wb1.Worksheets("Cleared - Cleared To").Cells(erow, 1)
.Close savechanges:=False
ElseIf Evaluate("isref('" & ShtName3 & "'!A1)") Then
.Sheets("Detail Lines").Range("c21000").Copy Destination:=wb1.Worksheets("CO SAR").Cells(erow, 1)
.Close savechanges:=False
ElseIf Evaluate("isref('" & ShtName2 & "'!A1)") Then
.Sheets("Detail Lines").Range("c21000").Copy Destination:=wb1.Worksheets("CO SAR").Cells(erow, 1)
.Close savechanges:=False
End If
End With
Else
'sheet doesn't exist do something else
End If
Application.ScreenUpdating = True
End Sub
It is erroring out because its looking for all file types in the sub folders. I want it to only look for .xls file types. Any ideas?
Jordan
I have the following
Sub LoopThroughFolder()
Dim folderPath As String
Dim filename As String
Dim WB As Workbook
folderPath = "K:\SHARED\TRANSFER\Enterprise Wide Suspense Initiative\Monthly Suspense Recon\"
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
filename = Dir(folderPath & "*.xls*")
Do While filename <> ""
Set WB = Workbooks.Open(folderPath & filename)
'Call a subroutine here to operate on the just-opened workbook
Call Clearedto
WB.Close False
filename = Dir
Loop
End Sub
Sub Clearedto()
Dim myfile As String
Dim myfile2 As String
Dim erow As Long
Dim wb1 As Workbook
Dim ShtName1 As String
Dim ShtName2 As String
Dim ShtName3 As String
ShtName1 = "Cleared - Cleared To"
ShtName2 = "Detail"
ShtName3 = "Detail -"
Application.ScreenUpdating = False
Set wb1 = ThisWorkbook
Dim strFileName As String
Dim strFileExists As String
strFileName = filepath & myfile
strFileExists = Dir(strFileName)
erow = wb1.Sheets("Cleared - Cleared To").Cells(Rows.Count, 14).End(xlUp).Offset(1, 0).Row
Dim ShtName As String
ShtName = "Cleared - Cleared To"
If Evaluate("isref('" & ShtName & "'!A1)") Then
With WB
Sheets("Cleared - Cleared To").Select
With ActiveSheet
If .AutoFilterMode Then
If .FilterMode Then
.ShowAllData
End If
Else
If .FilterMode Then
.ShowAllData
End If
End If
End With
If Evaluate("isref('" & ShtName1 & "'!A1)") Then
' wb2.Sheets("Cleared - Cleared To").Range("q2:q1000").Value = myfile
.Sheets("Cleared - Cleared To").Range("a2:AU20000").Copy Destination:=wb1.Worksheets("Cleared - Cleared To").Cells(erow, 1)
.Close savechanges:=False
ElseIf Evaluate("isref('" & ShtName3 & "'!A1)") Then
.Sheets("Detail Lines").Range("c21000").Copy Destination:=wb1.Worksheets("CO SAR").Cells(erow, 1)
.Close savechanges:=False
ElseIf Evaluate("isref('" & ShtName2 & "'!A1)") Then
.Sheets("Detail Lines").Range("c21000").Copy Destination:=wb1.Worksheets("CO SAR").Cells(erow, 1)
.Close savechanges:=False
End If
End With
Else
'sheet doesn't exist do something else
End If
Application.ScreenUpdating = True
End Sub
It is erroring out because its looking for all file types in the sub folders. I want it to only look for .xls file types. Any ideas?
Jordan