Solved

Excel VBA- fastest way to find the row number of a cell with a certain value in a range's column

Posted on 2013-01-01
3
3,235 Views
Last Modified: 2013-01-02
Hi

Given a certain range, what is the fastest way to find the row number of a cell with a certain value in column 2 ?
0
Comment
Question by:murbro
3 Comments
 
LVL 10

Expert Comment

by:tdlewis
ID: 38734947
The following code shows how to do it two different ways:
return the row within the range
return the row number within the worksheet

If the value is not found, the function returns -1.
Sub test()
    MsgBox (getRow(ActiveSheet.Range("B2:C5"), 2, 5))
    MsgBox (getRow(ActiveSheet.Range("B2:C5"), 2, 10))
End Sub

Function getRow(rng As Range, theColumn as Long, theValue As Variant)
Dim r As Long
  
    getRow = -1
    For r = 1 To rng.Rows.Count
        If rng.Cells(r, theColumn).Value = theValue Then
            ' Choose only one of the following assignments depending on what you want
            ' If you want the row with the range
            getRow = r
            ' if you want the absolute row number in the worksheet
            getRow = r + rng.Cells(1, 1).Row - 1
            Exit Function
        End If
    Next r
End Function

Open in new window

If the range C2:C5 contains the values 1,3,5,7 then the first call in the test subroutine will return 4 (the absolute row) and the second will return -1.
0
 
LVL 16

Accepted Solution

by:
terencino earned 500 total points
ID: 38735782
This uses the Find method, in the Worksheet_Change event...
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("A2"), Target) Is Nothing Then
    ActiveSheet.Range("A4").Value = ActiveSheet.Columns("B:B").Find(Range("A2").Value).Row
End If
End Sub

Open in new window

Pretty quick
...Terry
FastFind.xlsm
0
 

Author Closing Comment

by:murbro
ID: 38735785
Thanks very much
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now