Convert VBA Application.Filesearch Copy/Paste

Mtaylorlc

New Member
Joined
Mar 31, 2002
Messages
42
I am trying to update some old code I used years ago to work in a current process. The code below, for every source workbook in the same folder, will open, copy a specific range, paste that range in a summary/master workbook, and close the source document. It loops through this process for all excel documents in the folder, excluding the summary/master.

One change in how this is being used that may or may not be relevant... The source documents are all stored in a MS Teams folder...


VBA Code:
Private Sub cmdUpdate_Click()
Dim FS, i
Dim PlaceRow As Long
Dim OpenedName As String
Dim DoNotReopenActiveWB_Name As String
Dim DataBook As String

Sheet1.Range("A2:AF1000").ClearContents
Sheet1.Range("BA1:BA1000").ClearContents
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.ScreenUpdating = False
DoNotReopenActiveWB_Name = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
DataBook = ActiveWorkbook.Name
PlaceRow = 1
Debug.Print DoNotReopenActiveWB_Name
Set FS = Application.FileSearch
With FS
    .LookIn = ActiveWorkbook.Path
    .Filename = "*.xlsx"
    If .Execute Then
        For i = 1 To .FoundFiles.Count
            If .FoundFiles(i) <> DoNotReopenActiveWB_Name Then
                PlaceRow = PlaceRow + 1
                Workbooks.Open .FoundFiles(i)
                OpenedName = ActiveWorkbook.Name
                Workbooks(DataBook).Sheets("Level 1") _
                    .Range("A" & PlaceRow & ":AI" & PlaceRow).Value = _
                Workbooks(OpenedName).Sheets("Level 1") _
                    .Range("A114:AI114").Value
                Workbooks(DataBook).Sheets("Table1") _
                    .Range("BA" & PlaceRow).Value = .FoundFiles(i)
                Workbooks(OpenedName).Close savechanges:=False
            End If
        Next i
    End If
End With
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try this

VBA Code:
Private Sub cmdUpdate_Click()
  Dim wFiles As Variant, sPath As String, i As Long
  Dim wb1 As Workbook, wb2 As Workbook
  
  Application.DisplayAlerts = False
  Application.EnableEvents = False
  Application.ScreenUpdating = False
  
  Set wb1 = ThisWorkbook
  wb1.Sheets("Level 1").Range("A2:AI1000").ClearContents
  wb1.Sheets("Table1").Range("BA1:BA1000").ClearContents
  
  sPath = wb1.Path & "\"          '
  wFiles = Dir(sPath & "*.xlsx")  'all xlsx files
  i = 1                           'PlaceRow
  
  Do While wFiles <> ""
    If wFiles <> wb1.Name Then
      i = i + 1
      Set wb2 = Workbooks.Open(sPath & wFiles)
      wb1.Sheets("Level 1").Range("A" & i & ":AI" & i).Value = wb2.Sheets("Level 1").Range("A114:AI114").Value
      wb1.Sheets("Table1").Range("BA" & i).Value = wFiles
      wb2.Close savechanges:=False
    End If
    wFiles = Dir()
  Loop
  
  Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,745
Messages
6,180,699
Members
452,994
Latest member
Janick

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