Forum Discussion
Macro for an array formula
I have a product sales spreadsheet which is generated monthly. I also have a rebate report which is also generated monthly. I inserted an array formula into a new column (column E) in the sales spreadsheet which checks each row of data find a match of the customer name, catalog # and order number. If it find a match, it will insert the customer name into the row in Column E.
The formula in COL E in the products sales report is is as follows: =INDEX('Rebate Report'!$A$4:$L$396,MATCH(1,('Rebate Report'!A:A=B2)*('Rebate Report'!B:B=C2)*('Rebate Report'!C:C=D2),0),1)
I want to create a macro so that a user can click the command button and it will insert the row E into the product sales spreadsheet and calculate the above array formula into each row as shown below each month. The rebate report and the products sales reports have a different number of rows each month. I can make the range in the formula(i.e. currently it is 'Rebate Report'!$A$4:$L$396 in the formula) for the rebate report way bigger than will ever be needed e.g. 5000 rows if that makes things simpler than making it dynamic.
Thanks
David
The data looks like this:
Rebate Report
COL A Customer Name............. | COL B Catalog Number.......................... | COL C Invoice#....... | Contract No.......... | ShipDate. | UPC | Qty Shpd.. |  Actual Cost.. |  Rebate Cost |  Dif Amt...... |  Rebate Ext... |
DEF CO | 10250Q1643N | S1206376.013 | 003473 | 1/18/2017 | 7821134567 | -8 | Â Â Â Â Â Â Â 67.60 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 30.63 | Â Â Â Â Â Â Â 36.97 | Â Â Â Â Â (295.76) |
Product Sales Report
COL A Customer ID | COL B | COL C Catalog # | COL D Order Number | COL E On rebate report | COL F Price Line | Product ID | Description | Shipping Branch | Ship Qty |
1110 | ABC CO | 10250T1 | S1210754.001 | ABC CO | XXX | 8286 | XYZ | 1 | 50 |
1661 | DEF CO | 10250T1 | S1210215.001 | #NA | XXX | 8286 | XYZ | 1 | 2
|
2 Replies
- Matt MickleBronze Contributor
Try using something like this. Not sure how you plan on inserting rows....but this will get you the array formula via VBA. You'll need to define the last row of your data so the formula extends the correct length:
Sub Test()
'Define Lrow based on data in column A
Lrow = Worksheets("Rebate Report").Cells(Rows.Count, "A").End(xlUp).Row
'Enter array formula based on lastrow
Range("E2:E" & Lrow).FormulaArray = "=INDEX('Rebate Report'!$A$4:$L$" & Lrow & ",MATCH(1,('Rebate Report'!A:A=B2)" _
& "*('Rebate Report'!B:B=C2)*('Rebate Report'!C:C=D2),0),1)"
End Sub- DavidCopper Contributor
Thanks!