VBA: Using Evaluate with match/address to expand a formula works but also returns a type mismatch error

sheetspread

Well-known Member
Joined
Sep 19, 2005
Messages
5,160

Excel 2003
ABCDEFGHIJKLMNO
1NameTypeGroupJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember
2Rev11Yellow7,4162,5109,8207447,6217,9105,9565,0269,7928,8009,0595,761
3Rev21Yellow6,3148,2151304517,2631,6528,2583,4895,2455,2812,7281,193
4Rev31Green9,0058,4491,4683,9183,3874,6358,2535,9286,8413,3105,2318,729
5Rev41Blue3,9781,8797,2514,0557,7163414,7561,2444,4751,8814,0516,508
6Rev51Green6694317,4075,5081,2127,2523,7665,2862,0623,3005,7714,020
7Rev61Orange1,0475,5859,7309,1879,0234,3541,0832,6008,6988,6206,3132,180
8Rev72Yellow8,0959,8304,2633,6326,3539,8446,7141,9013645,9399,024427
9Rev82Green4,0386,1062596228,1653,8734,0903,5954,2042,1208,0332,425
10Rev92White9,7912,0577,4977,4638,9706,1418294,6539,5605,0061,8903,541
11Rev102White1,2876,5864,5319,4815205,4113,2905,1087,2436,9237,14951
12Rev112Orange7,8132545,1918,2673,5597786,4293,6135,1898,0183,9404,243
13Rev123Yellow2,8993,9088,6931,5211,1041,1399,5235,5673,4051,0442908,226
14Rev133Green9,0221,3771008,1591,3602,9181,6948,5831358,177219,980
15Rev143Blue9,0349,0922,3491,7337,1584582,1139,6304,7605,7393,3599,190
16Rev153Blue3,4077,3866838,1295,3479,7037,3322927,6333,5253,4009,825
17Rev163Yellow5,6977577,3274,1581,2021,8649249,0241,9958,2661,1825,144
18Rev174Green9,4187,0873,7578877857,0634,4711,2347,6414,7072,5405,771
19Rev184Blue3,1019886,2401,8905,6291,5186,4841,9326,0457933,0294,057
20Rev194Green1,2737,3237,0999,7977,8663,1061,9204,9542681,7153,614292
21Rev204Orange1,8622,4849,0566,1691,0514,8106438,9581,3595,9934,8612,714
22Rev214Yellow5,9342,7609,9875872562,9513,8429,4632,9014,7038,7121,868
23Rev224Green5,3929,5324,4677,7863,7384,3514,3858,1718,0259925,993153
24Rev235Blue4,5492,5025,1227,0127,1789,0556,2075261,8521,0448,6906,102
25Rev246Yellow6281,8406,3021,4216,3579,4306,1579,1804,7036,1899,3834,097
26Rev256Blue7,6275384,3265,3691,3866,7864,5911732,6048,3362,9094,727
27Cost11Blue7,7497,8465,3649,3226,0534,9889,8885,5221,4998,5159,2612,581
28Cost21Orange9,8403,7219849,6708,5271,8428,5572,5873,0136,7354,6501,876
29Cost31Yellow8,660658,1742,6917,1543,8755,4759,7228,1268,7162,4254,930
30Cost41Green4,3323,6162,8791,0645587,7042,0487,4635,3105,8005,1492,057
31Cost51White7,9903,9069,2043,4307,1652,3515,4606,0499,9877,8124,5613,306
32Cost61White6,0845,5002,9354,1581,5613,6499,7591,6692,3503,8677,0922,452
33Cost72Orange3,8999,3401,4173,7117,2725,7046,1265,7444,6821,3143,5734,697
Monthly PL


