Index()Match() macro (using an absolute location)

graybeam

New Member
Joined
Jun 1, 2010
Messages
24
My macro code looks like this:

Code:
Sub FixCodedField()
'
' FixCodedField Macro
'
'
    Range(Cells(1, ActiveCell.Column), Cells(Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row, ActiveCell.Column)).Select
    Selection.Copy
    ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
    Selection.Insert Shift:=xlToRight
    ActiveCell.Offset(1, 0).Range("A1").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = _
        "=INDEX(Sheet1!C[-12],MATCH(SBCLIENT!RC[-1],Sheet1!C[-13],0),1)"
    Range(Cells(2, ActiveCell.Column), Cells(Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row, ActiveCell.Column)).Select
    Selection.FillDown
    Selection.Copy
    ActiveCell.Offset(0, -1).Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    ActiveCell.Offset(0, -1).Range("A1").Select
End Sub

The piece I'm most troubled with is

Code:
ActiveCell.FormulaR1C1 = _
        "=INDEX(Sheet1!C[-12],MATCH(SBCLIENT!RC[-1],Sheet1!C[-13],0),1)"

It seems using Relative References is becoming an issue. I have a column of data with a code in it like "19" or "2" and there is a key that turns that code into a meaning like "2 = NET30". I'm using the INDEX and MATCH functions as a sort of vlookup to match the values in my table (SBCLIENT) to the key I keep on another worksheet (Sheet1). The macro works great, if I don't try to use it on another column.

For some reason, I think it's relative references, when I put a different key into Sheet1 and run this macro on a different column location, it ruins my reference and lookup array and I get the #N/A error.

I'd like to get a macro going that is flexable enough to use in any column that will look up a key on another sheet and use it to replace the code values with the actual values.

Does that make any sense at all??? LOL! :eek:
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I fixed it with this:

Code:
Sub FixCodedFields()
'
' FixCodedFields Macro
'
'
    Range(Cells(1, ActiveCell.Column), Cells(Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row, ActiveCell.Column)).Select
    Selection.Copy
    ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
    Selection.Insert Shift:=xlToRight
    ActiveCell.Offset(1, 0).Range("A1").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = _
        "=INDEX(Sheet1!C2,MATCH(SBCLIENT!RC[-1],Sheet1!C1,0),1)"
    Range(Cells(2, ActiveCell.Column), Cells(Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row, ActiveCell.Column)).Select
    Selection.FillDown
    Selection.Copy
    ActiveCell.Offset(0, -1).Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    ActiveCell.Offset(0, -1).Columns("A:A").EntireColumn.Select
    Selection.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    ActiveCell.Select
End Sub
 
Upvote 0
Try using Named Range's. Even if it is only for 1 cell it is still useful. Means that if you insert a column or row in between your data points then it won't flag up as an error.

Cheers,
Harry
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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