change Array index formula to VBA

Kevw1

New Member
Joined
Dec 6, 2017
Messages
36
Hi I am currenlty using the following formula in excel, but due to the repetition it is causing significant performance issues. I was hoping that if I could add this in as a macro instead, this would improve the performance of the sheet and make the excel doc a bit more stable. I have done some VBA but not sure where to start converting this size formula.

The formula is currently in the tab "Monthly view" and copied in every cell in columns D-HW (Date's) and rows 2-561 (projects), the columns and rows will be updated with new values over time.

{=IFERROR(IF(INDEX('Project Plan'!$AF$3:INDEX('ProjectPlan'!$AF:$AF,COUNTA('Project Plan'!$AF:$AF)),MATCH($A2&D$1,'ProjectPlan'!$AE$3:INDEX('Project Plan'!$AE:$AE,COUNTA('ProjectPlan'!$AE:$AE))&'Project Plan'!$AH$3:INDEX('ProjectPlan'!$AH:$AH,COUNTA('Project Plan'!$AH:$AH)),0))="","",INDEX('ProjectPlan'!$AF$3:INDEX('Project Plan'!$AF:$AF,COUNTA('ProjectPlan'!$AF:$AF)),MATCH($A2&D$1,'Project Plan'!$AE$3:INDEX('ProjectPlan'!$AE:$AE,COUNTA('Project Plan'!$AE:$AE))&'ProjectPlan'!$AH$3:INDEX('Project Plan'!$AH:$AH,COUNTA('Project Plan'!$AH:$AH)),0))),"")}

Any help on how to improve this formula would be great, Thanks
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi so the Formula converts the following data

[TABLE="width: 245"]
<tbody>[TR]
[TD]Project Ref
[/TD]
[TD]Phase
[/TD]
[TD]EOMonth
[/TD]
[/TR]
[TR]
[TD]P1
[/TD]
[TD]Start
[/TD]
[TD]01/01/10
[/TD]
[/TR]
[TR]
[TD]P1
[/TD]
[TD]0 - Phase1
[/TD]
[TD]01/02/10
[/TD]
[/TR]
[TR]
[TD]P1
[/TD]
[TD]01 - Phase2
[/TD]
[TD]01/05/19
[/TD]
[/TR]
[TR]
[TD]P1
[/TD]
[TD]02 - Phase3
[/TD]
[TD]01/11/19
[/TD]
[/TR]
[TR]
[TD]P1
[/TD]
[TD]03 - Phase4
[/TD]
[TD]01/11/19
[/TD]
[/TR]
[TR]
[TD]P1
[/TD]
[TD]04 - Phase5
[/TD]
[TD]01/05/20
[/TD]
[/TR]
[TR]
[TD]P1
[/TD]
[TD]05 - Phase6
[/TD]
[TD]01/12/20
[/TD]
[/TR]
[TR]
[TD]P1
[/TD]
[TD]06 - Phase7
[/TD]
[TD]01/12/20
[/TD]
[/TR]
[TR]
[TD]P1
[/TD]
[TD]07 - Phase8
[/TD]
[TD]01/06/21
[/TD]
[/TR]
[TR]
[TD]P1
[/TD]
[TD]08 - Phase9
[/TD]
[TD]01/10/22
[/TD]
[/TR]
[TR]
[TD]P1
[/TD]
[TD]09 - Phase10
[/TD]
[TD]01/04/23
[/TD]
[/TR]
[TR]
[TD]P1
[/TD]
[TD]10 - Phase11
[/TD]
[TD]01/10/23
[/TD]
[/TR]
</tbody>[/TABLE]


To the following table

<strike></strike>[TABLE="width: 348"]
<tbody>[TR]
[TD]Project ID
[/TD]
[TD]Jan-10
[/TD]
[TD]Feb-10
[/TD]
[TD]Mar-10
[/TD]
[/TR]
[TR]
[TD]P1
[/TD]
[TD]Start
[/TD]
[TD]0 – Phase1
[/TD]
[TD]
[/TD]
[/TR]
</tbody>[/TABLE]

This is repeated for each of the Projects and for all dates listed in the columns D1-HW1, where there is no value then it needs to be Blank please. Hope this helps?
 
Upvote 0
Which column(s) is the data in on the sheet 'Project Plan'?
 
Upvote 0
Hi Project Plan columns are:-

Project Ref = Column AE
Phase = Column AF
EOMonth = Column AH
[TABLE="class: cms_table, width: 245"]
<tbody style="border-collapse: collapse; border-spacing: 0px 0px; font-size: 13px; margin-bottom: 0px;">[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Not 100% sure what you are looking for but the folowing code should 'create' the monthly view from the data on 'Project Plan'.

Note, this doesn't not create formulas on the sheet 'Monthly View so would need to be run whenever a (significant?) change was made on the 'Project Plan'
Code:
Option Explicit

Sub CreateMonthlyView()
Dim rngDst As Range
Dim arrIn As Variant
Dim arrOut As Variant
Dim dicProjects As Object
Dim I As Long
Dim mth As Long
Dim ky As Variant

    With Sheets("Project Plan")
        arrIn = .Range("AE3", .Range("AH" & Rows.Count).End(xlUp)).Value
    End With
    
    Set dicProjects = CreateObject("Scripting.Dictionary")
    
    For I = 1 To UBound(arrIn)
        If Not dicProjects.exists(arrIn(I, 1)) Then
            ReDim arrOut(1 To 228)
            mth = DateDiff("m", DateSerial(2010, 1, 1), arrIn(I, 4)) + 1
            arrOut(mth) = arrIn(I, 2)
            dicProjects.Add arrIn(I, 1), arrOut
            
        Else
            arrOut = dicProjects(arrIn(I, 1))
            
            mth = DateDiff("m", DateSerial(2010, 1, 1), arrIn(I, 4)) + 1
            
            arrOut(mth) = arrIn(I, 2)
            dicProjects(arrIn(I, 1)) = arrOut
            
        End If
        
    Next I
    
    Set rngDst = Sheets("Monthly View").Range("D2")
    
    For Each ky In dicProjects.keys
        rngDst.Offset(, -3).Value = ky
        rngDst.Resize(, 228).Value = dicProjects(ky)
        Set rngDst = rngDst.Offset(1)
    Next ky
    
End Sub
 
Upvote 0
Hi thanks for this, but for some reason it is throughing an error Subscript out of range at
arrOut(mth) = arrIn(I, 2)
 
Upvote 0
Does this work without error?
Code:
Option Explicit

Sub CreateMonthlyView()
Dim rngDst As Range
Dim arrIn As Variant
Dim arrOut As Variant
Dim dicProjects As Object
Dim I As Long
Dim mth As Long
Dim ky As Variant

    With Sheets("Project Plan")
        arrIn = .Range("AE3", .Range("AH" & Rows.Count).End(xlUp)).Value
    End With
    
    Set dicProjects = CreateObject("Scripting.Dictionary")
    
    For I = 1 To UBound(arrIn)
        If Not dicProjects.exists(arrIn(I, 1)) Then
            ReDim arrOut(1 To 228)
            mth = DateDiff("m", DateSerial(2010, 1, 1), arrIn(I, 4)) + 1

            If mth>1 And mth<229 then
                arrOut(mth) = arrIn(I, 2)
            End If

            dicProjects.Add arrIn(I, 1), arrOut
            
        Else
            arrOut = dicProjects(arrIn(I, 1))
            
            mth = DateDiff("m", DateSerial(2010, 1, 1), arrIn(I, 4)) + 1
            
            If mth>1 And mth<229 then
                arrOut(mth) = arrIn(I, 2)
            End If

            dicProjects(arrIn(I, 1)) = arrOut
            
        End If
        
    Next I
    
    Set rngDst = Sheets("Monthly View").Range("D2")
    
    For Each ky In dicProjects.keys
        rngDst.Offset(, -3).Value = ky
        rngDst.Resize(, 228).Value = dicProjects(ky)
        Set rngDst = rngDst.Offset(1)
    Next ky
    
End Sub
 
Upvote 0
Hi Thanks all working as expected, just takes about 3-4 mins to fully run, but does what it needs to thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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