samilynn
Board Regular
- Joined
- Jun 24, 2003
- Messages
- 171
- Office Version
- 2016
- Platform
- Windows
It's now months later, and I am still baffled by the fact that FileSearch is gone from Excel 2007. I cannot get a grasp on the Dir Function, can anyone help me modify this code to use Dir instead of FileSearch?
Option Explicit
Dim rngData As Range
Sub ReadWorkbooks()
Const strDirectory As String = "C:\Documents and Settings\SamG\Desktop\mg"
Dim varFile As Variant
Set rngData = ThisWorkbook.Worksheets("Data").Range("A" & _
ThisWorkbook.Worksheets("Data").Range("A65536").End(xlUp).Row).Offset(1, 0)
Application.ScreenUpdating = False
With Application.FileSearch
.NewSearch
.LookIn = strDirectory
.SearchSubFolders = False
.FileType = msoFileTypeExcelWorkbooks
If .Execute > 0 Then
For Each varFile In .FoundFiles
Merge varFile
Next
End If
End With
Application.ScreenUpdating = True
End Sub
Sub Merge(ByVal strFileName As String)
Dim lngEndRow As Long, lngRow As Long
Dim ws As Worksheet, shp As Shape
Workbooks.Open strFileName
For Each ws In ActiveWorkbook.Worksheets
ws.Rows(1).Insert
ws.Columns("M").Insert
lngEndRow = ws.Range("A65536").End(xlUp).Row
ws.Range("M2:M" & lngEndRow).FormulaR1C1 = "=CountA(RC1:RC[-1])"
ws.Range("A1:M" & lngEndRow).AutoFilter Field:=13, Criteria1:="<>0"
ws.Range("A2:L" & lngEndRow).SpecialCells(xlCellTypeVisible).Copy
rngData.Paste
For Each shp In ActiveSheet.Shapes
shp.Delete
Next shp
Set rngData = ThisWorkbook.Worksheets("Data").Range("A" & _
ThisWorkbook.Worksheets("Data").Range("A65536").End(xlUp).Row).Offset(1, 0)
Next ws
Application.DisplayAlerts = False
ActiveWorkbook.Close SaveChanges:=False
Application.DisplayAlerts = True
End Sub
Option Explicit
Dim rngData As Range
Sub ReadWorkbooks()
Const strDirectory As String = "C:\Documents and Settings\SamG\Desktop\mg"
Dim varFile As Variant
Set rngData = ThisWorkbook.Worksheets("Data").Range("A" & _
ThisWorkbook.Worksheets("Data").Range("A65536").End(xlUp).Row).Offset(1, 0)
Application.ScreenUpdating = False
With Application.FileSearch
.NewSearch
.LookIn = strDirectory
.SearchSubFolders = False
.FileType = msoFileTypeExcelWorkbooks
If .Execute > 0 Then
For Each varFile In .FoundFiles
Merge varFile
Next
End If
End With
Application.ScreenUpdating = True
End Sub
Sub Merge(ByVal strFileName As String)
Dim lngEndRow As Long, lngRow As Long
Dim ws As Worksheet, shp As Shape
Workbooks.Open strFileName
For Each ws In ActiveWorkbook.Worksheets
ws.Rows(1).Insert
ws.Columns("M").Insert
lngEndRow = ws.Range("A65536").End(xlUp).Row
ws.Range("M2:M" & lngEndRow).FormulaR1C1 = "=CountA(RC1:RC[-1])"
ws.Range("A1:M" & lngEndRow).AutoFilter Field:=13, Criteria1:="<>0"
ws.Range("A2:L" & lngEndRow).SpecialCells(xlCellTypeVisible).Copy
rngData.Paste
For Each shp In ActiveSheet.Shapes
shp.Delete
Next shp
Set rngData = ThisWorkbook.Worksheets("Data").Range("A" & _
ThisWorkbook.Worksheets("Data").Range("A65536").End(xlUp).Row).Offset(1, 0)
Next ws
Application.DisplayAlerts = False
ActiveWorkbook.Close SaveChanges:=False
Application.DisplayAlerts = True
End Sub