Make this code run faster?

bigj4155

Board Regular
Joined
Mar 3, 2005
Messages
187
Hello everyone!

Hope everyone is having a good and relaxed holiday season! I have a situation that I can see running into problems down the road, so I am trying to solve it now.

Basically I have a directory filled with identical worksheets "layout wise" with different vales entered into the template. I then have a worksheet that runs code that goes through each worksheet in my directory and returns the values listed in A4:P4. The code is working wonderful, however as the directory grows in size and more worksheets are added the process of extracting this information is getting pretty slow. Currently there is only some 500 files in the directory and it takes roughly 2 minutes extract the information. However we will soon be adding around 200 new files a month. So in 5 months of so I can see this taking a very long time :) Anyway here is my code that I use now.



Public Sub GetDirXlsContents()
' Source sheet name, Source directory path, Source cell Range
Call CopyFromEachFileInPath("PDATemplate", "L:\Shared\YNAGC INVNTRY\STAINV\STAPDA\PDAhc", "A4:P4")
End Sub

Private Sub CopyFromEachFileInPath(SheetName, Path, Rng)
Dim fs, f, f1, fc, s, c As Long
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(Path & "\")
Set fc = f.Files

' make a temp sheet
Range("B3:P60000").Select
Selection.ClearContents
Application.ScreenUpdating = False
TargSh = ActiveSheet.Name
Sheets.Add
TempSh = ActiveSheet.Name
Sheets(TargSh).Activate
Application.ScreenUpdating = True

For Each f1 In fc
With Sheets(TempSh)

' clear temp sheet and start again
.Cells.ClearContents

' Place Src Info on Temp Targ Sheet
If Right(f1.Name, 3) = "xls" Then
fName = Left(f1.Name, Len(f1.Name) - 4)
.Range(Rng).FormulaArray = "='" & Path & "\[" & fName & "]" & SheetName & "'!" & Rng
.Range(Rng).Value = .Range(Rng).Value

'GetValuesFromAClosedWorkbook Path, f1.Name, SheetName, "A1:J500"
End If

NxRw = Workbooks("PDAlookup.xls").Sheets("Sheet1").Range("B1000").End(xlUp).Row + 1
.Range("A4:P4").Copy
Sheets(TargSh).Cells(NxRw, 1).Offset(0, 1).Select
ActiveSheet.Paste
End With
Next ' workbook

Columns("C:C").Select
Selection.NumberFormat = "m/d/yyyy"
Columns("K:K").Select
Selection.NumberFormat = "$#,##0.00"
Columns("B:O").Select
With Selection
.HorizontalAlignment = xlCenter
End With
Range("A1").Select

' get rid of temp sheet
Application.DisplayAlerts = False
Sheets(TempSh).Delete
Application.DisplayAlerts = True
End Sub

So when new files are added, we run this code, build the table from it and then save the worksheet. Is there a way to maybe, if the file is already listed skip line and continue? Would this even speed the process up?

Thanks all!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I don't know if this helps but you don't actually need to select ranges to work with them.

As to your question about skipping files, I'm sure it must be possible somehow. I'll have a think about that and post back.

One thought is that you have a list of files already imported and you somehow search it when you do a new import.
Code:
Private Sub CopyFromEachFileInPath(SheetName, Path, Rng)
Dim TargSh As Worksheet
Dim TempSh As Worksheet

Dim fs, f, f1, fc, s, c As Long

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(Path & "\")
    Set fc = f.Files
    
    Application.ScreenUpdating = False
    
    ' make a temp sheet
    Range("B3:P60000").ClearContents
    
    Set TargSh = ActiveSheet
    Sheets.Add
    Set TempSh = ActiveSheet
    
    TargSh.Activate
    Application.ScreenUpdating = True
    
    For Each f1 In fc
        With TempSh
    
            ' clear temp sheet and start again
            .Cells.ClearContents
    
            ' Place Src Info on Temp Targ Sheet
            If Right(f1.Name, 3) = "xls" Then
                fName = Left(f1.Name, Len(f1.Name) - 4)
                .Range(Rng).Formula = "='" & Path & "\[" & fName & "]" & SheetName & "'!" & Rng
                .Range(Rng).Value = .Range(Rng).Value
    
                'GetValuesFromAClosedWorkbook Path, f1.Name, SheetName, "A1:J500"
            End If
    
            NxRw = Workbooks("PDAlookup.xls").Sheets("Sheet1").Range("B1000").End(xlUp).Row + 1
                .Range("A4:P4").Copy
                TargSh.Cells(NxRw, 1).Offset(0, 1).Paste
        End With
    Next ' workbook
    
    Columns("C:C").NumberFormat = "m/d/yyyy"
    Columns("K:K").NumberFormat = "$#,##0.00"
    Columns("B:O").HorizontalAlignment = xlCenter
    
    Range("A1").Select
    
    ' get rid of temp sheet
    Application.DisplayAlerts = False
    TempSh.Delete
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Ahh bad habits that I never went back to fix. When copy/paste your code I got a error on this line.

TargSh.Cells(NxRw, 1).Offset(0, 1).Paste

No worries, corrected a few things and it runs the same just easier on the old eyes

What Im thinking is the value in Column O is the filename without the path and the ".xls" attached. So in human language :

If PDAhc\filename = Column "o" & ".xls" Then Next Filename
Else "rest of code"

like I said human language :)





Private Sub CopyFromEachFileInPath(SheetName, Path, Rng)
Dim fs, f, f1, fc, s, c As Long
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(Path & "\")
Set fc = f.Files

' make a temp sheet
Range("B3:P60000").ClearContents
Application.ScreenUpdating = False
TargSh = ActiveSheet.Name
Sheets.Add
TempSh = ActiveSheet.Name
Sheets(TargSh).Activate
Application.ScreenUpdating = True

For Each f1 In fc
With Sheets(TempSh)

' clear temp sheet and start again
.Cells.ClearContents

' Place Src Info on Temp Targ Sheet
If Right(f1.Name, 3) = "xls" Then
fName = Left(f1.Name, Len(f1.Name) - 4)
.Range(Rng).FormulaArray = "='" & Path & "\[" & fName & "]" & SheetName & "'!" & Rng
.Range(Rng).Value = .Range(Rng).Value

'GetValuesFromAClosedWorkbook Path, f1.Name, SheetName, "A1:J500"
End If

NxRw = Workbooks("PDAlookup.xls").Sheets("Sheet1").Range("B1000").End(xlUp).Row + 1
.Range("A4:P4").Copy
Sheets(TargSh).Cells(NxRw, 1).Offset(0, 1).Select
ActiveSheet.Paste
End With
Next ' workbook

Columns("C:C").NumberFormat = "m/d/yyyy"
Columns("K:K").NumberFormat = "$#,##0.00"
Columns("B:O").HorizontalAlignment = xlCenter
Range("A1").Select

' get rid of temp sheet
Application.DisplayAlerts = False
Sheets(TempSh).Delete
Application.DisplayAlerts = True
End Sub
 
Upvote 0
bigj4155 said:
Ahh bad habits that I never went back to fix. When copy/paste your code I got a error on this line.

TargSh.Cells(NxRw, 1).Offset(0, 1).Paste

That's my mistake. :oops:

Instead of what you changed it to I would try this.
Code:
TargSh.Cells(NxRw, 1).Offset(0, 1).PasteSpecial xlPasteAll
Like I said no need to select.

As for skipping files I'm still thinking about it, I didn't really work out exactly what the code is doing.

Perhaps you could explain in simple terms what's actually going on.
 
Upvote 0
How to explain this... lets see.

Basically we have a template excel file that we will list different part numbers in and what they cost, how many ect... ect...

I then take all of the "important" data from this template and line the data up in row 4. A4:P4. I do this because our template changes from time to time but the data is always the same kind. So I can just change my reference. This file is then saved in my PDAhc directory.

Now when I open up PDAlookup.xls I run this macro and it goes through every file in pdahc and grabs A4:P4 and list them in pdalookup.xls!sheet1

Everytime the macro is ran it erases sheet1 and starts right under the header column. Lemme grab a snap shot of the pda lookup file.
PDAlookup.xls
ABCDEFGHIJKLMNOPQ
1Back to Homepage
2Open PDATrackingDateCarlineYearSkuWhseCreated ByCost CenterPiecesCostResponsibiltyReasonMaterial Desc.Case #Case2#/InfoCase3#/Info
3FBU5424FBU542412/6/2005D21920055F9T14401XASTAUser5202$86.08491H=Release DecreaseFG - ProductionSSTAC00000000000
4DCBU-4788DCBU-478812/6/2005LX200605087332AESTAUser2432$93.92254A=Parts damaged during ReworkFG - ProductionSSTAC00000000022674000
5FBU- 5521FBU- 552112/9/2005P22120065L3T14358FHSTAUser2434$42.12254A=Parts damaged during ReworkFG - ProductionSSTAC00000000025123200
Sheet1



O column is the filename without the path and the extension ".xls"
Then as you can see in Column A it just grabs the info in Column O and adds the directory and extension so the users can open whichever file they choose.
 
Upvote 0
So is there only 1 row for each file?
 
Upvote 0
Ya, the data is scattered throughout the template but for instance

Is cell A4 "=C8" then B4 "=h8" and C4 "=G7" and so on.

So when I run PDAlookup it just goes into each file and grabs A4:P4 instead of me pointing it to individual cells.

A4:P4 has the data I want in every file, this will never change.

Im going to tinker with it today, although I am not to effecient with vba so I will probably break more than improve :) Should be a learning experience today tho.

Here ya go, maybe this will tie it together for you:
P.S - im scratching my head on what the deal with the is all over the place. Makes it hard to read. *Fixed, just had to read the FAQ on it lol, cleaned up previous file post also*
SSTAC000000000FBU5449.xls
ABCDEFGHIJKLMNOPQ
1
2
3TrackingDateCarlineYearSkuWhseCreated ByCost CenterPiecesCostResponsibilityReasonMaterial DescriptionCase #Case2#/InfoCase3#/Info
4FBU-544912/6/2005D21920055G1T13A625ACSTADTDUser Created520351.00$207.09491H=Release DecreaseFG - ProductionSSTAC000000000FBU544900
5
6
7*Attention Inventory Analyst: If discrepancies are discovered in processing this document, contact the issuer for resolution.Car Line:D219M/Y:2005
8Tracking #:FBU-5449Date Issued:12/6/05
9Component Part Number:Date PDA Received:12/6/05
10Customer/Vendor Part Number:5G1T13A625ACIssued by:User Created
11Whse location/Qty:STA351Bin Loc:Issuing Cost Center:520
12Whse location/Qty:DTD7Bin Loc:Last Date Received:
Sheet1
 
Upvote 0
I have a question / suggestion.

In the following few lines from your code you turn off screen updating and then turn it back on. I can't see why you need to do that. I'd leave it turned off. That will definitely make your code run faster.

' make a temp sheet
Range("B3:P60000").Select
Selection.ClearContents
Application.ScreenUpdating = False
TargSh = ActiveSheet.Name
Sheets.Add
TempSh = ActiveSheet.Name
Sheets(TargSh).Activate
Application.ScreenUpdating = True 'why turn it back on?
 
Upvote 0
Ahhh, well there is a semi good reason for that :) Basically I did place that there for a reason. When I leave screen updating off for the entire code it does run a smig faster "maybe 2% faster, not much" but if the process takes say 5 mins to read every file and post its results, excel APPEARS to be locked during this process.

In short I had people thinking the worksheet had locked up and they proceeded to close it through task manager ect...

This way it turns screen updating off only during the flip flop from temp sheet to copy to sheet1. So your screen doesnt go crazy, instead you get a nice flow of rows as it updates from each file. Its kinda like a status bar

I do believe that screen updating will have to be turned off and will have more of a impact once there are more files to add. Hopefully I can figure something out before I get to that point tho
 
Upvote 0

Forum statistics

Threads
1,215,427
Messages
6,124,830
Members
449,190
Latest member
rscraig11

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