VBA - DO while and filename contains*

30136353

Board Regular
Joined
Aug 14, 2019
Messages
105
Hi Guys,

I have the below code, which looks through a folder for files and copies the data into my open workbook. I need the code to look for filenames only containing a string of ABC, and if the filename contains anything else, then X (The sheet to copy) would be 1 to 2 (Instead of 3 to 3)... Anyhelp?

Thanks

VBA Code:
    Do While MyFile <> ""
Set wkbSource = Workbooks.Open(Filename:=MyFolder & "\" & MyFile)
For x = 3 To 3
If WorksheetFunction.CountA(Sheets(x).Cells) <> 0 Then
With Sheets(x)
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With
With Surveys
Sheets(x).UsedRange.Copy .Cells(.Rows.Count, "B").End(xlUp).Offset(2, 0)
.Cells(.Rows.Count, "A").End(xlUp).Offset(2, 0).Resize(LastRow) = wkbSource.Name
    End With

    End If
Next x
MyFile = Dir
wkbSource.Close False
    Loop
 
This should loop all the subfolders
VBA Code:
Sub newS()

Dim FSOLibrary As Object
Dim FSOFolder As Object
Dim folderName As String
Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select the latest ILC folder from your desktop"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then
MsgBox "You did not select a folder."
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\"
End With

LoopSubFolders FSOLibrary.GetFolder(MyFolder)

End Sub

Sub LoopSubFolders(FSOFolder As Object)
Dim FSOSubFolder As Object
Dim FSOFile As Object
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.DisplayAlerts = False

Set Surveys = ActiveWorkbook.Sheets("Survey Returns")
Set FNCs = ActiveWorkbook.Sheets("FNC's")

For Each FSOSubFolder In FSOFolder.subfolders
    LoopSubFolders FSOSubFolder
Next
For Each FSOFile In FSOFolder.Files
If InStr(FSOFile.Name, "xls") Then
If InStr(FSOFile.Name, "Surv") Then
Set wkbSource = Workbooks.Open(fileName:=FSOFile.Path)
For x = 3 To 3
If WorksheetFunction.CountA(Sheets(x).Cells) <> 0 Then
With Sheets(x)
On Error Resume Next
.Cells.UnMerge
On Error GoTo 0
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With
With Surveys
Sheets(x).UsedRange.Copy .Cells(.Rows.Count, "B").End(xlUp).Offset(2, 0)
.Cells(.Rows.Count, "A").End(xlUp).Offset(2, 0).Resize(LastRow) = wkbSource.Name
End With
End If
Next x

ElseIf InStr(FSOFile.Name, "EH") Then
For x = 1 To 2
If WorksheetFunction.CountA(Sheets(x).Cells) <> 0 Then
With Sheets(x)
On Error Resume Next
.Cells.UnMerge
On Error GoTo 0
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With
With FNCs
Sheets(x).UsedRange.Copy .Cells(.Rows.Count, "B").End(xlUp).Offset(2, 0)
.Cells(.Rows.Count, "A").End(xlUp).Offset(2, 0).Resize(LastRow) = wkbSource.Name
End With
End If
Next x
wkbSource.Close False
End If
End If
Next

End Sub
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
This should loop all the subfolders
VBA Code:
Sub newS()

Dim FSOLibrary As Object
Dim FSOFolder As Object
Dim folderName As String
Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select the latest ILC folder from your desktop"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then
MsgBox "You did not select a folder."
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\"
End With

LoopSubFolders FSOLibrary.GetFolder(MyFolder)

End Sub

Sub LoopSubFolders(FSOFolder As Object)
Dim FSOSubFolder As Object
Dim FSOFile As Object
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.DisplayAlerts = False

Set Surveys = ActiveWorkbook.Sheets("Survey Returns")
Set FNCs = ActiveWorkbook.Sheets("FNC's")

For Each FSOSubFolder In FSOFolder.subfolders
    LoopSubFolders FSOSubFolder
Next
For Each FSOFile In FSOFolder.Files
If InStr(FSOFile.Name, "xls") Then
If InStr(FSOFile.Name, "Surv") Then
Set wkbSource = Workbooks.Open(fileName:=FSOFile.Path)
For x = 3 To 3
If WorksheetFunction.CountA(Sheets(x).Cells) <> 0 Then
With Sheets(x)
On Error Resume Next
.Cells.UnMerge
On Error GoTo 0
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With
With Surveys
Sheets(x).UsedRange.Copy .Cells(.Rows.Count, "B").End(xlUp).Offset(2, 0)
.Cells(.Rows.Count, "A").End(xlUp).Offset(2, 0).Resize(LastRow) = wkbSource.Name
End With
End If
Next x

ElseIf InStr(FSOFile.Name, "EH") Then
For x = 1 To 2
If WorksheetFunction.CountA(Sheets(x).Cells) <> 0 Then
With Sheets(x)
On Error Resume Next
.Cells.UnMerge
On Error GoTo 0
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With
With FNCs
Sheets(x).UsedRange.Copy .Cells(.Rows.Count, "B").End(xlUp).Offset(2, 0)
.Cells(.Rows.Count, "A").End(xlUp).Offset(2, 0).Resize(LastRow) = wkbSource.Name
End With
End If
Next x
wkbSource.Close False
End If
End If
Next

End Sub
That's completely not working now, seems to be copying sheet 1 from active workbook into FNC's sheet. Nothing else seems to be working either... Is there any way to modify the previous code slightly? Thanks
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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