0

I want to grab a unique value from Outlook mail, look for that value in a column of an Excel file, and return an associated value.

I am working with the .find function from the Excel library to lookup my unique value. find is supposed to return a range of the first occurrence of my value but I cannot assign that value to the variable: pointer.

How can I reference it?

Sub OTM1S() '(ByVal Item As Object)
    Dim xlApp As Object
    Dim wb As Workbook
    Dim pointer As Range
    Set xlApp = CreateObject("Excel.Application")
    Set wb = xlApp.Workbooks.Open("I:\referencefile.xlsx")

    'On Error Resume Next
    pointer = wb.Sheets("Bonds").Range("A1:A10000").Find("XS1058142081")
    MsgBox pointer.Offset(0, 1)
    'On Error GoTo 0
 
    wb.Save
    wb.Close

End Sub

1 Answer 1

2

Where you are trying to set an object reference you need the Set keyword. Try this:

Sub OTM1S() '(ByVal Item As Object)
    Dim xlApp As Object
    Dim wb As Workbook
    Dim pointer As Range
    Set xlApp = CreateObject("Excel.Application")
    Set wb = xlApp.Workbooks.Open("I:\referencefile.xlsx")

    'On Error Resume Next
    Set pointer = wb.Sheets("Bonds").Range("A1:A10000").Find("XS1058142081")
    MsgBox pointer.Offset(0, 1)
    'On Error GoTo 0

    wb.Save
    wb.Close

End Sub

You should also handle the scenario where the reference is not found. This can be done like so:

Set pointer = wb.Sheets("Bonds").Range("A1:A10000").Find("XS1058142081")
If Not pointer Is Nothing Then
    MsgBox pointer.Offset(0,1)
Else
    MsgBox "Sorry, couldn't find that in the specified range."
End If
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.