Excel/VBA Find

Advertisement


Question
Hello,

' This summarizes what I am trying to do.
' Find the match for wsDB.range("D7") in wsOE Column E
' If after finding the match, look in same row column "M" for "Out of Stock", if true then
' write "Cancel" in Column A of same row
' otherwise do nothing


Option Explicit
Sub Cancel_Order()
Dim wsOE As Worksheet, wsDB As Worksheet
Dim vFIND As Range

Set wsDB = ThisWorkbook.Sheets("Dashboard")
Set wsOE = ThisWorkbook.Sheets("Order Entry")

If wsDB.Range("F7") > 0 And wsDB.Range("J7") = 1 Then
  Set vFIND = ThisWorkbook.Worksheets("Account 1234-5678").Range("E:E").Find(wsDB.Range("D7"), LookIn:=xlValues, LookAt:=xlWhole)
      If Not vFIND Is Nothing Then

Answer
David,

You say look in wsOE column E, then you set wsOE to "Order Entry" and then your code looks in a third sheet in column E.  So I changed the Find to look in wsOE column E

' This summarizes what I am trying to do.
' Find the match for wsDB.range("D7") in wsOE Column E
' If after finding the match, look in same row column "M" for "Out of Stock", if true then
' write "Cancel" in Column A of same row
' otherwise do nothing


Option Explicit
Sub Cancel_Order()
Dim wsOE As Worksheet, wsDB As Worksheet
Dim vFIND As Range

Set wsDB = ThisWorkbook.Sheets("Dashboard")
Set wsOE = ThisWorkbook.Sheets("Order Entry")


If wsDB.Range("F7").Value > 0 And wsDB.Range("J7").Value = 1 Then
  Set vFIND = wsOE.Range("E:E").Find( _
       What:=wsDB.Range("D7"), _
       After:=wsOE.cells(wsOE.rows.count, "E"), _
       LookIn:=xlValues, _
       LookAt:=xlWhole, _
       SearchOrder:=xlByRows, _
       SearchDirection:=xlNext, _
       MatchCase:=False)
      If Not vFIND Is Nothing Then
        if instr(1, wsOE.Cells(vFind.row, "M").Value, _
         "out of stock", vbTextCompare)  then
         wsOE.Cells(vFind.row, "A").Value = "Cancel"
        end if
      end if
End if

End Sub

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.