Help with Indexing in VBA

shiva_reshs

Board Regular
Joined
Sep 5, 2012
Messages
68
Hello,

I am not able to figure out, what is wrong in this Indexing code.

Code:
<code>Range("R6").Select ActiveCell.FormulaR1C1 = "=IF($Q6="","",INDEX('Worker-Exempt'!B:B,MATCH($Q6,'Worker-Exempt'!A:A,0)))"</code></pre>

I get application 1004 error msg.

Cross Posting

HTML:
http://www.excelforum.com/excel-programming-vba-macros/960412-vlookup-to-index-matching-in-vba.html
 
Maybe (untested)

Rich (BB code):
With Range("N6:N" & lr1)
    .Formula = "=IF($L6="""","""",INDEX('US Exempt Worker- US Exempt Wor'!C:C,MATCH($L6,'US Exempt Worker- US Exempt Wor'!B:B,0)))"
    .Value = .Value
End With
For i = 6 To lr1
    If Range("N" & i).Text = "#N/A" Then .Value = "-"
Next i
Peter,

I clicked into this thread by accident, but it looks like you need to put something in front of the dot for the part I highlighted in red.
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Oops! Thanks Rick

Code:
With Range("N6:N" & lr1)
    .Formula = "=IF($L6="""","""",INDEX('US Exempt Worker- US Exempt Wor'!C:C,MATCH($L6,'US Exempt Worker- US Exempt Wor'!B:B,0)))"
    .Value = .Value
End With
For i = 6 To lr1
    If Range("N" & i).Text = "#N/A" Then Range("N" & i).Value = "-"
Next i
 
Upvote 0
Maybe (untested)

Rich (BB code):
With Range("N6:N" & lr1)
    .Formula = "=IF($L6="""","""",INDEX('US Exempt Worker- US Exempt Wor'!C:C,MATCH($L6,'US Exempt Worker- US Exempt Wor'!B:B,0)))"
    .Value = .Value
End With
For i = 6 To lr1
    If Range("N" & i).Text = "#N/A" Then .Value = "-"
Next i
Peter,

I clicked into this thread by accident, but it looks like you need to put something in front of the dot for the part I highlighted in red.
Actually, in looking at what you posted, and what I think is being attempted, couldn't this work?

Rich (BB code):
With Range("N6:N" & lr1)
    .Formula = "=IF($L6="""","""",INDEX('US Exempt Worker- US Exempt Wor'!C:C,MATCH($L6,'US Exempt Worker- US Exempt Wor'!B:B,0)))"
    .Value = .Value
    .SpecialCells(xlConstants, xlErrors).Value = "-"
End With
 
Upvote 0
Actually, in looking at what you posted, and what I think is being attempted, couldn't this work?

Rich (BB code):
With Range("N6:N" & lr1)
    .Formula = "=IF($L6="""","""",INDEX('US Exempt Worker- US Exempt Wor'!C:C,MATCH($L6,'US Exempt Worker- US Exempt Wor'!B:B,0)))"
    .Value = .Value
    .SpecialCells(xlConstants, xlErrors).Value = "-"
End With

Much better :)
 
Upvote 0
Nicely done.

You guys are the best!!!

*Just for my learning. If I want to color the cell can it be done?

Thanks!!
 
Upvote 0
*Just for my learning. If I want to color the cell can it be done?
Which "cell" (singular?), the one that was #N/A and is now a dash? If so, try this...

Rich (BB code):
With Range("N6:N" & lr1)
    .Formula = "=IF($L6="""","""",INDEX('US Exempt Worker- US Exempt Wor'!C:C,MATCH($L6,'US Exempt Worker- US Exempt Wor'!B:B,0)))"
    .Value = .Value
    With .SpecialCells(xlConstants, xlErrors)
        .Value = "-"
        .Interior.ColorIndex = 6    'change the ColorIndex value as desired
    End With
End With
 
Upvote 0
Hi Rick,

when no #N/A is found, i get run time error 1004 on below line.
Code:
[/COLOR][/B][B][COLOR=#b22222].SpecialCells(xlConstants, xlErrors).Value = "-"[/COLOR][/B][B][COLOR=#b22222]
 
Upvote 0
Hi Rick,

when no #N/A is found, i get run time error 1004 on below line.
Code:
[B][COLOR=#008000]On Error Resume Next[/COLOR][/B]
[B][COLOR=#b22222].SpecialCells(xlConstants, xlErrors).Value = "-"
[/COLOR][COLOR=#008000]On Error GoTo 0[/COLOR][COLOR=#b22222]
[/COLOR][/B]
Sorry, I forgot to cover that possibility. Add the lines around the SpecialCells statement that I show in green above and all will be well.
 
Upvote 0
Hi Rick,

Need your help again..

Your code helps to change cell value to "-" for every #n/a, but is it possible to highlight the cell to color which do not have #n/a ??

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,456
Messages
6,124,939
Members
449,197
Latest member
k_bs

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