Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Excel VBA Vlookup

Hello

This is my first Excel VBA to publish in the forum and happy to join
the Excel VBA family.


Excel vlookup function is very useful in many applications.


I develop a VBA to make Vlookup run faster if you havew many rows to
lookup
, please try the code.


I appreciate your comment.


Option Explicit


Dim finalrow As Integer
Dim Mysheet As String
Dim mycolumn As Long
Dim Myrange As String
Dim mycount As Long
Dim i As Integer
Dim mylookup As Variant
Dim mystring As String
Dim myvalue As Long


Sub Vlookup()


mycolumn = Application.InputBox("Please enter the lookup column
number", Type:=1)
myvalue = Application.InputBox("Please enter the column number you want

to place your lookup up result ", Type:=1)
Mysheet = InputBox("Please enter lookup worksheet name",
"WorksheetName", Default)
Myrange = InputBox("Please enter lookup range", "Range", Default)


mycount = Range(Myrange).Columns.Count


On Error Resume Next


Columns(myvalue).Insert Shift:=xlToRight


finalrow = Cells(65536, mycolumn).End(xlUp).Row


For i = 1 To finalrow


mystring = (Cells(i, mycolumn))


mylookup = Application.Vlookup((mystring),
Worksheets(Mysheet).Range(Myrange), mycount, False)


Cells(i, myvalue).Value = mylookup


Next i


End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Excel VBA Vlookup

There's a few things that you can do to improve your code: use ranges for your input boxes, and
don't loop through the cells - do them all at once using formulas. And never name a macro with a
used function name - that's bad practice.

See the code below for a very flexible VLOOKUP formula creation routine - the lookup values,
results, and data table can be on any sheet in the workbook, anywhere. And it can leave the formula
as a formula....

HTH,
Bernie
MS Excel MVP

Option Explicit

Dim FirstRow As Long
Dim FinalRow As Long
Dim myValues As Range
Dim myRange As Range
Dim myResults As Range
Dim myCount As Integer

Sub VlookupMacro()

Set myValues = Application.InputBox("Please select the first cell in" & _
" the column with the values that you're looking for", Type:=8)
Set myResults = Application.InputBox("Please select the first cell " & _
" where you want your lookup results to start ", Type:=8)
Set myRange = Application.InputBox("Please select the entire lookup data table range" & _
" - with the desired values as the last column", Type:=8)

myCount = myRange.Columns.Count

On Error Resume Next
myResults.EntireColumn.Insert Shift:=xlToRight
Set myResults = myResults.Offset(, -1)
FirstRow = myValues.Row
FinalRow = Cells(65536, myValues.Column).End(xlUp).Row

Range(myResults, myResults.Offset(FinalRow - FirstRow)).Formula = _
"=VLOOKUP(" & Cells(FirstRow, myValues.Column).Address(False, False, , True) & ", " & _
myRange.Address(True, True, , True) & "," & myCount & ", False)"

If MsgBox("Do you want to convert to values?", vbYesNo) = vbNo Then Exit Sub

Columns(myResults.Column).Copy
Columns(myResults.Column).PasteSpecial xlPasteValues
Application.CutCopyMode = False

End Sub





wrote in message
oups.com...
Hello

This is my first Excel VBA to publish in the forum and happy to join
the Excel VBA family.


Excel vlookup function is very useful in many applications.


I develop a VBA to make Vlookup run faster if you havew many rows to
lookup
, please try the code.


I appreciate your comment.


Option Explicit


Dim finalrow As Integer
Dim Mysheet As String
Dim mycolumn As Long
Dim Myrange As String
Dim mycount As Long
Dim i As Integer
Dim mylookup As Variant
Dim mystring As String
Dim myvalue As Long


Sub Vlookup()


mycolumn = Application.InputBox("Please enter the lookup column
number", Type:=1)
myvalue = Application.InputBox("Please enter the column number you want

to place your lookup up result ", Type:=1)
Mysheet = InputBox("Please enter lookup worksheet name",
"WorksheetName", Default)
Myrange = InputBox("Please enter lookup range", "Range", Default)


mycount = Range(Myrange).Columns.Count


On Error Resume Next


Columns(myvalue).Insert Shift:=xlToRight


finalrow = Cells(65536, mycolumn).End(xlUp).Row


For i = 1 To finalrow


mystring = (Cells(i, mycolumn))


mylookup = Application.Vlookup((mystring),
Worksheets(Mysheet).Range(Myrange), mycount, False)


Cells(i, myvalue).Value = mylookup


Next i


End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Excel VBA Vlookup

Bernie

Your code runs 40% faster.

Many thanks

Paul



Bernie Deitrick ¼g¹D¡G

There's a few things that you can do to improve your code: use ranges for your input boxes, and
don't loop through the cells - do them all at once using formulas. And never name a macro with a
used function name - that's bad practice.

See the code below for a very flexible VLOOKUP formula creation routine - the lookup values,
results, and data table can be on any sheet in the workbook, anywhere. And it can leave the formula
as a formula....

HTH,
Bernie
MS Excel MVP

Option Explicit

Dim FirstRow As Long
Dim FinalRow As Long
Dim myValues As Range
Dim myRange As Range
Dim myResults As Range
Dim myCount As Integer

Sub VlookupMacro()

Set myValues = Application.InputBox("Please select the first cell in" & _
" the column with the values that you're looking for", Type:=8)
Set myResults = Application.InputBox("Please select the first cell " & _
" where you want your lookup results to start ", Type:=8)
Set myRange = Application.InputBox("Please select the entire lookup data table range" & _
" - with the desired values as the last column", Type:=8)

myCount = myRange.Columns.Count

On Error Resume Next
myResults.EntireColumn.Insert Shift:=xlToRight
Set myResults = myResults.Offset(, -1)
FirstRow = myValues.Row
FinalRow = Cells(65536, myValues.Column).End(xlUp).Row

Range(myResults, myResults.Offset(FinalRow - FirstRow)).Formula = _
"=VLOOKUP(" & Cells(FirstRow, myValues.Column).Address(False, False, , True) & ", " & _
myRange.Address(True, True, , True) & "," & myCount & ", False)"

If MsgBox("Do you want to convert to values?", vbYesNo) = vbNo Then Exit Sub

Columns(myResults.Column).Copy
Columns(myResults.Column).PasteSpecial xlPasteValues
Application.CutCopyMode = False

End Sub





wrote in message
oups.com...
Hello

This is my first Excel VBA to publish in the forum and happy to join
the Excel VBA family.


Excel vlookup function is very useful in many applications.


I develop a VBA to make Vlookup run faster if you havew many rows to
lookup
, please try the code.


I appreciate your comment.


Option Explicit


Dim finalrow As Integer
Dim Mysheet As String
Dim mycolumn As Long
Dim Myrange As String
Dim mycount As Long
Dim i As Integer
Dim mylookup As Variant
Dim mystring As String
Dim myvalue As Long


Sub Vlookup()


mycolumn = Application.InputBox("Please enter the lookup column
number", Type:=1)
myvalue = Application.InputBox("Please enter the column number you want

to place your lookup up result ", Type:=1)
Mysheet = InputBox("Please enter lookup worksheet name",
"WorksheetName", Default)
Myrange = InputBox("Please enter lookup range", "Range", Default)


mycount = Range(Myrange).Columns.Count


On Error Resume Next


Columns(myvalue).Insert Shift:=xlToRight


finalrow = Cells(65536, mycolumn).End(xlUp).Row


For i = 1 To finalrow


mystring = (Cells(i, mycolumn))


mylookup = Application.Vlookup((mystring),
Worksheets(Mysheet).Range(Myrange), mycount, False)


Cells(i, myvalue).Value = mylookup


Next i


End Sub


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Vlookup HELP [email protected] Excel Discussion (Misc queries) 3 September 4th 08 12:30 AM
VLOOKUP Conversion from Excel 2003 to Excel 2007 Michael D. Ober Excel Worksheet Functions 2 November 22nd 07 02:37 PM
Using Vlookup in Excel JimD New Users to Excel 2 December 20th 05 07:58 PM
vlookup?? trying to get Excel to tell me a value Jason Spence Excel Programming 4 July 8th 05 02:51 PM
VBA excel + Forms : Vlookup on database by form / VBA excel +formulaire : rechercheV sur base de données viaformulaire Dick Kusleika[_2_] Excel Programming 0 October 16th 04 02:30 AM


All times are GMT +1. The time now is 02:05 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"