+ Reply to Thread
Results 1 to 4 of 4

Code running extremely slow (20-30 minutes)

Hybrid View

  1. #1
    Registered User
    Join Date
    06-22-2017
    Location
    San Jose, CA
    MS-Off Ver
    Excel 2016
    Posts
    9

    Code running extremely slow (20-30 minutes)

    Hey guys,
    I have written a few Macros since my first post on here, and they're working out pretty well. One of these Macros I am trying to run on multiple files. I have a working code but for some reason it takes a massive amount of time to run. Any suggestions or ideas on why it's so slow.
    Here it is:
    Private Sub CommandButton1_Click()
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder
    Dim LastRow, Min, Max, StDev, Logavg As Double
    Dim FileNumber As Single
    Dim Sum As Long
    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(ThisWorkbook.Path)
    Application.ScreenUpdating = False
       On Error Resume Next
    ThisWorkbook.ActiveSheet.Cells(1, 5) = "Logger Avg."
    ThisWorkbook.ActiveSheet.Cells(1, 6) = "Logger Max."
    ThisWorkbook.ActiveSheet.Cells(1, 7) = "Logger Min."
    ThisWorkbook.ActiveSheet.Cells(1, 8) = "Std. Dev."
    FileNumber = 18
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        ActiveSheet.DisplayPageBreaks = False
        For Each FileItem In SourceFolder.Files
    If FileItem.Name <> ThisWorkbook.Name Then
        Workbooks.Open (ThisWorkbook.Path & Application.PathSeparator & FileItem.Name)
    LastRow = Workbooks(FileItem.Name).Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
    Row = 2
    r = 2
    Summ = 0
     Do
    If Workbooks(FileItem.Name).Sheets(1).Cells(Row, 4) < 90 Then
        Workbooks(FileItem.Name).Sheets(1).Cells(r, 12) = 10 ^ (Workbooks(FileItem.Name).Sheets(1).Cells(Row, 4) / 10)
        Row = Row + 1
        Summ = Summ + Workbooks(FileItem.Name).Sheets(1).Cells(r, 12)
        End If
    r = r + 1
        If Workbooks(FileItem.Name).Sheets(1).Cells(r, 4) = vbNullString Then
     Exit Do
     End If
        Loop
     Added = 0
     r = 2
     Added = Added + Workbooks(FileItem.Name).Sheets(1).Cells(r, 12)
    N = Summ / (LastRow - 1)
    Logavg = 10 * Log10(N)
        Max = Application.WorksheetFunction.Max(Workbooks(FileItem.Name).Sheets(1).Range(Cells(2, 4), Cells(LastRow, 4)).Value)
        Min = Application.WorksheetFunction.Min(Workbooks(FileItem.Name).Sheets(1).Range(Cells(2, 4), Cells(LastRow, 4)).Value)
        StDev = Application.WorksheetFunction.StDev(Workbooks(FileItem.Name).Sheets(1).Range(Cells(2, 4), Cells(LastRow, 4)).Value)
    
        Workbooks(FileItem.Name).Save
        Workbooks(FileItem.Name).Close
    FileNumber = FileNumber + 1
    End If
    ActiveSheet.Cells(FileNumber, 5) = Logavg
    ActiveSheet.Cells(FileNumber, 6) = Max
    ActiveSheet.Cells(FileNumber, 3) = "1"
    ActiveSheet.Cells(FileNumber, 9) = (Hour(Workbooks(FileItem.Name).Sheets(1).Cells(LastRow, 3))) - (Hour(Workbooks(FileItem.Name).Sheets(1).Cells(2, 3)))
    ActiveSheet.Cells(FileNumber, 1) = Workbooks(FileItem.Name).Sheets(1).Cells(2, 2)
        Next FileItem
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        ActiveSheet.DisplayPageBreaks = True
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
    
    End Sub
    
     Private Static Function Log10(x)
    
    
    
     Log10 = Log(x) / Log(10#)
     End Function

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Code running extremely slow (20-30 minutes)

    Hi Biker_Seth

    I am very certain that we can assist, however, be so kind and upload a sample file and explain to us what you are trying to achieve.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    06-22-2017
    Location
    San Jose, CA
    MS-Off Ver
    Excel 2016
    Posts
    9

    Re: Code running extremely slow (20-30 minutes)

    Quote Originally Posted by sintek View Post
    Hi Biker_Seth

    I am very certain that we can assist, however, be so kind and upload a sample file and explain to us what you are trying to achieve.
    Thank you, I am trying to loop through a folder full of files like this one, do some processing, and extract the results. Here is an example file.
    Attached Files Attached Files

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Code running extremely slow (20-30 minutes)

    I'm curious....How are you performing this code if you have already closed the file...Workbooks(FileItem.Name)
    ActiveSheet.Cells(FileNumber, 5) = Logavg
    ActiveSheet.Cells(FileNumber, 6) = Max
    ActiveSheet.Cells(FileNumber, 3) = "1"
    ActiveSheet.Cells(FileNumber, 9) = (Hour(Workbooks(FileItem.Name).Sheets(1).Cells(LastRow, 3))) - (Hour(Workbooks(FileItem.Name).Sheets(1).Cells(2, 3)))
    ActiveSheet.Cells(FileNumber, 1) = Workbooks(FileItem.Name).Sheets(1).Cells(2, 2)
    What is the reason for this...
    Added = 0
    Added = Added + Workbooks(FileItem.Name).Sheets(1).Cells(r, 12)
    Have a look at amended code...Might just be a shot in the dark...
    Private Sub CommandButton1_Click()
    Dim FSO As Object, SourceFolder As Object, FileItem As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set SourceFolder = FSO.GetFolder(ThisWorkbook.Path)
    Dim LastRow As Long, Sum As Long, FileNumber As Long
    Dim Min As Double, Max As Double, StDev As Double, Logavg As Double, Hr As Double, val As Double
    
    Application.ScreenUpdating = False
    With ActiveSheet
        .Cells(1, 5) = "Logger Avg."
        .Cells(1, 6) = "Logger Max."
        .Cells(1, 7) = "Logger Min."
        .Cells(1, 8) = "Std. Dev."
    End With
    FileNumber = 18
    For Each FileItem In SourceFolder.Files
        If FileItem.Name <> ThisWorkbook.Name Then
            Workbooks.Open (ThisWorkbook.Path & Application.PathSeparator & FileItem.Name)
            With ActiveSheet
                LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
                For r = 2 To LastRow
                    If .Cells(r, 4) <= 90 Then
                        .Cells(r, 12) = 10 ^ .Cells(r, 4) / 10
                        Summ = Summ + .Cells(r, 12)
                    End If
                Next r
                N = Summ / (LastRow - 1)
                Logavg = 10 * Log10(N)
                Max = Application.WorksheetFunction.Max(.Range(Cells(2, 4), .Cells(LastRow, 4)).Value)
                Min = Application.WorksheetFunction.Min(.Range(Cells(2, 4), .Cells(LastRow, 4)).Value)
                StDev = Application.WorksheetFunction.StDev(.Range(Cells(2, 4), .Cells(LastRow, 4)).Value)
                Hr = (Hour(.Cells(LastRow, 3))) - (Hour(.Cells(2, 3)))
                val = .Cells(2, 2)
            End With
        End If
        Workbooks(FileItem.Name).Save
        Workbooks(FileItem.Name).Close
        FileNumber = FileNumber + 1
        With ActiveSheet
            .Cells(FileNumber, 5) = Logavg
            .Cells(FileNumber, 6) = Max
            .Cells(FileNumber, 3) = "1"
            .Cells(FileNumber, 9) = Hr
            .Cells(FileNumber, 1) = val
        End With
    Next FileItem
    Application.ScreenUpdating = True
    End Sub
    So difficult not actually knowing what you are trying to achieve...it will be alot easier if we knew exactly step by step what you wanted to do...Also with such a huge amount of data..perhaps autofilter would be a better option.
    Perhaps someone else can assist further...
    Last edited by Sintek; 07-25-2017 at 04:06 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Alternative for my extremely slow code?
    By Warmerfare in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-23-2017, 11:16 AM
  2. [SOLVED] Macron running extremely slow after update to Excel 2016
    By masben in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-08-2016, 11:26 AM
  3. Replies: 1
    Last Post: 09-09-2013, 03:41 AM
  4. [SOLVED] Calculations running extremely slow, any suggestions
    By Dena in forum Excel General
    Replies: 1
    Last Post: 08-09-2013, 02:01 PM
  5. [SOLVED] Copy/Drag Down for Data range's running extremely slow.
    By Hyflex in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-23-2011, 01:35 AM
  6. Extremely Slow code. Please Help
    By flyersguy4 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-30-2011, 09:26 PM
  7. program running extremely slow
    By Toidz77 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-24-2009, 01:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1