VBA - using the MATCH formula

macroos

New Member
Joined
May 30, 2018
Messages
45
Hi.

I want to use the MATCH formula here, but if more rows are added or deleted, the macros below will not work because the cells used in the formula are absolute (I think that's the word).

Note, in my table, the numbers have been sorted into negative numbers on top and positive number on bottom before doing this macros.

=MATCH(-E2,$F$6:$F$10,0) * Formula here will always be -E2 on here. $F$6:$F10 will change depending on the first positive value in column F to the last row
=MATCH(-F6,$G$2:$G$5,0) * -F6 will be the first cell that contains the positive number. It will always start with $G$2 but $G$5 has to change based on the last negative number in the column

ABCDEFGHIJKL
2data1red-5-5
3data2red-4-4
4data3red-3-3
5data4green-2-2
6data5red55
7data6green44
8data7red33
9data8red22
10data9blue11

<tbody>
</tbody>


Code:
Sub FormulaM()

    Dim formulaAll As Long, formulaPos As Long
    formulaAll = Cells(Rows.Count, "B").End(xlUp).Row
    formulaPos = Columns("E").Find("(*", , xlValues, , xlRows, xlPrevious, , , False).Row + 1


    ' Based on above table, the formula on M2 would be =MATCH([COLOR=#ff0000]-E2,$F$6:$F$10[/COLOR],0) and would be copied from M3:M5
    Range("M2").Select
    ActiveCell.FormulaR1C1 = "=MATCH(-RC[-8],R47C6:R59C6,0)"
    Range("M2").Select
    Selection.AutoFill Destination:=Range("M2:M" & formulaAll), Type:=xlFillDefault
    
     ' Based on above table, the formula on M6 would be =MATCH([COLOR=#ff0000]-F6,$G$2:$G$5[/COLOR],0) and would be copied from M7:M10
    Range("M47").Select
    ActiveCell.FormulaR1C1 = "=MATCH(-RC[-7],R2C7:R46C7,0)"
    Range("M47").Select
    Selection.AutoFill Destination:=Range("M" & formulaPos & ":M" & formulaAll), Type:=xlFillDefault

    ...

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Would you consider using the same formula for all the rows as per the code below?
Note too that you can enter all the formulas at once without selecting anything and without AutoFill.

Test in a copy of your workbook.

Code:
Sub FormulaM_v2()
    Dim formulaAll As Long, formulaPos As Long
    
    formulaAll = Cells(Rows.Count, "B").End(xlUp).Row
    formulaPos = Columns("E").Find("(*", , xlValues, , xlRows, xlPrevious, , , False).Row + 1
    
    Range("M2:M" & formulaAll).FormulaR1C1 = "=MATCH(-RC[-8],IF(RC[-8]<0,R" & formulaPos & "C[-7]:R" & formulaAll & "C[-7],R2C[-6]:R" & formulaPos - 1 & "C[-6]),0)"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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