Excel 2003
ABCDEFGHIJKLMNO
34Cost82Yellow9,3694,6362565,4129,4003,3439,650217,2635,0696,0591,314
35Cost93Green5,0641,8904,3844,7761,5639393,6465,7567,3365,1933,8865,008
36Cost103Blue2483,1829,9133,0812007,9221,7594,5439,8163,0511,2664,742
37Cost113Blue2,3432865,6735,5953,9423585,9052,5164,0534,9358,5522,894
38Cost123Yellow1,1469,0745,9801,7267,4488,1302,5456,9315,0881,5653,6508,090
39Cost134Green8413,0236,5407845,0144,0252,1048,9468,4047447,8278,525
40Cost145Blue1121,4038,6069,5271,8509,1623,3626,5139,7142,8571105,212
41Cost155Green9,9666,8296,6329895,7732,3412,0488,1243,4985,2998,703976
42Cost166Orange7,8751,8935,2524,2792,2104,9603,3361,1544342,9989,6493,482
43Cost177Yellow9,2352,6709,1955,4398598,2196,5631,4587,0561,0174,5876,361
44Cost187Green1,4012,2297,7411,4196,9129,5317,1963,2152,6035,4131188,311
45Cost197Blue8,1471,7206853,9193,0014,6587915,3517,1648,3294,9367,559
46Cost207Yellow3,5513046,5176,8689,4564,9773883,2629,8985,6589,2003,535
47Cost217Blue9602,7848,2813,2705,9754,8319,6849,0613,103212,8593,664
48Cost227Green3,1565,0982,1224,1144,8069,4789,2827,2347,4365,1197,1577,395
49Cost238White3824,6683452,7208,8635,7339,7149,8699,1179,1411,8104,534
50Cost248Orange3,9583,5926,2677,0541,6478,4182,0731,5588,9947,3143,4115,120
Monthly PL



Excel 2003
ABCDEF
1NorthSouthEastWestCentral
2Orange0.360.130.5100
3Yellow0.330.270.040.320.04
4Green0.250.3100.050.39
5Blue0.150.1500.590.11
6White0.420.070.140.140.23
Groups


Code:
Sub repeatrowsbypercenttable()
Dim x As Integer
Dim y As Integer
Dim LRPL As Integer
Dim LCPL As Integer
Dim LRGroups As Integer
Dim LCGroups As Integer
Sheets("Monthly PL").Copy Before:=Sheets("Monthly PL")
ActiveSheet.Name = "Monthly PL Expanded"
LRPL = Sheets("Monthly PL Expanded").Cells(Rows.Count, 1).End(xlUp).Row
LCPL = Sheets("Monthly PL Expanded").Cells(2, Columns.Count).End(xlToLeft).Column
LRGroups = Sheets("Groups").Cells(Rows.Count, 1).End(xlUp).Row
LCGroups = Sheets("Groups").Cells(2, Columns.Count).End(xlToLeft).Column
For Each cell In Range(Cells(2, 4).Address & ":" & Cells(LRPL, LCPL).Address)
cell.Formula = "='Monthly PL'!" & cell.Address
Next cell
Range("D1").EntireColumn.Insert
Range("D1").Value = "Direction"
For x = LRPL To 2 Step -1
Cells(x, 1).EntireRow.Copy
Cells(x, 1).EntireRow.Resize(LCGroups - 2).Insert shift:=xlDown
Sheets("Groups").Range(Cells(1, 2).Address & ":" & Cells(1, LCGroups).Address).Copy
Range(Cells(x, 4).Address & ":" & Cells(x + LCGroups - 2, 4).Address).PasteSpecial Transpose:=True
Next
For Each cell In Range(Cells(2, 5).Address & ":" & Cells((LRPL * LCGroups - 1) - LCPL - 2, LCPL + 1).Address)
cell.Formula = cell.Formula & "*Groups!" & Evaluate("=address(match(C" & cell.Row & ",Groups!A1:" & Cells(LRGroups, 1).Address & ",0), match(D" & cell.Row & ",Groups!A1:" & Cells(1, LCGroups).Address & ",0))")
Next
End Sub



