Index(Match()) Within For Loop VBA Code Help

tjgiles2

New Member
Joined
Mar 11, 2014
Messages
1
Hello,

Im trying to populate cells using a an index match function within VBA, I know how to do this with in a cell but cant seem to get it to work in vba. The code segment below is what i have so far, the underlined part is the line i am haivng trouble with. Thanks for help it would be much appriciated.

Sub Merge()
Dim ws1, ws2 As Worksheet
Dim wb As Workbook
Dim a, b, PSum, StdHrs, LastRow As Integer
Set wb = ThisWorkbook
Set ws1 = wb.Sheets("DOWNLOAD2")
Set ws2 = wb.Sheets("JOHN")
a = 2
b = 2
PSum = 0
StdHrs = 0
Application.ScreenUpdating = False
LastRow = ws2.UsedRange.Rows.Count
For a = 2 To LastRow
If ws2.Cells(a, 4).Value <> ws2.Cells(a + 1, 4).Value Then

ws2.Cells(a, 22) = Application.WorksheetFunction.Index(Sheets("DOWNLOAD").Range("A:Q"), Application.WorksheetFunction.Match((ws2.Cells(a,3).Value) & (ws2.Cells(a,4).Value), (Sheets("DOWNLOAD").Range("A:A")) & (Sheets("DOWNLOAD").Range("C:C")), 0), 16)

End If
Next a
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi tjgiles2,

try recording a macro and type the function in a cell. I got something like this:
ActiveCell.FormulaR1C1 = "=INDEX(R[-5]C[-5]:R[-2]C[-2],MATCH(R[-7]C[-3],R[-8]C[2]:R[9]C[2],0),3)"

You could easily put that in place instead of the formula you're trying to build right now. If need be: put formula in place, copy down for all rows, calculate, copy whole range, paste values over whole range :). Whenever I use the Application.Worksheetfunction, I tend to start with "Set WSF = Application.WorksheetFunction" in my code, that simplifies the lines where you have to use a couple of them, like in your underlined example.

Cheers,

Koen
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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