Browse » Home
Sunday, September 16, 2012
How to Search a Cell Range for a Matching Value in Excel VBA
1. Open the worksheet in which you want to find a match. Press the 'Alt' and 'F11' keys together. This opens the Visual Basic Editor (VBE).
2. Click 'Insert' and then click 'Module.' This inserts a blank module window.
3. Copy and paste the following code into the blank window:Sub Find_First()Dim FindString As StringDim Rng As RangeFindString = InputBox('Enter a Search value')If Trim(FindString)
'' ThenWith Sheets('Sheet1').Range('A1:Z256')Set Rng = .Find(What:=FindString, _After:=.Cells(.Cells.Count), _LookIn:=xlValues, _LookAt:=xlWhole, _SearchOrder:=xlByRows, _SearchDirection:=xlNext, _MatchCase:=False)If Not Rng Is Nothing ThenApplication.Goto Rng, TrueElseMsgBox 'Nothing found'End IfEnd WithEnd IfEnd Sub
4. Change the 'Range' in the code to the cell range in which you want to look for a match. For example, if you want to look in cells C1:C1000, type 'C1:C1000' in place of 'A1:Z256.'
5. Press 'F5,' then type the string of text or data you want to find into the text box. Excel will find the match and highlight the cell.