scan for only excel files in folder

jordanburch

Active Member
Joined
Jun 10, 2016
Messages
440
Office Version
  1. 2016
Hey Guys,

I am trying to get this code to scan and open only .XLS or .XLSX files. It is opening PDF as well and putting them in their own excel file which i dont want. Any help is greatly appreciated! See below.


Filepath = "K:\SHARED\TRANSFER\Enterprise Wide Suspense Initiative\DRP\" & fn2 & fn2 & " " & "DRP" & "\" & fn2 & fn2 & "-" & fn3 & " " & "Reporting Cycle" & "\"
'K:\SHARED\TRANSFER\Enterprise Wide Suspense Initiative\DRP\2020 DRP\2020-05 Reporting Cycle
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0 And MyFile <> "suspense automation.xlsm"

erow = wb1.Sheets("DRP").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Set wb2 = Workbooks.Open(Filepath & MyFile)
With wb2


Dim ShtName As String
ShtName = "Sheet 1"
If Evaluate("isref('" & ShtName & "'!A1)") Then
'sheet exists do something
Else
'sheet doesn't exist do something else
End If
If Evaluate("isref('" & ShtName1 & "'!A1)") Then
.Sheets("Details").AutoFilterMode = False
.Sheets("Details").Range("d2:af1000").Copy Destination:=wb1.Worksheets("DRP").Cells(erow, 1)
.Close savechanges:=False
ElseIf Evaluate("isref('" & ShtName3 & "'!A1)") Then
.Sheets("Detail - DRP").AutoFilterMode = False
.Sheets("Detail - DRP").Range("c2:af1000").Copy Destination:=wb1.Worksheets("DRP").Cells(erow, 1)
.Close savechanges:=False

ElseIf Evaluate("isref('" & ShtName2 & "'!A1)") Then
.Sheets("Detail").AutoFilterMode = False
.Sheets("Detail").Range("c2:af1000").Copy Destination:=wb1.Worksheets("DRP").Cells(erow, 1)
.Close savechanges:=False

End If
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
How about
VBA Code:
MyFile = Dir(Filepath & "*.xls*")
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,483
Members
448,967
Latest member
visheshkotha

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