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 9051687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:58:07+00:00 2026-06-16T12:58:07+00:00

I would like to modify this function : Custom Excel VBA Function (Modified VLOOKUP)

  • 0

I would like to modify this function : Custom Excel VBA Function (Modified VLOOKUP) from Cell referring to a range in different file gives an error

functionality I need is conceptualy simple – I need VlookUp which return value that corresponds to k-th occurance of lookup value instead of standard 1-th, example :

If k-th occurance doesn’t exist then function should return an error.

Spreadsheet-like data :

     A       B   
1   "a"    "1a"   
2   "a"    "2a"    
3   "b"    "1b"
4   "a"    "3a"
5   "b"    "2a"

VLOOKUPnew(lookup_value =A1, table_array =A1:B3, 
        col_index_num = 2, exactMatch =0, k=1) should return 1a

VLOOKUPnew(lookup_value =A1, table_array =A1:B3, 
        col_index_num = 2, exactMatch =0, k=2) should return 2a

VLOOKUPnew(lookup_value =A1, table_array =A1:B3, 
        col_index_num = 2, exactMatch =0, k=3) should return 3a

VLOOKUPnew(lookup_value =A3, table_array =A1:B3, 
        col_index_num = 2, exactMatch =0, k=1) should return 1b

VLOOKUPnew(lookup_value =A3, table_array =A1:B3, 
        col_index_num = 2, exactMatch =0, k=2) should return 2b

VLOOKUPnew(lookup_value =A3, table_array =A1:B3, 
        col_index_num = 2, exactMatch =0, k=3) should return error

I’m familiar with R and Matlab, so my thinking is vector oriented, I’ve first tried to write code for case witk k=1 or 2 by rewriting one line of code (from post I’m linking to) :

row = .Match(lookup_value, table_array.Columns(1), 0)

into :

If k =2 Then
row_1 = .Match(lookup_value, table_array.Columns(1), 0)
number_of_rows=table_array.Columns(1).Rows.Count

row = .Match(lookup_value, table_array.Columns(1).Rows( (row_1+1):number_of_rows ), 0)

above line is pseudocode because I don’t have any idea how to write it properly (.Rows( (row_1+1):number_of_rows ) is vector of numbers and it looks quite funny)

else
row = .Match(lookup_value, table_array.Columns(1), 0)
End If

for k > 2 it would be simple (but inefficient) to put this code into for loop.

I’ve noticed that modified .Match() which takes also k as parameter would make all job needed. Using loop for to find position of k-th occurance of value seems to be quite slow or mayby I’m just not very familiar with VBA.

  • 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-06-16T12:58:08+00:00Added an answer on June 16, 2026 at 12:58 pm

    You may try out both of these Excel based formula: Adjust according to your data table.

    • Method 1:

    CountIF function allows you to count number of occurances of a lookup value in a column range.

    =COUNTIF(columnRange,lookupvalue)

    Assume this is what you may be looking for: Data extracted from the reference.

    CUST column is populated using =F78&COUNTIF($F$75:$F78,F78)

    Master Data Starts from `F75 to H84`    
    Customer    CUST    Phone number
    Smith   Smith1  320-966-4023
    Smith   Smith2  686-612-7782
    Jason   Jason1  122-617-7154
    Albert  Albert1 547-436-7376
    Nancy   Nancy1  956-633-7322
    Smith   Smith3  132-716-5240
    Grove   Grove1  340-267-0529
    Andy    Andy1   531-413-4718
    Jason   Jason2  613-228-4294
    Nancy   Nancy2  272-525-2042
    

    Final nth Lookup:

    e.g. Phonenumber for 4th occurance for Customer = Smith

    =VLOOKUP($D$74&"4",$G$75:$H$93,2,FALSE)

    Lookup  
    Customer    Smith
    Phone number    
    1st 320-966-4023
    2nd 686-612-7782
    3rd 132-716-5240
    4th 185-813-8883
    

    Reference from Chandoo: 4. Lookup 2nd / 3rd / 4th occurrence of an item in a list.

    • Method 2:
      Sample data used for following formula:

    enter image description here

    Formula:

    =INDEX(ALTable,SMALL(IF(OFFSET(ALTable,0,0,ROWS(ALTable),1)=F90,
    ROW(OFFSET(ALTable,0,0,ROWS(ALTable),1))-ROW(OFFSET(ALTable,0,0,1,1))+1,
    ROW(OFFSET(ALTable,ROWS(ALTable)-1,0,1,1))+1),F91),2)
    

    Reference from CPearson Arbitary Lookups.
    Personally I don’t fancy volatile functions such as index()…though..

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

Sidebar

Related Questions

I have to the following function that I would like to modify so that
Please see working code here: http://jsfiddle.net/hrnDG/6/ I would like to modify this code so
I have this function that works, but I would like to make this one
Actually i would like to modify the replaceWord function of the spellchecker . I
I would like modify HTML like I am <b>Sadi, novice</b> programmer. to I am
I would like to modify an object private variable class Example(): __myTest1 = 1
We have a customer that would like to modify application user messages that we
I'm absolutely new to JavaScript and would like to modify a textarea of a
I'm using HighLine to write my console application and I would like to modify
I have code, similar to the following, that I would like to modify: Sub

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.