Need help with a Directory Macro

copleyr

Active Member
Joined
Aug 24, 2009
Messages
381
Hello,

Below I have a macro that will go into a folder and pull the names of the excel files.

1) I now need a macro that will go into the "Defaults" tab of each of these files and copy row 70 (columns "A" through "BM") and paste it into a blank sheet.

There could be 100 files in this folder. Is there a way to do this?

Many thanks in advance!


PHP:
Sub ListFiles()
Dim MyPathName As String
Dim MyFileName As String
Dim NumChars As Long
Dim X As Long
    NumChars = 6 'Change this to the number of characters you wish to return
    MyPathName = "C:\Program Files\excel files_folder\*.xl*" 'Change this to the folder and filetypes you want to return
    MyFileName = Dir(MyPathName)
    Do While MyFileName <> ""
        X = X + 1
        Sheet1.Cells(X, 1) = Left(MyFileName, NumChars)
        MyFileName = Dir
    Loop
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
So you want A70:BM70 from every sheet copied one after the other in to a single sheet like a summary yes?
 
Upvote 0
Again, I am free typing this so it may have errors, please report back.

Code:
Option Explicit
Sub SumariseFiles()
Dim MyPathName As String
Dim MyFileName As String
Dim NumChars As Long
Dim X As Long
Dim SummarySheet As Workbook
Workbooks.Add
SummarySheet = ActiveWorkbook
MyPathName = "C:\Program Files\excel files_folder\*.xl*" 'Change this to the folder and filetypes you want to return
MyFileName = Dir(MyPathName)
Do While MyFileName <> ""
    X = X + 1
    Workbooks.Open (MyPathName & MyFileName)
    SummarySheet.Activate
    Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1 & ":BM" & Range("A" & Rows.Count).End(xlUp).Row + 1) = Application.Transpose(Application.Transpose(Workbooks(MyFileName).Sheets(1).Range("A70:BM70")))
    Workbooks(MyFileName).Close False
    MyFileName = Dir
Loop
End Sub
 
Upvote 0
Thank you so much for the quick turnaround. I got a Run-time error '91': object variable or with block variable not set.

it pointed at this line:

SummarySheet = ActiveWorkbook

Any ideas?
 
Upvote 0
Can't remember how to put the book to a variable properly, this workaround should fix that error:

Code:
Sub SummeriseFiles()
Dim MyPathName As String
Dim MyFileName As String
Dim NumChars As Long
Dim X As Long
Dim SummarySheet As String
Workbooks.Add
SummarySheet = ActiveWorkbook.Name
MyPathName = "C:\Program Files\excel files_folder\*.xl*" 'Change this to the folder and filetypes you want to return
MyFileName = Dir(MyPathName)
Do While MyFileName <> ""
    X = X + 1
    Workbooks.Open (MyPathName & MyFileName)
    Workbooks(SummarySheet).Activate
    Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1 & ":BM" & Range("A" & Rows.Count).End(xlUp).Row + 1) = Application.Transpose(Application.Transpose(Workbooks(MyFileName).Sheets(1).Range("A70:BM70")))
    Workbooks(MyFileName).Close False
    MyFileName = Dir
Loop
End Sub
 
Upvote 0
How to I get it to go to the "Defaults" tab in the file and copy the rows? "Defaults" is the name of the tab.

It is going into the files but it doesn't seem to go to the "Defaults" tab to copy. Nothing ends up being copied.

Any ideas?

Thanks again.
 
Last edited:
Upvote 0
Silly me, I forgot that bit.

5th Last line of code (the really long one)

Change this:

.Sheets(1).Range("A70:BM70")))

To this:

.Sheets("Defaults").Range("A70:BM70")))

In other words change the 1 to "Defaults"
 
Upvote 0

Forum statistics

Threads
1,224,508
Messages
6,179,189
Members
452,893
Latest member
denay

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