VBA index match code fails to work on certain cell

mychi11

Board Regular
Joined
May 11, 2020
Messages
95
Office Version
  1. 2016
Platform
  1. Windows
Hi all, my vba code is as follow, it successfully index match the price but not for cell E53 onward. Could any expert please help

VBA Code:
Sub IndexMatch_Function()

Dim x As Integer, y As Integer
Dim SourceRange As Range
Dim LookRange As Range
Dim Header_Range As Range
Dim ws As Worksheet

Set wsA = Sheets("Blood_Cost")
Set wsVO = Sheets("Blood_Price")
Set SourceRange = wsA.Range("A:B")
Set LookRange = wsA.Range("A:A")
Set Header_Range = wsA.Range("1:1")

wsVO.Select

MyLastRow = wsVO.Cells(Rows.Count, 1).End(xlUp).Row
MyLastColumn = wsVO.Cells(1, Columns.Count).End(xlToLeft).Column

For x = 2 To MyLastRow
For y = 2 To MyLastColumn

On Error Resume Next

wsVO.Cells(x, y) = WorksheetFunction.Index(SourceRange, _
WorksheetFunction.Match(wsVO.Cells(x, 2), LookRange, 0), _
WorksheetFunction.Match(wsVO.Cells(1, y), Header_Range, 0))

Next y
    Next x

On Error GoTo 0

End Sub


Purchase Order and Budget Estimation.xlsm
ABCDEFG
1TestFemaleMaleCostMarginFinal Price
40ANF1114150%171
41FSH3152150%684
42LH3121.6150%547.2
43E23152150%684
44Prolaction1121.6150%182.4
45Testosterone1190150%285
46DHEA1171150%256.5
47Androstenedione1744.8150%1117.2
48P4118.9150%28.35
49AMH1706.8150%1060.2
50Thalassemia Screening1495.4150%743.1
517339.05
52IVF/ICSI Treatment Sexual DiseaseHBsAg1168.4150%205.2
53Anti-HCV11150%0
54VDRL11150%0
55HIV Ab11150%0
56205.2
Blood_Price
Cell Formulas
RangeFormula
G52:G55,G40:G50G40=E40*(C40+D40)*F40
G51G51=SUM(G36:G50)
G56G56=SUM(G52:G55)
 
Last edited by a moderator:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug. My signature block below has more details. I have added them for you this time. 😊

it successfully index match the price but not for cell E53 onward. Could any expert please help
In column A you have merged cells so your code it evaluating MyLastRow as row 52 and that is why it is stopping there. Since column B appear to not have merged cells, try swapping to that to get MyLastRow

Rich (BB code):
MyLastRow = wsVO.Cells(Rows.Count, 1).End(xlUp).Row
MyLastRow = wsVO.Cells(Rows.Count, 2).End(xlUp).Row
 
Upvote 0
Solution
You're welcome. Thanks for the confirmation. :)
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,678
Members
449,116
Latest member
HypnoFant

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