How can I optimize my code

viettest

New Member
Joined
Mar 11, 2019
Messages
4
I'm going to try asking my question again as I can't come up with a faster solution for my code. Ill attach a sample of the excel sheet.

PeriodBacklog at the start of the monthVolume Receive during the monthIncident Completed during the month
Apr-17
May-17
Jun-17
Jul-17
Aug-17
Sep-17
Oct-17
Nov-17
Dec-17
Jan-18
Feb-18
Mar-18
Apr-18
May-18
Jun-18
Jul-18
Aug-18
Sep-18
Oct-18
Nov-18
Dec-18
Jan-19
Feb-19
Mar-19
Apr-19

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>

This is the table I am trying to populate. I've already written the macro for it as you can see in Module 1. The problem I'm having is speed. The original data set has over 300,000 rows and this code takes over 30 seconds to run. This is just one section in one table and there are many tables to populate. I'm trying to see how others would approach tackling this type of problem. Please let me know if you require more information.
 

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
I forgot to add a sample of the data and macro

IDDate LoggedDate Resolved
143123.4022743123.46993
241904.5395443010.32183
341941.5438342873.2952
442055.6259742922.37431
542067.5977942922.33264
642072.4654943217.52677
742072.4799742993.49816
842095.5684743202.58617
942103.4656843067.52628
1042124.5796842865.46007
1142129.6200242922.55455
1242131.4123443046.37365
1342139.4668243201.48561
1442187.5034443223.59108
1542262.4208443243.4297
1642276.3356542936.65436
1742276.494242922.37431
1842285.5553943432.64684
1942296.651642922.33264
2042299.5069842837.47481

<colgroup><col><col><col></colgroup><tbody>
</tbody>


Macro

Sub TableData()


With Application
.Calculation = xlCalculationManual
.DisplayAlerts = False
.ScreenUpdating = False
.EnableEvents = False
End With


Dim wb As Workbook: Set wb = ActiveWorkbook
Dim wsData As Worksheet: Set wsData = wb.Sheets("Data")
Dim wsTable As Worksheet: Set wsTable = wb.Sheets("Table")


With wsData
DataArray = .Range("A1").CurrentRegion.Value2
DateLoggedCol = .Cells.Find("Date Logged").Address(ReferenceStyle:=xlR1C1): DateLoggedCol = Mid(DateLoggedCol, InStr(DateLoggedCol, "C") + 1, 3)
DateResolvedCol = .Cells.Find("Date Resolved").Address(ReferenceStyle:=xlR1C1): DateResolvedCol = Mid(DateResolvedCol, InStr(DateResolvedCol, "C") + 1, 3)
End With


With wsTable
Table1Array = .Range("A1").CurrentRegion.Value2
End With


For i = 2 To UBound(DataArray)
DateResBlank = IsEmpty(DataArray(i, DateResolvedCol))
For j = 3 To UBound(Table1Array)
Period = Table1Array(j, 1)
NextPeriod = DateAdd("m", 1, Period)
If DataArray(i, DateLoggedCol) < Period And (DataArray(i, DateResolvedCol) >= Period Or DataArray(i, DateResolvedCol) = "") Then
Table1Array(j, 2) = Table1Array(j, 2) + 1
End If
If DataArray(i, DateLoggedCol) > Period And DataArray(i, DateLoggedCol) < NextPeriod Then
Table1Array(j, 3) = Table1Array(j, 3) + 1
End If
If DataArray(i, DateResolvedCol) > Period And DataArray(i, DateResolvedCol) < NextPeriod Then
Table1Array(j, 4) = Table1Array(j, 4) + 1
End If
Next j
Next i


With wsTable
.Range("A1").CurrentRegion.Value2 = Table1Array
End With


With Application
.Calculation = xlCalculationManual
.DisplayAlerts = True
.ScreenUpdating = True
.EnableEvents = True
End With


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,841
Messages
6,127,221
Members
449,371
Latest member
strawberrish

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