Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This seems to the do the trick for me. Some of it I have cobbled together from sites like this, the rest has been created by myself from scratch.</p> <p>When I click my button:</p> <ul> <li>An input box appears, which is the value that will be looked up in the spreadsheet.</li> <li>it looks in the range (specified in the code), for a match</li> <li>returns the value, two columns to the left of it.</li> <li>when it finds a match it puts it in the Subject line in Outlook.</li> </ul> <pre class="lang-vb prettyprint-override"><code>Dim jobno As String Dim Proj As String Sub Test() jobno = InputBox("Job Number?", "Test") GetNameFromXL If jobno &lt;&gt; "" Then Set myItem = Application.CreateItem(0) If Proj &lt;&gt; "" Then myItem.Subject = jobno &amp; " - " &amp; Proj &amp; " - " &amp; Format(Date, "dd.mm.yy") Else myItem.Subject = jobno &amp; " - " &amp; Format(Date, "dd.mm.yy") End If myItem.Display Else Exit Sub End If End Sub Sub GetNameFromXL() 'Late binding. No reference to Excel Object required. Dim xlApp As Object Dim xlWB As Object Dim xlWS As Object Set xlApp = CreateObject("Excel.Application") 'Open the spreadsheet to get data Set xlWB = xlApp.Workbooks.Open("X:\...\FILENAME.xlsx") ' &lt;-- Put your file path and name here Set xlWS = xlWB.Worksheets(1) ' &lt;-- Looks in the 1st Worksheet Debug.Print "-----Start of 'For Each' loop" For Each c In xlWS.Range("A6:A100") 'Change range to value you want to 'VLookUp' Proj = c.Offset(0, 2).Value 'This looks at the 2nd column after the range above Debug.Print c &amp; Proj If jobno = c Then Debug.Print "-----Match Found: " &amp; jobno &amp; " = " &amp; Proj GoTo lbl_Exit Else End If Next c Debug.Print "-----End of For Each loop" MsgBox jobno &amp; " not found in WorkBook." 'Clean up Set xlWS = Nothing Set xlWB = Nothing Set c = Nothing Proj = "" xlApp.Quit Set xlApp = Nothing lbl_Exit: Exit Sub End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload