VBA code to check whether value is in a list using if command

kanishkgarg

New Member
Joined
Sep 29, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a peculiar problem. I need to check whether one cell value is in a particular range or not. Pasted below are the relevant lines of code which are part of a bigger macro.

VBA Code:
Dim UserRange, FoundCell1 As Range
Dim fndFruit As String
Dim OutApp As Object
fndFruit = "Fruit"
Set UserRange = Application.InputBox(Prompt:="Please Select Range", Title:="Range Select", Type:=8)
Set FoundCell1 = UserRange.Find(what:=fndFruit)
If FoundCell1.Cells.Offset(1, 0)  = Worksheets("A").Range("C3:C50")  'this is the line of code i need help with. It should be so that if the value is in a specific range (SheetA!"C3:C50)  then it will send an email
With objMsg
           .body = WksMail.Range("A3") & vbNewLine & signature
           .CC = "example@hotmail.de"
           .Subject = "Verkaufauftrag  Depot: " & FoundCell2.Cells.Offset(1, 0) & "/" & FoundCell3.Cells.Offset(1, 0) & "  " & FoundCell4.Cells.Offset(1, 0) & " Anteile WKN: " & FoundCell5.Cells.Offset(1, 0) & "  " & Left(FoundCell6.Cells.Offset(1, 0), 20)
           .To = FoundCell1.Cells.Offset(1, 0)
           .Display
        End With
    Else
    'Next rngCellA
        GoTo NothingFound0
    End If

This is how data is present in the sheet (Sheet name is 'Data') i work in

FruitDestinationOriginTime
GrapeLondonParis12:00


And this is the data in 'SheetA':

mango
grape
banana
pear
apple


Basically, if i enter the data in the sheet, the macro will check whether the the input is in SheetA and if so, would send the email. Any help would be appreciated.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
See if this is what you had in mind.
VBA Code:
Sub test()

    Dim UserRange, FoundCell1 As Range
    Dim fndFruit As String
    Dim OutApp As Object
    fndFruit = "Fruit"
    Set UserRange = Application.InputBox(Prompt:="Please Select Range", Title:="Range Select", Type:=8)
    Set FoundCell1 = UserRange.Find(what:=fndFruit)
 
    If Not FoundCell1 Is Nothing Then
        Dim fndMatch As Long
     
        With Application
            fndMatch = .IfError(.Match(FoundCell1.Cells.Offset(1, 0), Worksheets("A").Range("C3:C50"), 0), 0)
        End With
     
        If fndMatch <> 0 Then
            With objMsg
                .body = WksMail.Range("A3") & vbNewLine & Signature
                .CC = "example@hotmail.de"
                .Subject = "Verkaufauftrag  Depot: " & FoundCell2.Cells.Offset(1, 0) & "/" & FoundCell3.Cells.Offset(1, 0) & "  " & FoundCell4.Cells.Offset(1, 0) & " Anteile WKN: " & FoundCell5.Cells.Offset(1, 0) & "  " & Left(FoundCell6.Cells.Offset(1, 0), 20)
                .To = FoundCell1.Cells.Offset(1, 0)
                .Display
            End With
        Else
            'Next rngCellA
            MsgBox FoundCell1.Cells.Offset(1, 0).Value & " - Not found in Sheet A"
        End If
       
    Else
        GoTo NothingFound0
    End If
  
NothingFound0:
 
End Sub
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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