list files which do not contain sheet: TEST

Rookeby7

New Member
Joined
Jul 5, 2019
Messages
18
I have used this to list files in folder,
but now want to only list files with do not contain a worksheet "TEST" can anyone help please?

To go one step further I would move the files which do not have worksheet TEST to a new subfolder "NO TEST"

Thanks in advance

***********
Sub List_Files()
Dim MyFolder As String
Dim myFile As String
Dim a As Integer

MyFolder = "C:\CHECK"
myFile = Dir(MyFolder & "*.*")
a = 0
Do While myFile <> ""
a = a + 1
Cells(a, 1).Value = myFile
myFile = Dir
Loop

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try this

Change the folder names for yours
.
Code:
Sub List_Files()
  Dim MyFolder As String, myFile As String, a As Integer, MyFolderDes As String
  Dim wb As Workbook, sh As Worksheet, notest As Boolean, fso As Object
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  MyFolder = "[COLOR=#ff0000]C:\CHECK\[/COLOR]"
  MyFolderDes = "[COLOR=#ff0000]C:\CHECK\NO TEST\[/COLOR]"
  
  Set fso = CreateObject("scripting.filesystemobject")
  myFile = Dir(MyFolder & "*.xls*")
  a = 1
  Do While myFile <> ""
    Set wb = Workbooks.Open(MyFolder & myFile)
    notest = True
    For Each sh In wb.Sheets
      If UCase(sh.Name) = UCase("TEST") Then
        notest = False
        Exit For
      End If
    Next
    wb.Close False
    If notest = True Then
      Cells(a, 1).Value = myFile
      a = a + 1
      fso.MoveFile Source:=MyFolder & myFile, Destination:=MyFolderDes & myFile
    End If
    myFile = Dir()
  Loop
  MsgBox "End"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,749
Messages
6,180,727
Members
452,995
Latest member
isldboy

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