2016-11-28 17 views
0

私はoutlookにvba subを書いています。メールから一意の値を取得し、その値をExcelファイルの列で探し、関連する値を返します。私はExcelのライブラリの.find関数を使用してExcelの独自の値を検索していますが、findは最初の値の範囲を返しますが、その値を変数:pointerに割り当てることはできません。私はそれを参照することはできません。どんな洞察も高く評価されます。ありがとうございました!VBA Outlook/Excel

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 

答えて

2

オブジェクト参照を設定しようとしているところでは、Setキーワードが必要です。試してください:

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 

また、参照が見つからないシナリオも処理する必要があります。これはそうすることができます:

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