Can you help me speed up this posted code?

Grizlore

Active Member
Joined
Aug 22, 2006
Messages
259
Although this code works great, it takes an age (+50mins) to run.

Could anyone have a look and see if this code can be speeded up anywhere please?

Any help would be appreciated




Code:
Sub Button()

Application.ScreenUpdating = False

Dim TheFolder As String
Dim TheFile As String
Dim TheHyperlinkPath As String
Dim TheRow As Integer
Dim CreationDate As Date

TheRow = 2

TheFolder = "\\WWW0\WWWSHARE\GMP\Quality and Hygiene\CofAs"

TheFile = Dir(TheFolder & "\*.*")

Sheets("All the CofAs").Select

    Range("A2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.ClearContents
    Range("A2").Select


Do Until TheFile = ""

            TheHyperlinkPath = TheFolder & "\" & TheFile
            Sheets("All the CofAs").Cells(TheRow, 1).Select
            Sheets("All the CofAs").Hyperlinks.Add Anchor:=Cells(TheRow, 1), Address:= _
            TheHyperlinkPath, TextToDisplay:=TheFile
            ActiveCell.Offset(0, 1).Activate
            ActiveCell = FileDateTime(TheHyperlinkPath)
            TheRow = TheRow + 1
            TheFile = Dir

Loop

    With Sheets("All the CofAs")

        With .Columns("A")
        .AutoFilter Field:=1, Criteria1:="Certificates Of Analysis.xls"
        On Error Resume Next
        .SpecialCells(xlCellTypeVisible).EntireRow.Delete
                On Error GoTo 0
        End With


    End With

    Rows("1:1").Select
    Selection.Insert Shift:=xlDown
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "Certificate of Analysis File Name"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "File Date"
    Rows("1:1").Select
    Selection.Font.Bold = True
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = False
    End With

        Range("B2").Select
    Selection.Sort Key1:=Range("B2"), Order1:=xlDescending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom


        Columns("B:B").Select
    Selection.Insert Shift:=xlToRight
    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :=".", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "File Type"
    Range("C9").Select
    Columns("B:C").EntireColumn.AutoFit

      Columns("B:C").Select
    Selection.Font.Underline = xlUnderlineStyleSingle
    Selection.Font.Underline = xlUnderlineStyleNone
    Selection.Font.ColorIndex = 1
    Columns("B:C").EntireColumn.AutoFit
    Selection.ColumnWidth = 12.5
    Columns("C:C").Select
    Columns("C:C").EntireColumn.AutoFit
    Columns("B:B").Select
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Columns("C:C").Select
    Selection.ColumnWidth = 17.71
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Range("A1").Select
    Selection.AutoFilter
    
    MsgBox "All CofAs have now been imported"
   
   Application.ScreenUpdating = True
    
End Sub


Thanks for looking
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I think we'll have to send you back to do your homework first. ;)

This:
Code:
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "Certificate of Analysis File Name"
should really be like this:
Code:
    Rows("1:1").Insert Shift:=xlDown
    Range("A1").FormulaR1C1 = "Certificate of Analysis File Name"
and the same goes for largely ALL the ".select Selection." pairs
fix that and we can look further, I think you are eg reformatting the columns B and C multiple times.

Also when you clearcontents, could you just not simply clear ALL contents in one go instead of finding the filled range? Or is there something you want to save before making the new links?
 
Upvote 0
Thanks... I'll tidy up those ".selects"

What I basically want to do is...

Catalogue the stated folder, into one worksheet... with columns for "File Name" (which is a hyperlink to the file) and the "File Date". There is also a column for "file type" but that is just needed to create the proper hyperlink address.

The clearing of the sheet needs only to be done once, if/when the code is started
 
Upvote 0
When you've tidied up those selects put the code back up and we'll have another look at it. There was just SO many selects I would have to remove it was looking like a bother. Also removing the selects should speed up the code.

This block:
Code:
Sheets("All the CofAs").Select

    Range("A2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.ClearContents
    Range("A2").Select
can probably be replaced with this (provided you just want to clear everything existing in the worksheet):
Code:
Worksheets("All the CofAs").usedrange.clearcontents
 
Upvote 0
Now that's slimmed it down somewhat :)

Code:
Sub Button()

Application.ScreenUpdating = False

Dim TheFolder As String
Dim TheFile As String
Dim TheHyperlinkPath As String
Dim TheRow As Integer
Dim CreationDate As Date

TheRow = 1

TheFolder = "\\Wbx0\WBXSHARE\GMP\Quality and Hygiene\CofAs"
TheFile = Dir(TheFolder & "\*.*")

Worksheets("All the CofAs").UsedRange.ClearContents

Do Until TheFile = ""

            TheHyperlinkPath = TheFolder & "\" & TheFile
            Sheets("All the CofAs").Select ' doesnt work without this line in
            Sheets("All the CofAs").Cells(TheRow, 1).Select
            Sheets("All the CofAs").Hyperlinks.Add Anchor:=Cells(TheRow, 1), Address:= _
            TheHyperlinkPath, TextToDisplay:=TheFile
            TheRow = TheRow + 1
            TheFile = Dir
Loop

    Rows("1:1").Select
    Selection.Insert Shift:=xlDown
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "Certificate of Analysis File Name"

    Rows("1:1").Select
    Selection.Font.Bold = True

    Columns("A:C").EntireColumn.AutoFit
      Range("B1").Select
    MsgBox "All CofAs have now been imported"
   
   Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
I think you can trim it down further to this. But after that I think we are running out of things to do.

I see you've cut out a lot of code, was it unnecessary or you just didn't want to swamp us in code?
Hopefully you haven't cut out too much of what you needed done.
Code:
Sub Button()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Dim TheFolder As String
Dim TheFile As String
Dim TheHyperlinkPath As String
Dim TheRow As Integer
Dim CreationDate As Date

TheRow = 1

TheFolder = "\\Wbx0\WBXSHARE\GMP\Quality and Hygiene\CofAs"
TheFile = Dir(TheFolder & "\*.*")

Worksheets("All the CofAs").UsedRange.ClearContents

Do Until TheFile = ""

            TheHyperlinkPath = TheFolder & "\" & TheFile
            Sheets("All the CofAs").Select ' doesnt work without this line in
            Sheets("All the CofAs").Hyperlinks.Add Anchor:=Cells(TheRow, 1), Address:= _
            TheHyperlinkPath, TextToDisplay:=TheFile
            TheRow = TheRow + 1
            TheFile = Dir
Loop

    Rows("1:1").Insert Shift:=xlDown
    Range("A1").FormulaR1C1 = "Certificate of Analysis File Name"

    Rows("1:1").Font.Bold = True

    Columns("A:C").EntireColumn.AutoFit
      Range("B1").Select
    MsgBox "All CofAs have now been imported"

   Application.Calculation = xlCalculationAutomatic
   Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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