Index Match Match in VBA

CarolynS

Board Regular
Joined
Oct 27, 2015
Messages
56
Hi

I've been trying to use index match match in vba and I just can't get it right. I've tried a couple of ways. The 1st thing I tried was:

Code:
Me.Controls("POinv" & x).Value = Application.Index(wsP.Range("B:L"), Application.Match(Me.Inv2.Value, wsP.Range("K:K"), 0), Application.Match(wsP.Cells(3, x + 1), wsP.Range("3:3"), 0))

I am getting an error saying "could not find specified object"

I then tried

Code:
       With Application
            Col = .Match(Me.Inv2.Value, wsP.Range("K:K"), 0)
            Rw = .Match(wsP.Cells(3, x + 1), wsP.Range("3:3"), 0)
            Me.Controls("POinv" & x).Value = .Index(wsP.Range("B:L"), Col, Row)
        End With

This is also giving the "could not find specified object" error and the error is occuring on the 4th line of code however, the problem seems to be the Col variable as this variable is shwoing a value of "error 2042". Rw variable seems to be correct.

What am I doing wrong?

Full code below for reference:

Code:
Private Sub CommandButton2_Click()
Dim wb As Workbook
Dim WSi As Worksheet
Dim wsP As Worksheet
Dim Cellref As String
Dim x As Integer
Dim Rw As Variant
Dim Col As Variant

Set wb = Workbooks("Learning and Development Purchase Order and Invoice Tracker")
Set WSi = wb.Sheets("InvoiceLists")
Set wsP = wb.Sheets("PORequestLists")

'validate purchase order info on invoice form
 
'unlock text boxes
'checkn if PO number exists
    If WorksheetFunction.CountIf(wsP.Range("K:K"), Me.Inv2.Value) = 0 Then
        MsgBox "No corresponding purchase order found. Please try again!"
        Exit Sub
    End If
    
    For x = 1 To 11
    
        'Me.Controls("POinv" & x).Value = Application.Index(wsP.Range("B:L"), Application.Match(Me.Inv2.Value, wsP.Range("K:K"), 0), Application.Match(wsP.Cells(3, x + 1), wsP.Range("3:3"), 0))
        
       With Application
            Col = .Match(Me.Inv2.Value, wsP.Range("K:K"), 0)
            Rw = .Match(wsP.Cells(3, x + 1), wsP.Range("3:3"), 0)
            Me.Controls("POinv" & x).Value = .Index(wsP.Range("B:L"), Col, Row)
        End With
            
            
        Next

End Sub
 
Try

Code:
Dim MyVar As Variant
On Error Resume Next
MyVar = CDbl(Me.Inv2.Value)
On Error GoTo 0
If IsEmpty(MyVar) Then MyVar = Me.Inv2.Value
With Application
    Col = .Match(wsP.Cells(3, x + 1), wsP.Range("3:3"), 0)
    Rw = .Match(MyVar, wsP.Range("K:K"), 0)
    Me.Controls("POinv" & x).Value = .Index(wsP.Range("B:L"), Rw, Col)
 End With
MyVar = Empty
 
Upvote 0

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