Excel 2003
ABCDEFGHIJKLMNOP
1NameTypeGroupDirectionJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember
2Rev11YellowNorth2,4478283,2412462,5152,6101,9651,6593,2312,9042,9891,901
3Rev11YellowSouth2,0026782,6512012,0582,1361,6081,3572,6442,3762,4461,555
4Rev11YellowEast29710039330305316238201392352362230
5Rev11YellowWest2,3738033,1422382,4392,5311,9061,6083,1332,8162,8991,844
6Rev11YellowCentral29710039330305316238201392352362230
7Rev21YellowNorth2,0842,711431492,3975452,7251,1511,7311,743900394
8Rev21YellowSouth1,7052,218351221,9614462,2309421,4161,426737322
9Rev21YellowEast2533295182916633014021021110948
10Rev21YellowWest2,0202,629421442,3245292,6431,1161,6781,690873382
11Rev21YellowCentral2533295182916633014021021110948
12Rev31GreenNorth2,2512,1123679808471,1592,0631,4821,7108281,3082,182
13Rev31GreenSouth2,7922,6194551,2151,0501,4372,5581,8382,1211,0261,6222,706
14Rev31GreenEast000000000000
15Rev31GreenWest45042273196169232413296342166262436
16Rev31GreenCentral3,5123,2955731,5281,3211,8083,2192,3122,6681,2912,0403,404
17Rev41BlueNorth5972821,0886081,15751713187671282608976
18Rev41BlueSouth5972821,0886081,15751713187671282608976
19Rev41BlueEast000000000000
20Rev41BlueWest2,3471,1094,2782,3924,5522012,8067342,6401,1102,3903,840
21Rev41BlueCentral43820779844684938523137492207446716
22Rev51GreenNorth1671081,8521,3773031,8139421,3225168251,4431,005
23Rev51GreenSouth2071342,2961,7073762,2481,1671,6396391,0231,7891,246
24Rev51GreenEast000000000000
25Rev51GreenWest332237027561363188264103165289201
26Rev51GreenCentral2611682,8892,1484732,8281,4692,0628041,2872,2511,568
27Rev61OrangeNorth3772,0113,5033,3073,2481,5673909363,1313,1032,273785
28Rev61OrangeSouth1367261,2651,1941,1735661413381,1311,121821283
29Rev61OrangeEast5342,8484,9624,6854,6022,2215521,3264,4364,3963,2201,112
30Rev61OrangeWest000000000000
31Rev61OrangeCentral000000000000
Monthly PL Expanded
Cell Formulas
RangeFormula
E2='Monthly PL'!$D$2*Groups!$B$3
E3='Monthly PL'!$D$2*Groups!$C$3
E4='Monthly PL'!$D$2*Groups!$D$3
E5='Monthly PL'!$D$2*Groups!$E$3


............etc


I get the runtime 13 error and this line highlighted yellow:

cell.Formula = cell.Formula & "*Groups!" & Evaluate("=address(match(C" & cell.Row & ",Groups!A1:" & Cells(LRGroups, 1).Address & ",0), match(D" & cell.Row & ",Groups!A1:" & Cells(1, LCGroups).Address & ",0))")

Any ideas?
 
Last edited:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I haven't studied your entire formula, but one thing that jumps out is that you'll need to replace:

Evaluate("=address...
with:
Evaluate("address...
 
Upvote 0
Both work, but thanks for looking at this. I just found the mistake (wrong column reference/multiplier too high):

For Each cell In Range(Cells(2, 5).Address & ":" & Cells((LRPL * LCGroups - 1) - LCPL - 2, LCPL + 1).Address)

should be:

For Each cell In Range(Cells(2, 5).Address & ":" & Cells((LRPL * (LCGroups - 1) - LCGroups + 2), LCPL + 1).Address)

Otherwise it exceeds the expanded range row count to where there is no data.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,840
Members
449,471
Latest member
lachbee

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