vba help - in Creating Vlookup Function

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
983
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

Need help in creating UDF Function for Vlookup. which I come across daily.

Convert below formula into UDF Vlookup.
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],[Scores.xlsx]Sheet1!R4C4:R7C5,2,0)"

Below is my attempted Code. which is not working, I am not good in function.

VBA Code:
Sub Create_vlookupFunction()

    Dim Twbk As Workbook
    Set Twbk = ThisWorkbook
  
    Dim sht_output As Worksheet
    Set sht_output = Twbk.Worksheets(1) ' expected output in this sheet
  
  
    Dim input_wbk As Workbook
    Set input_wbk = Workbooks.Open("D:\Testing Folder\Scores.xlsx")
     
  
    Dim Input_Table As Range
    Dim TableString As String
  
    Dim sht_Input As Worksheet
    Set sht_Input = input_wbk.Worksheets(1)
  
 
   Set Input_Table = sht_Input.Range("B3:b6")
    TableString = Input_Table.Address(, , xlR1C1)  'Input Table Range Converted into String
  
  
    Dim Lookup_Column As Long
    Lookup_Column = Application.WorksheetFunction.Match("PlayerName", sht_output.Range("B3:C3"), 0) 'Look up Column
 
          
     Dim Find_Column As Long 'Column index no
    Find_Column = Application.WorksheetFunction.Match("Score", sht_Input.Range("D4:E4"), 0) 'Find Column
  
  
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],[Scores.xlsx]Sheet1!R4C4:R7C5,2,0)"
  
    sht_output.Range("C4:C16").formulaRICI = Vlookup(wbk, ws, TableString, Lookup_Column, Find_Column)
  
  
    End Sub


Function Vlookup(ByVal wbk_Input As Workbook, ByVal sht_Input As Worksheet, ByVal Table As String, ByVal Lookup_Column As Long, ByVal Find_Column As Long) As String

    Dim str As String

    If InStr(wbk_Input.Name, " ") = 0 Then
    'ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],[Scores.xlsx]Sheet1!R4C4:R7C5,2,0)" 'If workbook Contains Space
      '.FormulaR1C1 = "=VLOOKUP(RC" & Lookup_Column & ",[" & wbk_Input.Name & "]" & sht_Input.Name & "!" & Table & ",Find_Column,0)"
     str = "=VLOOKUP(RC" & Lookup_Column & ",[" & wbk_Input.Name & "]" & sht_Input.Name & "!" & Table & ",Find_Column,0)"
  
  
    Else
    'ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],[Scores.xlsx]Sheet1!R4C4:R7C5,2,0)" If workbook Contains Space
     '.FormulaR1C1 = "=VLOOKUP(RC" & Lookup_Column & ",[" & wbk_Input.Name & "]" & sht_Input.Name & "'!" & Table & ",Find_Column,0)"
     str = "=VLOOKUP(RC" & Lookup_Column & ",[" & wbk_Input.Name & "]" & sht_Input.Name & "'!" & Table & ",Find_Column,0)"
  
    End If

End Function

Table Column C to Fill.
Book16
BC
3PlayerNameScore
4Sachin
5Sehwag
6Dhoni
Sheet1


Table Lookup , extract data from Column E (Score)
Scores.xlsx
DE
3Suppose Table1 from Closed workbook
4PlayerNameScore
5Sachin100
6Sehwag80
7Dhoni45
Sheet1



Thanks in advance for your help.

Thanks
mg
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
What is wrong with the native vlookup function? Everything that you are attempting appears to be a recipe for disaster.

You will be more likely to receive help if you explain what you want to do (not how you want to do it) rather than just showing us your unsuccessful attempts.
 
Upvote 0

Forum statistics

Threads
1,224,621
Messages
6,179,937
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top