jewkes6000
Board Regular
- Joined
- Mar 25, 2020
- Messages
- 60
- Office Version
- 365
- Platform
- Windows
To import data from multiple projects and view in a side-by-side manner, I had a macro which was looping through data on two sheets, finding data which matched some criteria, and then copying that data into specified rows and columns. The code I came up simply looped through the data on both sheets and it worked; however, it took about 30 seconds per project to import. With 8 projects, it could take anywhere from 4 to 5 minutes to complete. While this isn't too painful, it was suggested that I use arrays as they are MUCH MUCH faster. Below is my best attempt to use arrarys; however, it took even longer than my original method and I was only trying to import one project. Can anyone let me know what I can do to speed up this macro? Below is the code I've tried to use with arrays. I've also included a link to actual file. In the file, you can also view my old macro titled "Sub Compare_Projects_NoArrarys ()".
Looping with Arrarys Still Taking Too Long.xlsm
drive.google.com
VBA Code:
Sub Compare_Projects()
Dim Toolary As Variant, Data_ary As Variant, PrjTitle_ary As Variant, CurrentAry As Variant
Dim r As Long, nr As Long, x As Long, c As Long, CurrentCostCod As Long
Dim Cl As Range
Dim Project1 As String, Project2 As String, Project3 As String, Project4 As String, Project5 As String, Project6 As String, Project7 As String, Project8 As String
Application.ScreenUpdating = False
With Sheets("Setup Page")
Typology = .Range("L18")
Project1 = .Range("U11").Value
Project2 = .Range("U12").Value
Project3 = .Range("U13").Value
Project4 = .Range("U14").Value
Project5 = .Range("U15").Value
Project6 = .Range("U16").Value
Project7 = .Range("U17").Value
Project8 = .Range("U18").Value
End With
'Put data into the arrarys (Toolary & Data_ary)
Data_ary = Sheets("Cost Data").Range("A1").CurrentRegion.Value2
With Sheets("Compare Tool")
Toolary = .Range("A28:DV" & .Range("U" & Rows.Count).End(xlUp).Row).Value2
End With
'Project 1
'Check if Project field is blank
Sheets("Setup Page").Select
If Range("U11") = "" Then GoTo Project2
With Sheets("Cost Data")
FirstRowDB = .Range("A:A").Find(What:=Project1, searchdirection:=xlNext).Row 'xlNext starts from top
GSFPrj = .Cells(FirstRowDB, 13)
GSFTypology = .Cells(FirstRowDB, 18)
End With
'Copy the GSF area & Total Project cost and paste into the top of the "Compare Tool" tab
Sheets("Prj Info").Select
FindPrj = Application.Match(Project1, Range("A:A"), 0)
Total_Prj_Cost = Sheets("Prj Info").Cells(FindPrj, 16)
Sheets("Compare Tool").Range("AD19") = GSFTypology
Sheets("Compare Tool").Range("AD16") = GSFPrj
Sheets("Compare Tool").Range("AD15") = Total_Prj_Cost
For r = 1 To UBound(Toolary)
If Toolary(r, 5) = "Single" Or Toolary(r, 5) = "T2 Head" Then
CurrentCostCode = Toolary(r, 21)
CurrentT0 = Toolary(r, 9)
For x = 2 To UBound(Data_ary)
If Data_ary(x, 1) = Project1 And Data_ary(x, 34) = CurrentCostCode And Data_ary(x, 22) = CurrentT0 And Data_ary(x, 17) = Typology Then
Sheets("Compare Tool").Range("X" & r + 27).Value = Data_ary(x, 37)
Sheets("Compare Tool").Range("Y" & r + 27).Value = Data_ary(x, 38)
Sheets("Compare Tool").Range("Z" & r + 27).Value = Data_ary(x, 39)
Sheets("Compare Tool").Range("AA" & r + 27).Value = Data_ary(x, 40)
Sheets("Compare Tool").Range("AB" & r + 27).Value = Data_ary(x, 41)
Sheets("Compare Tool").Range("AC" & r + 27).Value = Data_ary(x, 42)
Sheets("Compare Tool").Range("AD" & r + 27).Value = Data_ary(x, 43)
If Data_ary(x, 44) <> "" Then
Sheets("Compare Tool").Range("AE" & r + 27).Value = Data_ary(x, 44)
Sheets("Compare Tool").Range("AF" & r + 27).Value = Data_ary(x, 45)
End If
End If
Next x
End If
Next r
'Project 2
Project2:
Application.ScreenUpdating = True
End Sub