Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if value exists in column in VBA

I have a column of numbers of over 500 rows. I need to use VBA to check if variable X matches any of the values in the column.

Can someone please help me?

like image 966
Trung Tran Avatar asked Sep 28 '12 14:09

Trung Tran


People also ask

How do you check if value exists in column Excel VBA?

FYI, you can do much easier than the match function: =countif(A:A,12345)>0 will return True if the number is found, false if it isn't.

How do you check if a value exists in a column?

You can use the MATCH() function to check if the values in column A also exist in column B. MATCH() returns the position of a cell in a row or column. The syntax for MATCH() is =MATCH(lookup_value, lookup_array, [match_type]) . Using MATCH, you can look up a value both horizontally and vertically.

How do you check if a value is in an array VBA?

Use Match() function in excel VBA to check whether the value exists in an array.


1 Answers

The find method of a range is faster than using a for loop to loop through all the cells manually.

here is an example of using the find method in vba

Sub Find_First() Dim FindString As String Dim Rng As Range FindString = InputBox("Enter a Search value") If Trim(FindString) <> "" Then     With Sheets("Sheet1").Range("A:A") 'searches all of column A         Set Rng = .Find(What:=FindString, _                         After:=.Cells(.Cells.Count), _                         LookIn:=xlValues, _                         LookAt:=xlWhole, _                         SearchOrder:=xlByRows, _                         SearchDirection:=xlNext, _                         MatchCase:=False)         If Not Rng Is Nothing Then             Application.Goto Rng, True 'value found         Else             MsgBox "Nothing found" 'value not found         End If     End With End If End Sub 
like image 121
scott Avatar answered Sep 23 '22 05:09

scott