Index and Match array in VBA

Lefemmenikita

Board Regular
Joined
Jan 28, 2014
Messages
59
Office Version
  1. 2013
Platform
  1. Windows
Hi

I am dealing with thousands of rows of data.

I am trying to use the multiple criteria index and match in a VBA formula to return a result.

The formula I am currently using is:

Code:
Selection.FormulaArray ="=IF(RC[-3]=""Billable"",INDEX(Coding!C1:C5,MATCH(1,(Coding!C1=Data!RC11)*(Coding!C2=Data!RC1),0),3),INDEX(Coding!C1:C5,MATCH(1,(Coding!C1=Data!RC11)*(Coding!C2=Data!RC1),0),4))"
    Range("P2").Select
   
    Columns("P:P").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

This runs out of memory before cycling through all the cells in the column and returning a result

My question is:

Is there a way to make an index/match formula use a dynamic column reference? (therefore, eliminating the need to have an "if" statement in the above array formula)?

Also, is there a way to have an array index/match formula execute via VBA without running out of memory?

I am able to do this outside of VBA, though it takes around 5 mins for Excel to finish calculating the result before I am able to replace the formula with the calculated value.


Hope my question makes sense

Thanks
 
The only thing I can see wrong with the code is the declarations, the fact I is an integer might be the probelm
SO change the declarations to:
VBA Code:
Dim LastMeasureDesc ' this defaults to varaint which works fine
Dim LastData
Dim j As Long
Dim i As Long

Dim Data
Dim Results
Dim MeasureDesc
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I update the declarations to what you show and it's still not iterating through the ValidMeasureDescription tab correctly.

VBA Code:
Sub Test()

Dim LastMeasureDesc
Dim LastData
Dim j As Long
Dim i As Long

Dim Data
Dim Results
Dim MeasureDesc

With Worksheets("ValidMeasureDescriptions")
 LastMeasureDesc = .Cells(Rows.Count, "A").End(xlUp).Row
 MeasureDesc = Range(.Cells(1, 1), .Cells(LastMeasureDesc, 4))
End With


With Worksheets("Measure Fields")
 LastData = .Cells(Rows.Count, "A").End(xlUp).Row
 Data = Range(.Cells(1, 1), .Cells(LastData, 195))
 
 
 Range(.Cells(2, 195), .Cells(LastData, 195)) = "Not found"
   Results = Range(.Cells(1, 195), .Cells(LastData, 195))


' I assume there is a header row so I start on row 2
For j = 2 To LastData
  For i = 2 To LastMeasureDesc
  
Debug.Print "MeasureDesctiptionName: " & (MeasureDesc(i, 1))
Debug.Print "DataName: "; (Data(j, 189))

Debug.Print "MeasureDesctiptionType: " & (MeasureDesc(i, 2))
Debug.Print "DataType: " & (Data(j, 194))

    If MeasureDesc(i, 1) = Data(j, 189) And MeasureDesc(i, 2) = Data(j, 194) Then
     ' code and description match so check billable
       Results(j, 1) = MeasureDesc(i, 4)
      End If
     Exit For
   Next i
 Next j
 
' write the results out
 Range(.Cells(1, 195), .Cells(LastData, 195)) = Results
End With
 
End Sub

Even at the last rows of the Measure Fields tab, the Measure Description names and types aren't finding the match:
MeasureDesctiptionName: Advanced Power Strip-$10 Copay-Joint HEA
DataName: Showerhead-Gas DHW-Joint HEA
MeasureDesctiptionType:
DataType: VA
 
Upvote 0
ValidMeasureDescription Tab Layout
Old NameLookup HelperExtra ColumnNew Name
Measure Name 1New Measure Name 1
Measure Name 2New Measure Name 2
Measure Name 3DIMNew Measure Name 3
Measure Name 3VANew Measure Name 4
 
Upvote 0
I can't see anything wrong with the code, the fact is actually does complete some of it implies that there is something wrong with how it detects the last row of data. Have checked the values in
Lastdata and Lastmeasuredesc which are the two variables that control the loop. Use debug print or put a breakpoint in the code and check with debug or viewlocals If these are the correct values then the only other expanation is that the check:
VBA Code:
If MeasureDesc(i, 1) = Data(j, 189) And MeasureDesc(i, 2) = Data(j, 194) Then
Fails after a certain number of rows which means the data isn't quite as you expect it
 
Upvote 0
As I am stepping through the debugger, I am never getting to
VBA Code:
 Next i

If I move Next i higher into the code, I get Compile Error: Next without For
 
Upvote 0
Don't move the next i it is correct, you need to check the value in Lastdata and Lastmeasuredesc just before the loops
 
Upvote 0
useing the debugger put a break point on the if statement and check the values there, that must be where it is failing
 
Upvote 0
This is what I changed.

Original Code:
VBA Code:
If MeasureDesc(i, 1) = Data(j, 189) And MeasureDesc(i, 2) = Data(j, 194) Then
     ' code and description match so check billable
       Results(j, 1) = MeasureDesc(i, 4)
      End If
     Exit For
   Next i
 Next j



New Code:
VBA Code:
If MeasureDesc(i, 1) = Data(j, 189) And MeasureDesc(i, 2) = Data(j, 194) Then
     ' code and description match so check billable
       Results(j, 1) = MeasureDesc(i, 4)

      End If
    Next i
 Next j
 
Upvote 0
I have just spotted that the exit for is in the wrong place it should be inside the if statement
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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