VBA Check If Variable Exists in a Column

ckp90c

New Member
Joined
Dec 22, 2016
Messages
9
Hi all,

Just a question about checking if a variable exists in a certain column.

I want to find out if a variable is located in Column A in another sheet.

I have an If statement that currently says below...

If VARIABLE <> "" Then

Output.Cells(m, 1) = xxx
Output.Cells(m, 2) = yyy
Output.Cells(m, 3) = zzz


However, I would rather it check if the Variable is in a range of cells instead.

Is there an easy way to do this?

Thanks
 

Some videos you may like

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.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
Depends or how your variable is in the cells and what the data type is in some cases. But here are a couple of ways.
Code:
If Application.CountIf(Range("A:A"), variable) > 0 Then 'if it is a stand alone value
 MsgBox "It's there"
End If
Dim c As Range
For Each c In Range("A:A")
 If InStr(c.Value, variable) > 0 Then 'Embeded in other data
  MsgBox "It's there"
 End If
Next
End If
 

Watch MrExcel Video

Forum statistics

Threads
1,123,318
Messages
5,600,924
Members
414,416
Latest member
Nobu

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