Speed up excel file (maybe VBA?)

spanishnick

New Member
Joined
Nov 10, 2021
Messages
12
Platform
  1. Windows
Hi,

I have a very large file (100'000+ rows and 40+ columns), each column containing property characteristics. Each property characteristic is assigned a weight on a different sheet (using a combination of Index+Match, i.e. INDEX('Sheet1'!$E$96:$E$143, MATCH(A2, 'Sheet1'!$C$96:$C$143, 0)) and, with this weight, I am estimating the property value by multiplying each weight (so I only have a single column with PRODUCT(INDEX(...), INDEX(...), ....). I would like to give the option for individuals to "play" with the weights and see the impact on total property value, so need the calculations to be updated, and setting manual calculations does not solve the issue as it can still take 2-3 minutes to generate the final output. I was wondering if there was an alternative (i.e. using VBA?).

Thank you
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Your problem is similar to Dorians problem on this thread:
Is there a smarter way to do this: 1-Copy Formula >> 2-Paste Formula Down >> 3-Hardcode Pasted Range process I use frequently?
At the end of that thread I posted some code that did the same as Index/Match, but entirely in VBA by using the dictionary object. the code is very fast. The main point about doing it this way is so that you can control exactly what get recalculated and when . I suspect by careful design of what get recalculated at what point by putting it all in VBA, when you are playing around with weights you will get a usable workbook.
 
Upvote 0
Hi @offthelip,

Thank you for your response and your help. I am very new to VBA (first time using it in fact!) and I'm not sure I was able to edit the code (see beliw)

VBA Code:
Sub dictionary()
 ' this does an index match by using the dictionary object
   Dim Ary As Variant
   Dim i As Long
   Dim Dic As Object
  
   Dim outarr(1 To 161, 1 To 1)
   Set Dic = CreateObject("Scripting.dictionary")
   With Sheets("Baseline Weights")
      Ary = .Range("B5", .Range("B" & Rows.Count).End(xlUp).Offset(, 4)).Value2
   For i = 1 To UBound(Ary)
      Dic(Ary(i, 1)) = Ary(i, 3)
   Next i
  
   For i = 1 To 160
    outarr(i, 1) = Dic(Ary(i, 4))
   Next i
   .Range("au2:au161") = outarr
   End With
  
   End Sub

The data I would like to generate is in columns AJ, AL, AN and AP in sheet "Data2" and column AO in sheet "Data", using weights from "Baseline Weights" sheet (see below a subset of the data). If you could provide me with additional assistance, I would be eternally grateful!

* Sheet Baseline Weights
Test data.xlsx
BCDE
2FeaturesResultBaseline WeightTest Weight
3Coefficient231,859.13231,859.13
4Square Root of Surface Area (in sq ft)
5Original UseDomestic+ 0 %+ 0 %
6Bank+ 106 %+ 106 %
7Car Dealership+ 24 %+ 24 %
8Filling Station+ 53 %+ 53 %
9Guesthouse+ 4 %+ 4 %
10Hotel+ 70 %+ 70 %
11Industrial Manufacturing- 37 %- 37 %
12Industrial Warehouse- 47 %- 47 %
13Mixed Retail / Office+ 13 %+ 13 %
14Motor Garage- 30 %- 30 %
15Office+ 37 %+ 37 %
16Private Clinic+ 72 %+ 72 %
17Private School+ 44 %+ 44 %
18Retail+ 3 %+ 3 %
19Supermarket+ 7 %+ 7 %
20Institutional Property TypeGovernment Offices+ 79 %+ 79 %
21Parliament Buildings+ 79 %+ 79 %
22Police Buildings+ 70 %+ 70 %
23Police Compounds+ 70 %+ 70 %
24Court Buildings+ 79 %+ 79 %
25Municipal Offices+ 79 %+ 79 %
26Army barracks and installations+ 43 %+ 43 %
27Fire Station+ 43 %+ 43 %
28Domestic Ground UseYes+ 0 %+ 0 %
29No+ 5 %+ 5 %
30Street QualityBad- 10 %- 10 %
31Average+ 0 %+ 0 %
32Good+ 4 %+ 4 %
33Street LightingYes+ 0 %+ 0 %
34No+ 0 %+ 0 %
35Number of LanesNone- 15 %- 15 %
36One+ 0 %+ 0 %
37Two+ 2 %+ 2 %
38Four+ 4 %+ 4 %
39Street AccessEasy+ 0 %+ 0 %
40Difficult- 7 %- 7 %
41DrainageYes+ 2 %+ 2 %
42No+ 0 %+ 0 %
43FeaturesBeach+ 24 %+ 24 %
44Environmental Hazard- 15 %- 15 %
45Main Road with High Visibility+ 18 %+ 18 %
46Informal Settlement- 21 %- 21 %
47Commercial Corridor+ 25 %+ 25 %
48High-valued Area+ 0 %+ 0 %
49Potential to BuildYes+ 3 %+ 3 %
50No+ 0 %+ 0 %
51WaterYes+ 2 %+ 2 %
52No+ 0 %+ 0 %
53Grade of DesignTraditional+ 0 %+ 0 %
54Modern/Architectural+ 0 %+ 0 %
55Wall MaterialLow-value material- 49 %- 49 %
56Medium-value material- 28 %- 28 %
57High-value material+ 0 %+ 0 %
58Wall QualityBad- 11 %- 11 %
59Average+ 0 %+ 0 %
60Good+ 25 %+ 25 %
61Wall FinishNo finish+ 0 %+ 0 %
62Painted or Whitewashed+ 0 %+ 0 %
63Aluco Bond Metal+ 0 %+ 0 %
64Tile+ 0 %+ 0 %
65Glazed Curtain Wall+ 0 %+ 0 %
66Stone+ 0 %+ 0 %
67Fence MaterialNo fence+ 0 %+ 0 %
68Low-value material+ 0 %+ 0 %
69Medium-value material+ 0 %+ 0 %
70High-value material+ 0 %+ 0 %
71Fence ConditionNo fence+ 0 %+ 0 %
72Bad+ 0 %+ 0 %
73Average+ 0 %+ 0 %
74Good+ 0 %+ 0 %
75Roof MaterialNot Visible+ 0 %+ 0 %
76Low-value material- 21 %- 21 %
77High-value material+ 0 %+ 0 %
78Roof ConditionNot Visible+ 0 %+ 0 %
79Bad- 38 %- 38 %
80Average+ 0 %+ 0 %
81Good+ 20 %+ 20 %
82WindowsLow-value material- 46 %- 46 %
83Medium-value material+ 0 %+ 0 %
84High-value material+ 15 %+ 15 %
85Air ConditionYes+ 54 %+ 54 %
86No+ 0 %+ 0 %
87SecurityYes+ 43 %+ 43 %
88No+ 0 %+ 0 %
89GarageYes+ 0 %+ 0 %
90No+ 0 %+ 0 %
91OutbuildingYes+ 10 %+ 10 %
92No+ 0 %+ 0 %
93PoolYes+ 101 %+ 101 %
94No+ 0 %+ 0 %
95VerandaYes+ 3 %+ 3 %
96No+ 0 %+ 0 %
97Location (Ward)399+ 0 %+ 0 %
98400+ 10 %+ 10 %
99401- 3 %- 3 %
100402+ 27 %+ 27 %
101403+ 3 %+ 3 %
102404- 16 %- 16 %
103405- 6 %- 6 %
104406- 5 %- 5 %
105407+ 7 %+ 7 %
106408+ 10 %+ 10 %
107409- 9 %- 9 %
108410- 14 %- 14 %
109411- 35 %- 35 %
110412+ 22 %+ 22 %
111413+ 11 %+ 11 %
112414+ 52 %+ 52 %
113415+ 45 %+ 45 %
114416+ 15 %+ 15 %
115417+ 56 %+ 56 %
116418+ 0 %+ 0 %
117419+ 5 %+ 5 %
118420+ 68 %+ 68 %
119421+ 106 %+ 106 %
120422+ 60 %+ 60 %
121423+ 36 %+ 36 %
122424+ 38 %+ 38 %
123425+ 78 %+ 78 %
124426+ 18 %+ 18 %
125427+ 240 %+ 240 %
126428+ 189 %+ 189 %
127429+ 192 %+ 192 %
128430+ 231 %+ 231 %
129431+ 233 %+ 233 %
130432+ 139 %+ 139 %
131433+ 176 %+ 176 %
132434+ 26 %+ 26 %
133435+ 71 %+ 71 %
134436+ 237 %+ 237 %
135437+ 178 %+ 178 %
136438+ 196 %+ 196 %
137439+ 62 %+ 62 %
138440+ 120 %+ 120 %
139441+ 123 %+ 123 %
140442+ 126 %+ 126 %
141443+ 129 %+ 129 %
142444+ 98 %+ 98 %
143445+ 41 %+ 41 %
144446+ 180 %+ 180 %
145Special PropertiesBollore Office+ 3819 %+ 3819 %
146Bollore Warehouse+ 1426 %+ 1426 %
147Bollore Garage+ 1894 %+ 1894 %
148Dangote Office+ 346 %+ 346 %
149Dangote Warehouse+ 73 %+ 73 %
150SLPA Warehouse+ 119 %+ 119 %
151SLPA Office+ 461 %+ 461 %
152SLPA House Form+ 310 %+ 310 %
153SLPA Industrial+ 158 %+ 158 %
154SLPA Motor Garage+ 186 %+ 186 %
155SLPA Canteen+ 322 %+ 322 %
156Nectar House Form+ 174 %+ 174 %
157Nectar Warehouse+ 46 %+ 46 %
158Nectar Cargo+ 182 %+ 182 %
159Nectar Office+ 275 %+ 275 %
160Leocem Industrial Manufacturing- 37 %- 37 %
161Leocem Office+ 37 %+ 37 %
Baseline Weights
Cell Formulas
RangeFormula
E5:E161E5=D5


* Sheet Data
Test data.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAP
1wardzone_cleanmarker2surface_areaoriginal_usedomestic_useroad_statusstreet_lightingnumber_lanesstreet_accesshas_drainagebeachhazardroad_visibinformal_settlementcommercial_corridorhigh_valued_areahigh_valued_area_namehas_potential_to_buildhas_watergrade_of_designwall_categorystate_of_repairwall_finishfence_categoryfence_conditionroof_categoryroof_repairwindows_categoryairconditioningsecurityowns_garagepoolowns_outbuildingveranda annual_rent ln_annual_rent model2_exp test_exp model2_error test_exp test_error
2399116865.98House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesYesModern/ArchitecturalHigh-value materialGoodPainted or WhitewashedNo fenceNo fenceLow-value materialGoodHigh-value materialYesNoYesNoNoYes3250000017.29683221635639,267,161.391.2239,267,1621.21
3399121628.53House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAverageNo finishNo fenceNo fenceLow-value materialAverageMedium-value materialNoNoNoNoNoYes450000015.31966838487.57,057,747.791.667,057,7481.57
4399131764.73House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAverageNo finishNo fenceNo fenceLow-value materialAverageMedium-value materialNoNoNoNoYesYes325000014.99426871429.58,081,650.522.258,081,6512.49
5399141702.31House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAveragePainted or WhitewashedHigh-value materialAverageLow-value materialGoodHigh-value materialNoNoNoNoNoYes725000015.7965104581029,957,874.951.399,957,8751.37
639915380.42House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalLow-value materialAverageNo finishNo fenceNo fenceLow-value materialAverageLow-value materialNoNoNoNoNoNo40000012.8992877425.8125912,066.782.36912,0672.28
7399162709.53House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAveragePainted or WhitewashedNo fenceNo fenceLow-value materialGoodHigh-value materialNoNoNoNoYesYes825000015.92571183294213,819,330.541.6913,819,3311.68
8399171821.36House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAveragePainted or WhitewashedNo fenceNo fenceLow-value materialAverageMedium-value materialNoNoNoNoNoYes737500015.813672320297,463,905.711.027,463,9061.01
9399182967.13House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesYesTraditionalHigh-value materialGoodPainted or WhitewashedNo fenceNo fenceLow-value materialGoodHigh-value materialNoNoNoNoNoYes1050000016.16691673088116,761,998.551.3716,761,9991.60
10399191354.39House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAveragePainted or WhitewashedNo fenceNo fenceLow-value materialAverageMedium-value materialNoNoNoNoNoYes812500015.91056236402.56,436,356.940.806,436,3570.79
Data
Cell Formulas
RangeFormula
AO2:AO10AO2=PRODUCT((INDEX('Baseline Weights'!$E$97:$E$144,MATCH(A2,'Baseline Weights'!$C$97:$C$144,0))+1), (INDEX('Baseline Weights'!$E$28:$E$29,MATCH(F2,'Baseline Weights'!$C$28:$C$29,0))+1), (INDEX('Baseline Weights'!$E$30:$E$32,MATCH(G2,'Baseline Weights'!$C$30:$C$32,0))+1), (IF(ISNA(INDEX('Baseline Weights'!$E$33:$E$34,MATCH(H2,'Baseline Weights'!$C$33:$C$34,0))),1,INDEX('Baseline Weights'!$E$33:$E$34,MATCH(H2,'Baseline Weights'!$C$33:$C$34,0)))+1), (INDEX('Baseline Weights'!$E$35:$E$38,MATCH(I2,'Baseline Weights'!$C$35:$C$38,0))+1), (INDEX('Baseline Weights'!$E$39:$E$40,MATCH(J2,'Baseline Weights'!$C$39:$C$40,0))+1), (INDEX('Baseline Weights'!$E$41:$E$42,MATCH(K2,'Baseline Weights'!$C$41:$C$42,0))+1), IF(L2="Yes",'Baseline Weights'!$D$43+1,1), IF(M2="Yes",'Baseline Weights'!$D$44+1,1), IF(N2="Yes",'Baseline Weights'!$D$45+1,1), IF(O2="Yes",'Baseline Weights'!$D$46+1,1), IF(P2="Yes",'Baseline Weights'!$D$47+1,1), IF(Q2="Yes",'Baseline Weights'!$D$48+1,1), (INDEX('Baseline Weights'!$E$49:$E$50, MATCH(S2, 'Baseline Weights'!$C$49:$C$50, 0))+1), (INDEX('Baseline Weights'!$E$51:$E$52, MATCH(T2, 'Baseline Weights'!$C$51:$C$52, 0))+1), IF(ISNA(INDEX('Baseline Weights'!$E$53:$E$54, MATCH(U2, 'Baseline Weights'!$C$53:$C$54, 0))), 1, INDEX('Baseline Weights'!$E$53:$E$54, MATCH(U2, 'Baseline Weights'!$C$53:$C$54, 0))+1), (INDEX('Baseline Weights'!$E$55:$E$57, MATCH(V2, 'Baseline Weights'!$C$55:$C$57, 0))+1), (INDEX('Baseline Weights'!$E$58:$E$60, MATCH(W2, 'Baseline Weights'!$C$58:$C$60, 0))+1), (INDEX('Baseline Weights'!$E$61:$E$66, MATCH(X2, 'Baseline Weights'!$C$61:$C$66, 0))+1), (INDEX('Baseline Weights'!$E$67:$E$70, MATCH(Y2, 'Baseline Weights'!$C$67:$C$70, 0))+1), (INDEX('Baseline Weights'!$E$71:$E$74, MATCH(Z2, 'Baseline Weights'!$C$71:$C$74, 0))+1), (INDEX('Baseline Weights'!$E$71:$E$74, MATCH(Z2, 'Baseline Weights'!$C$71:$C$74, 0))+1), (INDEX('Baseline Weights'!$E$75:$E$77, MATCH(AA2, 'Baseline Weights'!$C$75:$C$77, 0))+1), (INDEX('Baseline Weights'!$E$78:$E$81, MATCH(AB2, 'Baseline Weights'!$C$78:$C$81, 0))+1), (INDEX('Baseline Weights'!$E$82:$E$84, MATCH(AC2, 'Baseline Weights'!$C$82:$C$84, 0))+1), (INDEX('Baseline Weights'!$E$85:$E$86, MATCH(AD2, 'Baseline Weights'!$C$85:$C$86, 0))+1), (INDEX('Baseline Weights'!$E$87:$E$88, MATCH(AE2, 'Baseline Weights'!$C$87:$C$88, 0))+1), (IF(ISNA(INDEX('Baseline Weights'!$E$89:$E$90, MATCH(AF2, 'Baseline Weights'!$C$89:$C$90, 0))), 1, INDEX('Baseline Weights'!$E$89:$E$90, MATCH(AF2, 'Baseline Weights'!$C$89:$C$90, 0)))+1), (INDEX('Baseline Weights'!$E$93:$E$94, MATCH(AG2, 'Baseline Weights'!$C$93:$C$94, 0))+1), (INDEX('Baseline Weights'!$E$91:$E$92, MATCH(AH2, 'Baseline Weights'!$C$91:$C$92, 0))+1), (INDEX('Baseline Weights'!$E$95:$E$96, MATCH(AI2, 'Baseline Weights'!$C$95:$C$96, 0))+1), SQRT(D2), 'Baseline Weights'!$D$3)
AP2:AP10AP2=AO2/AJ2
 
Upvote 0
* Sheet Data2
Test data.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASAT
1wardoriginal_useinstitutional_typedomestic_useroad_statusstreet_lightingnumber_lanesstreet_accesshas_drainagebeachhazardroad_visibinformal_settlementcommercial_corridorhigh_valued_areahas_potential_to_buildhas_watergrade_of_designwall_categorystate_of_repairwall_finishfence_categoryfence_conditionroof_categoryroof_repairwindows_categoryairconditioningsecurityowns_garagepoolowns_outbuildingverandacomm_surface_areadom_surface_areainst_surface_area base commercial test_commercial domestic test_domestic institutional test_institutional test_property quintile test_property test_quintile
2432YesAverageNoOneEasyYesNoNoNoNoNoNoNoYesTraditionalHigh-value materialAverageNo finishLow-value materialBadLow-value materialAverageMedium-value materialNoNoNoNoYesYes319.26516,035.34--9,220,441.129,220,441.19--9,220,441.1249,220,441.194
3418YesBadNoNoneDifficultNoNoNoNoNoNoNoNoNoTraditionalLow-value materialGoodPainted or WhitewashedNo fenceNo fenceLow-value materialGoodLow-value materialNoNoNoNoYesYes227.2460,993.11--919,439.52919,439.53--919,439.521919,439.531
4419YesBadNoNoneDifficultNoNoNoNoNoNoNoYesNoTraditionalMedium-value materialAverageNo finishNo fenceNo fenceLow-value materialGoodLow-value materialNoNoNoNoNoYes627.8567,727.79--1,697,050.921,697,050.93--1,697,050.9211,697,050.931
5446YesAverageNoOneDifficultNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialGoodPainted or WhitewashedNo fenceNo fenceLow-value materialGoodHigh-value materialNoNoNoNoNoYes2079.69872,882.57--39,806,603.2839,806,603.57--39,806,603.28539,806,603.575
6433YesAverageNoOneEasyYesNoNoNoNoNoNoNoYesTraditionalHigh-value materialAveragePainted or WhitewashedNo fenceNo fenceLow-value materialAverageLow-value materialNoNoNoNoYesNo219.82312,426.00--4,632,130.254,632,130.28--4,632,130.2534,632,130.283
7400YesAverageNoOneEasyYesNoNoNoNoNoNoYesYesTraditionalHigh-value materialAveragePainted or WhitewashedHigh-value materialAverageLow-value materialAverageHigh-value materialNoYesNoNoNoYes2908.71365,723.32--19,724,363.9419,724,364.09--19,724,363.94519,724,364.095
8407DomesticYesBadNoOneEasyYesNoNoNoNoNoNoNoYesTraditionalHigh-value materialGoodPainted or WhitewashedNo fenceNo fenceLow-value materialGoodMedium-value materialNoNoNoNoNoYes335.892351.23283,534.825,196,428.335,196,428.3713,748,457.0613,748,457.16--18,944,885.39518,944,885.535
9443YesAverageNoNoneEasyNoNoNoNoNoNoNoYesYesTraditionalHigh-value materialAveragePainted or WhitewashedHigh-value materialAverageLow-value materialAverageMedium-value materialNoYesNoNoNoYes1072.11551,716.99--18,064,919.4818,064,919.61--18,064,919.48518,064,919.615
10408YesBadNoNoneDifficultNoNoYesNoYesNoNoNoNoTraditionalHigh-value materialAveragePainted or WhitewashedNo fenceNo fenceLow-value materialAverageMedium-value materialNoNoNoNoNoYes700.4899,145.18--2,624,034.082,624,034.10--2,624,034.0822,624,034.102
Data2
Cell Formulas
RangeFormula
AR2:AR10,AT2:AT10AR2=IF(AQ2<=PERCENTILE.INC(AQ:AQ,0.2),1,IF(AQ2<=PERCENTILE.INC(AQ:AQ,0.4),2,IF(AQ2<=PERCENTILE.INC(AQ:AQ,0.6),3,IF(AQ2<=PERCENTILE.INC(AQ:AQ,0.8),4,5))))
AS2:AS10AS2=AL2+AN2+AP2
AJ2:AJ10AJ2=PRODUCT((INDEX('Baseline Weights'!$E$97:$E$144,MATCH(A2,'Baseline Weights'!$C$97:$C$144,0))+1), (INDEX('Baseline Weights'!$E$28:$E$29,MATCH(D2,'Baseline Weights'!$C$28:$C$29,0))+1), (INDEX('Baseline Weights'!$E$30:$E$32,MATCH(E2,'Baseline Weights'!$C$30:$C$32,0))+1), (IF(ISNA(INDEX('Baseline Weights'!$E$33:$E$34,MATCH(F2,'Baseline Weights'!$C$33:$C$34,0))),1,INDEX('Baseline Weights'!$E$33:$E$34,MATCH(F2,'Baseline Weights'!$C$33:$C$34,0)))+1), (INDEX('Baseline Weights'!$E$35:$E$38,MATCH(G2,'Baseline Weights'!$C$35:$C$38,0))+1), (INDEX('Baseline Weights'!$E$39:$E$40,MATCH(H2,'Baseline Weights'!$C$39:$C$40,0))+1), (INDEX('Baseline Weights'!$E$41:$E$42,MATCH(I2,'Baseline Weights'!$C$41:$C$42,0))+1), IF(J2="Yes",'Baseline Weights'!$D$43+1,1), IF(K2="Yes",'Baseline Weights'!$D$44+1,1), IF(L2="Yes",'Baseline Weights'!$D$45+1,1), IF(M2="Yes",'Baseline Weights'!$D$46+1,1), IF(N2="Yes",'Baseline Weights'!$D$47+1,1), IF(O2="Yes",'Baseline Weights'!$D$48+1,1), (INDEX('Baseline Weights'!$E$49:$E$50, MATCH(P2, 'Baseline Weights'!$C$49:$C$50, 0))+1), (INDEX('Baseline Weights'!$E$51:$E$52, MATCH(Q2, 'Baseline Weights'!$C$51:$C$52, 0))+1), IF(ISNA(INDEX('Baseline Weights'!$E$53:$E$54, MATCH(R2, 'Baseline Weights'!$C$53:$C$54, 0))), 1, INDEX('Baseline Weights'!$E$53:$E$54, MATCH(R2, 'Baseline Weights'!$C$53:$C$54, 0))+1), (INDEX('Baseline Weights'!$E$55:$E$57, MATCH(S2, 'Baseline Weights'!$C$55:$C$57, 0))+1),(INDEX('Baseline Weights'!$E$58:$E$60, MATCH(T2, 'Baseline Weights'!$C$58:$C$60, 0))+1),(INDEX('Baseline Weights'!$E$61:$E$66, MATCH(U2, 'Baseline Weights'!$C$61:$C$66, 0))+1),(INDEX('Baseline Weights'!$E$67:$E$70, MATCH(V2, 'Baseline Weights'!$C$67:$C$70, 0))+1),(INDEX('Baseline Weights'!$E$71:$E$74, MATCH(W2, 'Baseline Weights'!$C$71:$C$74, 0))+1),(INDEX('Baseline Weights'!$E$71:$E$74, MATCH(W2, 'Baseline Weights'!$C$71:$C$74, 0))+1),(INDEX('Baseline Weights'!$E$75:$E$77, MATCH(X2, 'Baseline Weights'!$C$75:$C$77, 0))+1),(INDEX('Baseline Weights'!$E$78:$E$81, MATCH(Y2, 'Baseline Weights'!$C$78:$C$81, 0))+1),(INDEX('Baseline Weights'!$E$82:$E$84, MATCH(Z2, 'Baseline Weights'!$C$82:$C$84, 0))+1),(INDEX('Baseline Weights'!$E$85:$E$86, MATCH(AA2, 'Baseline Weights'!$C$85:$C$86, 0))+1),(INDEX('Baseline Weights'!$E$87:$E$88, MATCH(AB2, 'Baseline Weights'!$C$87:$C$88, 0))+1),(IF(ISNA(INDEX('Baseline Weights'!$E$89:$E$90, MATCH(AC2, 'Baseline Weights'!$C$89:$C$90, 0))), 1, INDEX('Baseline Weights'!$E$89:$E$90, MATCH(AC2, 'Baseline Weights'!$C$89:$C$90, 0)))+1),(INDEX('Baseline Weights'!$E$93:$E$94, MATCH(AD2, 'Baseline Weights'!$C$93:$C$94, 0))+1),(INDEX('Baseline Weights'!$E$91:$E$92, MATCH(AE2, 'Baseline Weights'!$C$91:$C$92, 0))+1),(INDEX('Baseline Weights'!$E$95:$E$96, MATCH(AF2, 'Baseline Weights'!$C$95:$C$96, 0))+1),'Baseline Weights'!$D$3)
AL2:AL10AL2=AJ2*SQRT(AG2)*IF(ISNA(VLOOKUP(B2, 'Baseline Weights'!C$5:E$19, 3, FALSE)), 1, VLOOKUP(B2, 'Baseline Weights'!C$5:E$19, 3, FALSE)+1)
AN2:AN10AN2=AJ2*SQRT(AH2)
AP2AP2=AJ2*SQRT(AI2)*'Baseline Weights'!$D$3*IF(ISNA(VLOOKUP(C2, 'Baseline Weights'!C$20:E$27, 3, FALSE)), IF(ISNA(VLOOKUP(C2, 'Baseline Weights'!C145:E161, 3, FALSE)), 1, VLOOKUP(C2, 'Baseline Weights'!C145:E161, 3, FALSE)+1), VLOOKUP(C2, 'Baseline Weights'!C$20:E$27, 3, FALSE)+1)
AP3:AP10AP3=(INDEX('Baseline Weights'!$E$97:$E$144,MATCH(A3,'Baseline Weights'!$C$97:$C$144,0))+1)*(INDEX('Baseline Weights'!$E$28:$E$29,MATCH(D3,'Baseline Weights'!$C$28:$C$29,0))+1)*(INDEX('Baseline Weights'!$E$30:$E$32,MATCH(E3,'Baseline Weights'!$C$30:$C$32,0))+1)*(IF(ISNA(INDEX('Baseline Weights'!$E$33:$E$34,MATCH(F3,'Baseline Weights'!$C$33:$C$34,0))),1,INDEX('Baseline Weights'!$E$33:$E$34,MATCH(F3,'Baseline Weights'!$C$33:$C$34,0)))+1)*(INDEX('Baseline Weights'!$E$35:$E$38,MATCH(G3,'Baseline Weights'!$C$35:$C$38,0))+1)*(INDEX('Baseline Weights'!$E$39:$E$40,MATCH(H3,'Baseline Weights'!$C$39:$C$40,0))+1)* (INDEX('Baseline Weights'!$E$41:$E$42,MATCH(I3,'Baseline Weights'!$C$41:$C$42,0))+1)*IF(J3="Yes",'Baseline Weights'!$D$43+1,1)*IF(K3="Yes",'Baseline Weights'!$D$44+1,1)*IF(L3="Yes",'Baseline Weights'!$D$45+1,1)*IF(M3="Yes",'Baseline Weights'!$D$46+1,1)*IF(N3="Yes",'Baseline Weights'!$D$47+1,1)*IF(O3="Yes",'Baseline Weights'!$D$48+1,1)*(INDEX('Baseline Weights'!$E$49:$E$50, MATCH(P3, 'Baseline Weights'!$C$49:$C$50, 0))+1)*(INDEX('Baseline Weights'!$E$51:$E$52, MATCH(Q3, 'Baseline Weights'!$C$51:$C$52, 0))+1)*IF(ISNA(INDEX('Baseline Weights'!$E$53:$E$54, MATCH(R3, 'Baseline Weights'!$C$53:$C$54, 0))), 1, INDEX('Baseline Weights'!$E$53:$E$54, MATCH(R3, 'Baseline Weights'!$C$53:$C$54, 0))+1)*(INDEX('Baseline Weights'!$E$55:$E$57, MATCH(S3, 'Baseline Weights'!$C$55:$C$57, 0))+1)*(INDEX('Baseline Weights'!$E$58:$E$60, MATCH(T3, 'Baseline Weights'!$C$58:$C$60, 0))+1)*(INDEX('Baseline Weights'!$E$61:$E$66, MATCH(U3, 'Baseline Weights'!$C$61:$C$66, 0))+1)*(INDEX('Baseline Weights'!$E$67:$E$70, MATCH(V3, 'Baseline Weights'!$C$67:$C$70, 0))+1)*(INDEX('Baseline Weights'!$E$71:$E$74, MATCH(W3, 'Baseline Weights'!$C$71:$C$74, 0))+1)*(INDEX('Baseline Weights'!$E$71:$E$74, MATCH(W3, 'Baseline Weights'!$C$71:$C$74, 0))+1)*(INDEX('Baseline Weights'!$E$75:$E$77, MATCH(X3, 'Baseline Weights'!$C$75:$C$77, 0))+1)*(INDEX('Baseline Weights'!$E$78:$E$81, MATCH(Y3, 'Baseline Weights'!$C$78:$C$81, 0))+1)*(INDEX('Baseline Weights'!$E$82:$E$84, MATCH(Z3, 'Baseline Weights'!$C$82:$C$84, 0))+1)*(INDEX('Baseline Weights'!$E$85:$E$86, MATCH(AA3, 'Baseline Weights'!$C$85:$C$86, 0))+1)*(INDEX('Baseline Weights'!$E$87:$E$88, MATCH(AB3, 'Baseline Weights'!$C$87:$C$88, 0))+1)*(IF(ISNA(INDEX('Baseline Weights'!$E$89:$E$90, MATCH(AC3, 'Baseline Weights'!$C$89:$C$90, 0))), 1, INDEX('Baseline Weights'!$E$89:$E$90, MATCH(AC3, 'Baseline Weights'!$C$89:$C$90, 0)))+1)*(INDEX('Baseline Weights'!$E$93:$E$94, MATCH(AD3, 'Baseline Weights'!$C$93:$C$94, 0))+1)*(INDEX('Baseline Weights'!$E$91:$E$92, MATCH(AE3, 'Baseline Weights'!$C$91:$C$92, 0))+1)*(INDEX('Baseline Weights'!$E$95:$E$96, MATCH(AF3, 'Baseline Weights'!$C$95:$C$96, 0))+1)*SQRT(AI3)*'Baseline Weights'!$D$3*IF(ISNA(VLOOKUP(C3, 'Baseline Weights'!C$20:E$27, 3, FALSE)), IF(ISNA(VLOOKUP(C3, 'Baseline Weights'!C146:E162, 3, FALSE)), 1, VLOOKUP(C3, 'Baseline Weights'!C146:E162, 3, FALSE)+1), VLOOKUP(C3, 'Baseline Weights'!C$20:E$27, 3, FALSE)+1)
 
Upvote 0
This is quite possible to do using the code that I have written as above.
However it would make the code much much simpler if the names in the headers in the Data2 sheet were the same as the categories in column A of sheet data. as an example the header in column A of sheet data2 is "ward" however the group of rows the match is done from is rows 97 to 144 which have "Location (Ward)" in column A of the Baseline weights sheet.
what I am thinking is loading into one dictionary all the values from the baseline sheets with the index being the concatenation of columns A and B for the baseline weights, then to lookup any value we just lookup in the dictionary with the input being the concatenation of the header and row value from sheet data2. If it is not possible to do this, then we can put in and extra lookup table to convert the header to the column A value. Using multiple dictionaries is probably going to make the code very slow.
Ideally it would be much easier and slightly faster if the "merged" cells in column A were not merged but the category was repeated on each row, Is this possible??
 
Upvote 0
Hi @offthelip ,

Thank you for your suggestion. I can definitely change the names of the headers (i.e. "Location (Ward)" instead of "ward"). For the rest of your suggestions, unfortunately I don't think my skills in VBA (first time using it) would allow me to do it successfully. I am currently doing it my steps, by concatenating the column name variable with the different options in sheet "Baseline Weights" (column B), and use index-match to display the weights in column F in column AP of sheet "Data". The either would be to loop over each column to generate weights for each characteristics, then have a final column as a product of all previous columns (and then, later attempt to find a way to combine all computations into a single column). However, I am already struggling to complete the first step (see code below). Any help would be greatly appreciated.

VBA Code:
Sub indexandmatchexample()

Dim weightsWs As Worksheet, dataWs As Worksheet
Dim weightLastRow As Long, dataLastRow As Long, x As Long
Dim IndexRng As Range, MatchRng As Range
Dim StringOne As String

Set weightsWs = ThisWorkbook.Worksheets("Baseline Weights")
Set dataWs = ThisWorkbook.Worksheets("Data")

weightLastRow = weightsWs.Range("B" & Rows.Count).End(xlUp).Row
dataLastRow = dataWs.Range("A" & Rows.Count).End(xlUp).Row

Set IndexRng = weightsWs.Range("B2:B" & dataLastRow)
Set MatchRng = weightsWs.Range("F2:F" & dataLastRow)

For x = 2 To weightLastRow
    
    On Error Resume Next
    
    StringOne = Range("A1").Value
    
    dataWs.Range("AP" & x).Value = Application.WorksheetFunction.Index( _
        IndexRng, _
        Application.WorksheetFunction.Match(dataWs.Range("A" & x).Value, StringOne & MatchRng, 0))

Next x

End Sub


* Baseline Weights sheet
Test.xlsm
BCDEF
2FeaturesResultBaseline WeightTest Weight
3Coefficient231,859.13231,859.13
4Square Root of Surface Area (in sq ft)
5original_useDomesticOriginal UseDomestic+ 0 %+ 0 %
6original_useBankBank+ 106 %+ 106 %
7original_useCar DealershipCar Dealership+ 24 %+ 24 %
8original_useFilling StationFilling Station+ 53 %+ 53 %
9original_useGuesthouseGuesthouse+ 4 %+ 4 %
10original_useHotelHotel+ 70 %+ 70 %
11original_useIndustrial ManufacturingIndustrial Manufacturing- 37 %- 37 %
12original_useIndustrial WarehouseIndustrial Warehouse- 47 %- 47 %
13original_useMixed Retail / OfficeMixed Retail / Office+ 13 %+ 13 %
14original_useMotor GarageMotor Garage- 30 %- 30 %
15original_useOfficeOffice+ 37 %+ 37 %
16original_usePrivate ClinicPrivate Clinic+ 72 %+ 72 %
17original_usePrivate SchoolPrivate School+ 44 %+ 44 %
18original_useRetailRetail+ 3 %+ 3 %
19original_useSupermarketSupermarket+ 7 %+ 7 %
20institutional_typeGovernment OfficesInstitutional Property TypeGovernment Offices+ 79 %+ 79 %
21institutional_typeParliament BuildingsParliament Buildings+ 79 %+ 79 %
22institutional_typePolice BuildingsPolice Buildings+ 70 %+ 70 %
23institutional_typePolice CompoundsPolice Compounds+ 70 %+ 70 %
24institutional_typeCourt BuildingsCourt Buildings+ 79 %+ 79 %
25institutional_typeMunicipal OfficesMunicipal Offices+ 79 %+ 79 %
26institutional_typeArmy barracks and installationsArmy barracks and installations+ 43 %+ 43 %
27institutional_typeFire StationFire Station+ 43 %+ 43 %
28domestic_useYesDomestic Ground UseYes+ 0 %+ 0 %
29domestic_useNoNo+ 5 %+ 5 %
30road_statusBadStreet QualityBad- 10 %- 10 %
31road_statusAverageAverage+ 0 %+ 0 %
32road_statusGoodGood+ 4 %+ 4 %
33street_lightingYesStreet LightingYes+ 0 %+ 0 %
34street_lightingNoNo+ 0 %+ 0 %
35number_lanesNoneNumber of LanesNone- 15 %- 15 %
36number_lanesOneOne+ 0 %+ 0 %
37number_lanesTwoTwo+ 2 %+ 2 %
38number_lanesFourFour+ 4 %+ 4 %
39street_accessEasyStreet AccessEasy+ 0 %+ 0 %
40street_accessDifficultDifficult- 7 %- 7 %
41has_drainageYesDrainageYes+ 2 %+ 2 %
42has_drainageNoNo+ 0 %+ 0 %
43beachYesFeaturesBeach+ 24 %+ 24 %
44hazardYesEnvironmental Hazard- 15 %- 15 %
45road_visibYesMain Road with High Visibility+ 18 %+ 18 %
46informal_settlementYesInformal Settlement- 21 %- 21 %
47commercial_corridorYesCommercial Corridor+ 25 %+ 25 %
48high_valued_areaYesHigh-valued Area+ 0 %+ 0 %
49has_potential_to_buildYesPotential to BuildYes+ 3 %+ 3 %
50has_potential_to_buildNoNo+ 0 %+ 0 %
51has_waterYesWaterYes+ 2 %+ 2 %
52has_waterNoNo+ 0 %+ 0 %
53grade_of_designTraditionalGrade of DesignTraditional+ 0 %+ 0 %
54grade_of_designModern/ArchitecturalModern/Architectural+ 0 %+ 0 %
55wall_categoryLow-value materialWall MaterialLow-value material- 49 %- 49 %
56wall_categoryMedium-value materialMedium-value material- 28 %- 28 %
57wall_categoryHigh-value materialHigh-value material+ 0 %+ 0 %
58state_of_repairBadWall QualityBad- 11 %- 11 %
59state_of_repairAverageAverage+ 0 %+ 0 %
60state_of_repairGoodGood+ 25 %+ 25 %
61wall_finishNo finishWall FinishNo finish+ 0 %+ 0 %
62wall_finishPainted or WhitewashedPainted or Whitewashed+ 0 %+ 0 %
63wall_finishAluco Bond MetalAluco Bond Metal+ 0 %+ 0 %
64wall_finishTileTile+ 0 %+ 0 %
65wall_finishGlazed Curtain WallGlazed Curtain Wall+ 0 %+ 0 %
66wall_finishStoneStone+ 0 %+ 0 %
67fence_categoryNo fenceFence MaterialNo fence+ 0 %+ 0 %
68fence_categoryLow-value materialLow-value material+ 0 %+ 0 %
69fence_categoryMedium-value materialMedium-value material+ 0 %+ 0 %
70fence_categoryHigh-value materialHigh-value material+ 0 %+ 0 %
71fence_conditionNo fenceFence ConditionNo fence+ 0 %+ 0 %
72fence_conditionBadBad+ 0 %+ 0 %
73fence_conditionAverageAverage+ 0 %+ 0 %
74fence_conditionGoodGood+ 0 %+ 0 %
75roof_categoryNot VisibleRoof MaterialNot Visible+ 0 %+ 0 %
76roof_categoryLow-value materialLow-value material- 21 %- 21 %
77roof_categoryHigh-value materialHigh-value material+ 0 %+ 0 %
78roof_repairNot VisibleRoof ConditionNot Visible+ 0 %+ 0 %
79roof_repairBadBad- 38 %- 38 %
80roof_repairAverageAverage+ 0 %+ 0 %
81roof_repairGoodGood+ 20 %+ 20 %
82windows_categoryLow-value materialWindowsLow-value material- 46 %- 46 %
83windows_categoryMedium-value materialMedium-value material+ 0 %+ 0 %
84windows_categoryHigh-value materialHigh-value material+ 15 %+ 15 %
85airconditioningYesAir ConditionYes+ 54 %+ 54 %
86airconditioningNoNo+ 0 %+ 0 %
87securityYesSecurityYes+ 43 %+ 43 %
88securityNoNo+ 0 %+ 0 %
89owns_garageYesGarageYes+ 0 %+ 0 %
90owns_garageNoNo+ 0 %+ 0 %
91owns_outbuildingYesOutbuildingYes+ 10 %+ 10 %
92owns_outbuildingNoNo+ 0 %+ 0 %
93poolYesPoolYes+ 101 %+ 101 %
94poolNoNo+ 0 %+ 0 %
95verandaYesVerandaYes+ 3 %+ 3 %
96verandaNoNo+ 0 %+ 0 %
97ward399Location (Ward)399+ 0 %+ 0 %
98ward400400+ 10 %+ 10 %
99ward401401- 3 %- 3 %
100ward402402+ 27 %+ 27 %
101ward403403+ 3 %+ 3 %
102ward404404- 16 %- 16 %
103ward405405- 6 %- 6 %
104ward406406- 5 %- 5 %
105ward407407+ 7 %+ 7 %
106ward408408+ 10 %+ 10 %
107ward409409- 9 %- 9 %
108ward410410- 14 %- 14 %
109ward411411- 35 %- 35 %
110ward412412+ 22 %+ 22 %
111ward413413+ 11 %+ 11 %
112ward414414+ 52 %+ 52 %
113ward415415+ 45 %+ 45 %
114ward416416+ 15 %+ 15 %
115ward417417+ 56 %+ 56 %
116ward418418+ 0 %+ 0 %
117ward419419+ 5 %+ 5 %
118ward420420+ 68 %+ 68 %
119ward421421+ 106 %+ 106 %
120ward422422+ 60 %+ 60 %
121ward423423+ 36 %+ 36 %
122ward424424+ 38 %+ 38 %
123ward425425+ 78 %+ 78 %
124ward426426+ 18 %+ 18 %
125ward427427+ 240 %+ 240 %
126ward428428+ 189 %+ 189 %
127ward429429+ 192 %+ 192 %
128ward430430+ 231 %+ 231 %
129ward431431+ 233 %+ 233 %
130ward432432+ 139 %+ 139 %
131ward433433+ 176 %+ 176 %
132ward434434+ 26 %+ 26 %
133ward435435+ 71 %+ 71 %
134ward436436+ 237 %+ 237 %
135ward437437+ 178 %+ 178 %
136ward438438+ 196 %+ 196 %
137ward439439+ 62 %+ 62 %
138ward440440+ 120 %+ 120 %
139ward441441+ 123 %+ 123 %
140ward442442+ 126 %+ 126 %
141ward443443+ 129 %+ 129 %
142ward444444+ 98 %+ 98 %
143ward445445+ 41 %+ 41 %
144ward446446+ 180 %+ 180 %
145original_useBollore OfficeSpecial PropertiesBollore Office+ 3819 %+ 3819 %
146original_useBollore WarehouseBollore Warehouse+ 1426 %+ 1426 %
147original_useBollore GarageBollore Garage+ 1894 %+ 1894 %
148original_useDangote OfficeDangote Office+ 346 %+ 346 %
149original_useDangote WarehouseDangote Warehouse+ 73 %+ 73 %
150original_useSLPA WarehouseSLPA Warehouse+ 119 %+ 119 %
151original_useSLPA OfficeSLPA Office+ 461 %+ 461 %
152original_useSLPA House FormSLPA House Form+ 310 %+ 310 %
153original_useSLPA IndustrialSLPA Industrial+ 158 %+ 158 %
154original_useSLPA Motor GarageSLPA Motor Garage+ 186 %+ 186 %
155original_useSLPA CanteenSLPA Canteen+ 322 %+ 322 %
156original_useNectar House FormNectar House Form+ 174 %+ 174 %
157original_useNectar WarehouseNectar Warehouse+ 46 %+ 46 %
158original_useNectar CargoNectar Cargo+ 182 %+ 182 %
159original_useNectar OfficeNectar Office+ 275 %+ 275 %
160original_useLeocem Industrial ManufacturingLeocem Industrial Manufacturing- 37 %- 37 %
161original_useLeocem OfficeLeocem Office+ 37 %+ 37 %
Baseline Weights
Cell Formulas
RangeFormula
B97:B144B97="ward"&D97
F5:F161F5=E5


* Data sheet

Test.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAP
1wardzone_cleanmarker2surface_areaoriginal_usedomestic_useroad_statusstreet_lightingnumber_lanesstreet_accesshas_drainagebeachhazardroad_visibinformal_settlementcommercial_corridorhigh_valued_areahas_potential_to_buildhas_watergrade_of_designwall_categorystate_of_repairwall_finishfence_categoryfence_conditionroof_categoryroof_repairwindows_categoryairconditioningsecurityowns_garagepoolowns_outbuildingveranda annual_rent ln_annual_rent model2_exp test_exp model2_error test_exp test_error
2399116865.98House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesYesModern/ArchitecturalHigh-value materialGoodPainted or WhitewashedNo fenceNo fenceLow-value materialGoodHigh-value materialYesNoYesNoNoYes3.3E+0717.29683.2E+0739,267,161.391.2239,267,1621.21
3399121628.53House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAverageNo finishNo fenceNo fenceLow-value materialAverageMedium-value materialNoNoNoNoNoYes450000015.319668384887,057,747.791.667,057,7481.57
4399131764.73House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAverageNo finishNo fenceNo fenceLow-value materialAverageMedium-value materialNoNoNoNoYesYes325000014.994268714308,081,650.522.258,081,6512.49
5399141702.31House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAveragePainted or WhitewashedHigh-value materialAverageLow-value materialGoodHigh-value materialNoNoNoNoNoYes725000015.79651E+079,957,874.951.399,957,8751.37
639915380.42House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalLow-value materialAverageNo finishNo fenceNo fenceLow-value materialAverageLow-value materialNoNoNoNoNoNo40000012.8992877426912,066.782.36912,0672.28
7399162709.53House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAveragePainted or WhitewashedNo fenceNo fenceLow-value materialGoodHigh-value materialNoNoNoNoYesYes825000015.92571.2E+0713,819,330.541.6913,819,3311.68
8399171821.36House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAveragePainted or WhitewashedNo fenceNo fenceLow-value materialAverageMedium-value materialNoNoNoNoNoYes737500015.813672320297,463,905.711.027,463,9061.01
9399182967.13House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesYesTraditionalHigh-value materialGoodPainted or WhitewashedNo fenceNo fenceLow-value materialGoodHigh-value materialNoNoNoNoNoYes1.1E+0716.16691.7E+0716,761,998.551.3716,761,9991.60
10399191354.39House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAveragePainted or WhitewashedNo fenceNo fenceLow-value materialAverageMedium-value materialNoNoNoNoNoYes812500015.910562364036,436,356.940.806,436,3570.79
Data
Cell Formulas
RangeFormula
AN2:AN10AN2=PRODUCT((INDEX('Baseline Weights'!$F$97:$F$144,MATCH(A2,'Baseline Weights'!$D$97:$D$144,0))+1), (INDEX('Baseline Weights'!$F$28:$F$29,MATCH(F2,'Baseline Weights'!$D$28:$D$29,0))+1), (INDEX('Baseline Weights'!$F$30:$F$32,MATCH(G2,'Baseline Weights'!$D$30:$D$32,0))+1), (IF(ISNA(INDEX('Baseline Weights'!$F$33:$F$34,MATCH(H2,'Baseline Weights'!$D$33:$D$34,0))),1,INDEX('Baseline Weights'!$F$33:$F$34,MATCH(H2,'Baseline Weights'!$D$33:$D$34,0)))+1), (INDEX('Baseline Weights'!$F$35:$F$38,MATCH(I2,'Baseline Weights'!$D$35:$D$38,0))+1), (INDEX('Baseline Weights'!$F$39:$F$40,MATCH(J2,'Baseline Weights'!$D$39:$D$40,0))+1), (INDEX('Baseline Weights'!$F$41:$F$42,MATCH(K2,'Baseline Weights'!$D$41:$D$42,0))+1), IF(L2="Yes",'Baseline Weights'!$E$43+1,1), IF(M2="Yes",'Baseline Weights'!$E$44+1,1), IF(N2="Yes",'Baseline Weights'!$E$45+1,1), IF(O2="Yes",'Baseline Weights'!$E$46+1,1), IF(P2="Yes",'Baseline Weights'!$E$47+1,1), IF(Q2="Yes",'Baseline Weights'!$E$48+1,1), (INDEX('Baseline Weights'!$F$49:$F$50, MATCH(R2, 'Baseline Weights'!$D$49:$D$50, 0))+1), (INDEX('Baseline Weights'!$F$51:$F$52, MATCH(S2, 'Baseline Weights'!$D$51:$D$52, 0))+1), IF(ISNA(INDEX('Baseline Weights'!$F$53:$F$54, MATCH(T2, 'Baseline Weights'!$D$53:$D$54, 0))), 1, INDEX('Baseline Weights'!$F$53:$F$54, MATCH(T2, 'Baseline Weights'!$D$53:$D$54, 0))+1), (INDEX('Baseline Weights'!$F$55:$F$57, MATCH(U2, 'Baseline Weights'!$D$55:$D$57, 0))+1), (INDEX('Baseline Weights'!$F$58:$F$60, MATCH(V2, 'Baseline Weights'!$D$58:$D$60, 0))+1), (INDEX('Baseline Weights'!$F$61:$F$66, MATCH(W2, 'Baseline Weights'!$D$61:$D$66, 0))+1), (INDEX('Baseline Weights'!$F$67:$F$70, MATCH(X2, 'Baseline Weights'!$D$67:$D$70, 0))+1), (INDEX('Baseline Weights'!$F$71:$F$74, MATCH(Y2, 'Baseline Weights'!$D$71:$D$74, 0))+1), (INDEX('Baseline Weights'!$F$71:$F$74, MATCH(Y2, 'Baseline Weights'!$D$71:$D$74, 0))+1), (INDEX('Baseline Weights'!$F$75:$F$77, MATCH(Z2, 'Baseline Weights'!$D$75:$D$77, 0))+1), (INDEX('Baseline Weights'!$F$78:$F$81, MATCH(AA2, 'Baseline Weights'!$D$78:$D$81, 0))+1), (INDEX('Baseline Weights'!$F$82:$F$84, MATCH(AB2, 'Baseline Weights'!$D$82:$D$84, 0))+1), (INDEX('Baseline Weights'!$F$85:$F$86, MATCH(AC2, 'Baseline Weights'!$D$85:$D$86, 0))+1), (INDEX('Baseline Weights'!$F$87:$F$88, MATCH(AD2, 'Baseline Weights'!$D$87:$D$88, 0))+1), (IF(ISNA(INDEX('Baseline Weights'!$F$89:$F$90, MATCH(AE2, 'Baseline Weights'!$D$89:$D$90, 0))), 1, INDEX('Baseline Weights'!$F$89:$F$90, MATCH(AE2, 'Baseline Weights'!$D$89:$D$90, 0)))+1), (INDEX('Baseline Weights'!$F$93:$F$94, MATCH(AF2, 'Baseline Weights'!$D$93:$D$94, 0))+1), (INDEX('Baseline Weights'!$F$91:$F$92, MATCH(AG2, 'Baseline Weights'!$D$91:$D$92, 0))+1), (INDEX('Baseline Weights'!$F$95:$F$96, MATCH(AH2, 'Baseline Weights'!$D$95:$D$96, 0))+1), SQRT(D2), 'Baseline Weights'!$E$3)
AO2:AO10AO2=AN2/AI2
 
Upvote 0
I have written some code that I hope will solve your problem. This might not be the final solution but just a step on the way. This code basically does all the lookups for you and it loadds the values into an array (datout) which I am writing out to a workhseet called "test" in order to check that the lookup are working correctly. I have run it on the dat ayou have posted and it seems to works. Soem of the items come back "notFound" . I checked the values for "BeachNo" and that is correct the isn't weight given for BeachNo. Obviously these can be change from "not found" to zero very easily.
What i am thinking is the time consuming part of your update is undoubtedly the index match stuff. this basically does than so you could buidl the equations up from the values written to the test sheet,
(multiplies and summation is very fast) See what you think:
VBA Code:
Sub test()
 Dim Ary As Variant
   Dim i As Long
   Dim Dic As Object
   
   Set Dic = CreateObject("Scripting.dictionary")
   Dim outr()
barray = Array("A2", "F2", "G2", "H2", "I2", "J2", "K2", "L2", "M2", "N2", "O2", "P2", "Q2", "S2", "T2", "U2", "V2", "W2", "X2", "Y2", "Z2", "AA2", "AB2", "AC2", "AD2", "AE2", "AF2", "AG2", "AH2", "AI2")
' first convert the column letters in barray to column numbers
ReDim outr(1 To 10, 1 To UBound(barray))
For i = 1 To UBound(barray)
 tt = Len(barray(i))
  If tt = 2 Then
   outr(2, i) = Asc(Left(barray(i), 1)) - 64 ' column Number
   outr(1, i) = barray(i)                    ' Column Letter
  Else
     outr(2, i) = Asc(Mid(barray(i), 2, 1)) - 64 + 26
     outr(1, i) = barray(i)
  End If
Next i
''''''''''''''''''''''''''''
   With Sheets("Baseline_Weights")
      Ary = .Range("b5:E161")   ' load all the bae line weights into a variant array
   For i = 1 To UBound(Ary)
      Dic(Ary(i, 1)) = Ary(i, 4)  ' add thenm all to the dictionary
   Next i
   End With
''''''''''''''''''''''''''''''
   With Sheets("Data")
      datr = .Range("A1:Am10")  ' load all the data into a variant array
   End With
   With Sheets("Test")
      .Range("A1:Am10") = ""
      datout = .Range("A1:Am10")  ' load all the data into a variant array
    
    For j = 1 To UBound(barray) ' loop through all the columns
        head = datr(1, outr(2, j)) ' pick up header from row 1
        datout(1, outr(2, j)) = head
      For k = 2 To UBound(datr, 1) ' loop through all the rows
        ditem = head & datr(k, outr(2, j))
        If Dic.Exists(ditem) Then
         datout(k, outr(2, j)) = Dic(ditem)
        Else
        datout(k, outr(2, j)) = "Not Found"
        End If
      Next k
    Next j
      .Range("A1:Am10") = datout
      
   End With
'Range(Cells(1, 1), Cells(10, UBound(barray))) = outr
End Sub
Note this code will be very fast!!!
 
Upvote 0
Hi @offthelip ,

Thank you so much, this looks great. After slightly tweeking it, I was nearly able to produce the desired result. The only thing that is missing is to export the weights for column A (ward) in the "Test" sheet. I tried to go through the code but can't figure out why it's not exporting as the column has been added to the dictionary ("A2").

Thank you again for all of your help
 
Upvote 0
Hi @offthelip ,

I was able to produce the following table (couldn't find an elegant way to multiply all columns in one cell). The computation with 1'226 rows takes about 50 seconds, unsure if there is anything else I could do to speed up the task as I would like to run the similar code on 100'000+ rows. Also, I've noticed that the quotient formula is not yielding the correct result but don't understand why this is the case.

Thank you again!

VBA Code:
Sub test()
 Dim Ary As Variant
   Dim i As Long
   Dim Dic As Object
  
   Set Dic = CreateObject("Scripting.dictionary")
   Dim outr()
barray = Array("A2", "F2", "G2", "H2", "I2", "J2", "K2", "L2", "M2", "N2", "O2", "P2", "Q2", "R2", "S2", "T2", "U2", "V2", "W2", "X2", "Y2", "Z2", "AA2", "AB2", "AC2", "AD2", "AE2", "AF2", "AG2", "AH2")
' first convert the column letters in barray to column numbers
ReDim outr(1 To 1226, 1 To UBound(barray))
For i = 1 To UBound(barray)
 tt = Len(barray(i))
  If tt = 2 Then
   outr(2, i) = Asc(Left(barray(i), 1)) - 64 ' column Number
   outr(1, i) = barray(i)                    ' Column Letter
  Else
     outr(2, i) = Asc(Mid(barray(i), 2, 1)) - 64 + 26
     outr(1, i) = barray(i)
  End If
Next i
''''''''''''''''''''''''''''
   With Sheets("Baseline Weights")
      Ary = .Range("b5:F167")   ' load all the bae line weights into a variant array
   For i = 1 To UBound(Ary)
      Dic(Ary(i, 1)) = Ary(i, 5)  ' add them all to the dictionary
   Next i
   End With
''''''''''''''''''''''''''''''
   With Sheets("Data")
      datr = .Range("A1:Am1226")  ' load all the data into a variant array
   End With
   With Sheets("Data")
      .Range("Aq1:bx1226") = ""
      datout = .Range("Aq1:bx1226")  ' load all the data into a variant array
    
    For j = 1 To UBound(barray) ' loop through all the columns
        head = datr(1, outr(2, j)) ' pick up header from row 1
        datout(1, outr(2, j)) = head
      For k = 2 To UBound(datr, 1) ' loop through all the rows
        ditem = head & datr(k, outr(2, j))
        If Dic.Exists(ditem) Then
         datout(k, outr(2, j)) = Dic(ditem) + 1
        Else
        datout(k, outr(2, j)) = "Not Found"
        End If
      Next k
    Next j
      .Range("Aq1:bx1226") = datout
      

    For r = 2 To 1226
        Cells(r, 79).Value = Application.WorksheetFunction.Product(Cells(r, 48).Value, Cells(r, 49).Value, Cells(r, 50).Value, Cells(r, 51).Value, Cells(r, 52).Value, Cells(r, 53).Value, Cells(r, 54).Value, Cells(r, 55).Value, Cells(r, 56).Value, Cells(r, 57).Value, Cells(r, 58).Value, Cells(r, 59).Value, Cells(r, 60).Value, Cells(r, 61).Value, Cells(r, 62).Value, Cells(r, 63).Value, Cells(r, 64).Value, Cells(r, 65).Value, Cells(r, 66).Value, Cells(r, 67).Value, Cells(r, 68).Value, Cells(r, 69).Value, Cells(r, 70).Value, Cells(r, 71).Value, Cells(r, 72).Value, Cells(r, 73).Value, Cells(r, 74).Value, Cells(r, 75).Value, Cells(r, 76).Value)
    Next r
    
    For s = 2 To 1226
        Cells(s, 40).Value = Application.WorksheetFunction.Product(231859.13, Cells(s, 78).Value, Cells(s, 79).Value, Cells(s, 80).Value)
    Next s
    
    For t = 2 To 1226
        Cells(t, 41).Value = WorksheetFunction.Quotient(Cells(t, 40).Value, Cells(t, 35).Value)
    Next t

    
   End With
'Range(Cells(1, 1), Cells(10, UBound(barray))) = outr
End Sub

Test.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBPBQBRBSBTBUBVBWBXBYBZCACB
1wardzone_cleanmarker2surface_areaoriginal_usedomestic_useroad_statusstreet_lightingnumber_lanesstreet_accesshas_drainagebeachhazardroad_visibinformal_settlementcommercial_corridorhigh_valued_areahas_potential_to_buildhas_watergrade_of_designwall_categorystate_of_repairwall_finishfence_categoryfence_conditionroof_categoryroof_repairwindows_categoryairconditioningsecurityowns_garagepoolowns_outbuildingveranda annual_rent ln_annual_rent model2_exp original_exp model2_error test_exp test_errordomestic_useroad_statusstreet_lightingnumber_lanesstreet_accesshas_drainagebeachhazardroad_visibinformal_settlementcommercial_corridorhigh_valued_areahas_potential_to_buildhas_watergrade_of_designwall_categorystate_of_repairwall_finishfence_categoryfence_conditionroof_categoryroof_repairwindows_categoryairconditioningsecurityowns_garagepoolowns_outbuildingverandaWard weightsquare root of surface
2399116866House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesYesModern/ArchitecturalHigh-value materialGoodPainted or WhitewashedNo fenceNo fenceLow-value materialGoodHigh-value materialYesNoYesNoNoYes3250000017.2973.2E+0739,267,161.391.2239,267,161.671.0010.911111111111.031.02111.251110.791.21.151.5411111.03231859.11.002.0438782.86120914
3399121628.5House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAverageNo finishNo fenceNo fenceLow-value materialAverageMedium-value materialNoNoNoNoNoYes450000015.3268384887,057,747.791.667,057,747.841.0010.911111111111.0311111110.7911111111.03231859.11.000.754340.35504961
4399131764.7House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAverageNo finishNo fenceNo fenceLow-value materialAverageMedium-value materialNoNoNoNoYesYes325000014.99468714308,081,650.522.258,081,650.582.0010.911111111111.0311111110.791111111.11.03231859.11.000.8297342.00868934
5399141702.3House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalHigh-value materialAveragePainted or WhitewashedHigh-value materialAverageLow-value materialGoodHigh-value materialNoNoNoNoNoYes725000015.7971E+079,957,874.951.399,957,875.021.0010.911111111111.0311111110.791.21.15111111.03231859.11.001.0409341.25906032
639915380.42House or dwelling formYesBadNoOneEasyNoNoNoNoNoNoNoYesNoTraditionalLow-value materialAverageNo finishNo fenceNo fenceLow-value materialAverageLow-value materialNoNoNoNoNoNo40000012.899877426912,066.782.36912,066.792.0010.911111111111.03110.5111110.7910.54111111231859.11.000.2016819.50435883
Data
Cell Formulas
RangeFormula
CB2:CB6CB2=SQRT(D2)
 
Upvote 0
Hi @offthelip ,

Thank you so much, this looks great. After slightly tweeking it, I was nearly able to produce the desired result. The only thing that is missing is to export the weights for column A (ward) in the "Test" sheet. I tried to go through the code but can't figure out why it's not exporting as the column has been added to the dictionary ("A2").

Thank you again for all of your help
I found the problem as to why the Ward match wasn't working I had got the indexing in to the barray wrong it should ahve started a zero not at one. I have fixed in the latest code see below
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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