Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1093143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:47:12+00:00 2026-05-16T23:47:12+00:00

Given A search key ————————— | | A | B | C | D

  • 0

Given

A search key

---------------------------
|   |  A  | B | C |   D   |
---------------------------
| 1 | 01  | 2 | 4 | TextA |
---------------------------

An Excel sheet

------------------------------
|   |  A | B | C |   D   | E |
------------------------------
| 1 | 03 | 5 | C | TextZ |   |
------------------------------
| 2 | 01 | 2 | 4 | TextN |   |
------------------------------
| 3 | 01 | 2 | 4 | TextA |   |
------------------------------
| 4 | 22 | T | N | TextX |   |
------------------------------

Question

I would like to have a function like this: f(key) -> result_row.

That means: given a search key (01, 2, 4, TextA), the function should tell me that the matching row is 3 in the example above.

The values in the search key (A, B, C, D) are a unique identifier.

How do I get the row number of the matching row?

One solution

The solution that comes first to my mind is to use Visual Basic for Application (VBA) to scan column A for “01”. Once I’ve found a cell containing “01”, I’d check the adjacent cells in columns B, C and D whether they match my search criteria.

I guess that algorithm will work. But: is there a better solution?

Version

  • Excel 2000 9.0.8961 SP3
  • VBA 6.5

However, if you happen to know a solution in any higher versions of Excel or VBA, I am curious to know as well.

Edit: 22.09.2010

Thank you all for your answers. @MikeD: very nice function, thank you!

My solution

Here is the solution I prototyped. It’s all hard-coded, too verbose and not a function as in MikeD’s solution. But I’ll rewrite it in my actual application to take parameters and to return a result value.

Sub FindMatchingRow()
    Dim searchKeyD As Variant
    Dim searchKeyE As Variant
    Dim searchKeyF As Variant
    Dim searchKeyG As Variant

    Const indexStartOfRange As String = "D6"
    Const indexEndOfRange As String = "D9"

    ' Initialize search key
    searchKeyD = Range("D2").Value
    searchKeyE = Range("E2").Value
    searchKeyF = Range("F2").Value
    searchKeyG = Range("G2").Value


    ' Initialize search range
    myRange = indexStartOfRange + ":" + indexEndOfRange

    ' Iterate over given Excel range
    For Each myCell In Range(myRange)

        foundValueInD = myCell.Offset(0, 0).Value
        foundValueInE = myCell.Offset(0, 1).Value
        foundValueInF = myCell.Offset(0, 2).Value
        foundValueInG = myCell.Offset(0, 3).Value

        isUnitMatching = (searchKeyD = foundValueInD)
        isGroupMatching = (searchKeyE = foundValueInE)
        isPortionMatching = (searchKeyF = foundValueInF)
        isDesignationMatching = (searchKeyG = foundValueInG)
        isRowMatching = isUnitMatching And isGroupMatching And isPortionMatching And isDesignationMatching

        If (isRowMatching) Then
            Range("D21").Value = myCell.Row
            Exit For
        End If

    Next myCell
End Sub

This is the Excel sheet that goes with the above code:

alt text

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:47:12+00:00Added an answer on May 16, 2026 at 11:47 pm

    here’s a small VBA function I often use for that kind of purpose

    Function FindInRange(InRange As Range, Arg As Range) As Integer
    Dim Idx As Integer, Jdx As Integer, IsFound As Boolean
    
        FindInRange = 0
        IsFound = False
    
        For Idx = 1 To InRange.Rows.Count
            IsFound = True
            For Jdx = 1 To InRange.Columns.Count
                If InRange(Idx, Jdx) <> Arg(1, Jdx) Then
                    IsFound = False
                    Exit For
                End If
            Next Jdx
    
            If IsFound Then
                FindInRange = Idx
                Exit For
            End If
        Next Idx
    
    End Function
    

    InRange must be same width or wider than Arg, but can be of course larger or smaller than your A:D

    In your Sample sheet enter in an empty cell “=findinrange(A1:D4,A3:D3)”

    The formula will return “0” if nothing found, else the row #

    Good luck – MikeD

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I was given a URL. It might already have GET parameters (e.g. http://example.com/search?q=question
I have to search a given file name (let say Keyword) in a directory
I have a script which allows to display search results. Given a query there
alt text http://img216.imageshack.us/img216/2288/problem11.png As given in the picture, I have an search bar in
Is it possible to search by key value in Apache CouchDB? Given the sample
I am trying to write a .reg file that would take a given key,
I have a doubt about one of my book's statement. Talking about key-indexed search
Given a search engine like Lucene and a set of XML documents which need
On one hand it is easy to see given a key function, one can
I have a task and would like to develop in my mind how i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.