Quick Help Plz

momo3

New Member
Joined
Jul 25, 2019
Messages
1
Hello,
I am starting with VBA and I'd like to know if it was possible to use list from a worksheet inside the code. I wouldn't want to create an array as my list is pretty long. I would like to have an if statement that goes like:

If Range("A1").Value IN [a liste of value inside the worksheet] Then
...

Is there a way to do that?
Thanks in advance!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
60,189
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
One way to do that is with a Dictionary, like
Code:
Sub momo3()
   Dim Ary As Variant
   Dim i As Long
   Dim Dic As Object
   Dim Cl As Range
   
   Ary = Sheets("List").Range("A1").CurrentRegion.Value2
   Set Dic = CreateObject("scripting.dictionary")
   For i = 1 To UBound(Ary)
      Dic.Item(Ary(i, 1)) = Empty
   Next i
   With Sheets("Sheet1")
      For Each Cl In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
         If Dic.Exists(Cl.Value) Then
            'do something
         End If
      Next Cl
   End With
End Sub
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,321
Office Version
  1. 2007
Platform
  1. Windows
Welcome to the forum

Try this

Code:
Sub dam_test1()
  Dim r As Range, f As Range
  Set r = Range("B1", Range("B" & Rows.Count).End(xlUp))
  Set f = r.Find(Range("A1"), , xlValues, xlWhole)
  If Not f Is Nothing Then
    MsgBox "exists, do some"
  Else
    MsgBox "Does not exists"
  End If
End Sub
 

Forum statistics

Threads
1,136,868
Messages
5,678,226
Members
419,754
Latest member
Sallylwy

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
Top