Results 1 to 9 of 9

Improving Macro Efficiency - taking hours to run

Threaded View

greenstar Improving Macro Efficiency -... 09-24-2021, 07:34 AM
jindon Re: Improving Macro... 09-24-2021, 08:30 AM
greenstar Re: Improving Macro... 09-24-2021, 10:06 AM
jindon Re: Improving Macro... 09-24-2021, 10:10 AM
greenstar Re: Improving Macro... 09-24-2021, 11:15 AM
jindon Re: Improving Macro... 09-24-2021, 11:23 AM
greenstar Re: Improving Macro... 09-24-2021, 12:16 PM
jindon Re: Improving Macro... 09-24-2021, 12:23 PM
greenstar Re: Improving Macro... 09-24-2021, 12:44 PM
  1. #1
    Registered User
    Join Date
    07-08-2020
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    42

    Improving Macro Efficiency - taking hours to run

    Hello

    I built this macro in the attached sample book through research and it works, however it is very very very slow.

    The sample data has only 500 rows and it takes 47s or so to run across 1 day.

    Where as actual data has 4000+ rows with across multiple days = hours!


    What the Macro is currently doing:
    Compare Column P and Q on the "Data" sheet with the Date in Column A on the "TOTAL" and if the "Data" dates fall into that range, copy AT:BF into the TOTALs sheet.
    It increments the numbers on TOTAL each time it finds a matching date for every minute.

    Note:
    I had issues comparing the dates, hence more variables to extract the hour and minute and reconstruct it. Which worked..


    Any advice on speeding this up please?
    I am flexible in the layout of Totals sheets, i.e alternatively have dates going across the top with time going down to compare against



    Full Macro:

    Option Explicit
    Public Declare PtrSafe Function GetTickCount Lib "kernel32.dll" () As Long
    
    Sub PopulateTotalSheet()
        
        Dim rng, rng2, cell, cell2 As Range
        Dim lrow5, lrow52, UsersAffected, Timer1 As Long
        Dim TotalStartDate, TotalStartTime, TotalStartDateTime, CiStartDay, CiStartTime, CiStartDateTime As Date
        Dim CiEndDay, CiEndtime, CiEndDateTime, CiTemp As Date
        Dim DataWS, TotalWS As Worksheet
        Dim i, e As Integer
        
        Timer1 = GetTickCount
        
        Set DataWS = ThisWorkbook.Sheets("Data")
        Set TotalWS = ThisWorkbook.Sheets("Total")
        
       
        lrow5 = TotalWS.Cells(Rows.Count, 1).End(xlUp).Row
        lrow52 = DataWS.Cells(Rows.Count, 1).End(xlUp).Row
        
        Set rng = TotalWS.Range(Cells(3, 1).Address, TotalWS.Cells(lrow5, 1).Address)
        
        Set rng2 = DataWS.Range(Cells(2, 16).Address, DataWS.Cells(lrow52, 16).Address)
        
        For Each cell2 In rng2
            
            On Error Resume Next
            
            CiTemp = DataWS.Range("Q" & cell2.Row).Value
            
            If Not IsEmpty(cell2.Value) And cell2.Value <> "" And CiTemp > 1 Then
                
                CiStartDay = Int(cell2.Value)
                CiStartTime = TimeSerial(Hour(cell2.Value), Minute(cell2.Value), 0)
                CiStartDateTime = CiStartDay + CiStartTime
                
                CiEndDay = Int(CiTemp)
                CiEndtime = TimeSerial(Hour(CiTemp), Minute(CiTemp), 0)
                CiEndDateTime = CiEndDay + CiEndtime
                
                For Each cell In rng
                    
                    e = 2
                    UsersAffected = 0
                    
                    TotalStartDate = Int(TotalWS.Range(Cells(cell.Row, 1).Address).Value2)
                    
                    TotalStartTime = TotalWS.Range(Cells(cell.Row, 1).Address).Value2 - TotalStartDate
                    
                    TotalStartTime = TimeSerial(Hour(TotalStartTime), Minute(TotalStartTime), 0)
                    
                    TotalStartDateTime = TotalStartDate + TotalStartTime
                    
                    If TotalStartDateTime >= CiStartDateTime And TotalStartDateTime <= CiEndDateTime Then
                        
                        For i = 46 To 58
                            
                            UsersAffected = ThisWorkbook.Sheets("Data").Range(Cells(cell2.Row, i).Address).Value + TotalWS.Range(Cells(cell.Row, e).Address).Value2 + UsersAffected
                            
                            TotalWS.Range(Cells(cell.Row, e).Address).Value2 = UsersAffected
                            
                            e = e + 1
                            UsersAffected = 0
                            
                        Next i
                        
                    Else
                        
                    End If
                    
                Next cell
                
            End If
            
        Next cell2
        
        MsgBox "Completed in: " & (GetTickCount - Timer1) / 1000, , "Seconds"
        
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Due Date Calculation by taking only working hours and days
    By Prashanth Gangala in forum Excel General
    Replies: 13
    Last Post: 10-10-2017, 05:15 PM
  2. Improving formula/workbook efficiency
    By aquixano in forum Excel General
    Replies: 0
    Last Post: 06-24-2016, 05:53 PM
  3. Replies: 15
    Last Post: 10-29-2014, 04:08 PM
  4. Need help improving Job efficiency
    By popovgor in forum Excel General
    Replies: 5
    Last Post: 03-07-2014, 01:11 PM
  5. efficiency recomendations, code taking long time to execute...
    By am_hawk in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-04-2013, 03:55 PM
  6. Replies: 2
    Last Post: 01-26-2013, 04:26 AM
  7. improving speed and efficiency
    By wishmaker in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-14-2010, 11:08 AM

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