Vlook up function in Macro

asyamonique

Well-known Member
Joined
Jan 29, 2008
Messages
1,186
Office Version
  1. 2013
Platform
  1. Windows
Code:
Dim ws As Worksheet, rngFind As Range
Set ws = ThisWorkbook.Sheets("0600")
Set rngFind = ws.Range("b:b").Find(what:=Sheets("Result").Range("A5").Value, MatchCase:=True)
If Not rngFind Is Nothing Then
Sheets("Result").Range("b5").Value = rngFind.Offset(0, 1).Value
Sheets("Result").Range("c5").Value = rngFind.Offset(0, 2).Value
End If

Good Day,
Im trying to change the above code as given code below but im not good enough to make it!

Code:
Dim ws As Worksheet, rngFind As Range
Set ws = ThisWorkbook.Sheets("0600")
Set rngFind = ws.Range("b:b").Find(what:=Sheets("Result").Range("A5:A24").Value, MatchCase:=True)
If Not rngFind Is Nothing Then
Sheets("Result").Range("b5:d24").Value = rngFind.Offset(0, 3).Value
Sheets("Result").Range("c5:e24").Value = rngFind.Offset(0, 4).Value
End If

I just wanted make it easier due to instead of writing them all one by one!
Could somebody help me on this problem?
Many Thanks
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

BobUmlas

Well-known Member
Joined
Mar 14, 2002
Messages
1,181
Range("A5:A24").Value doesn't make sense -- try entering =A5:A24 in a cell -- you'll either get #VALUE! or the intersection of A5:A24 and the cell you enter it into (if you enter =A4:A24 in cell F22, you'll see the value of A22 there.)
You don't have to do this one by 1, you could put it in a loop, something like:

Sub try()
Dim ws As Worksheet, rngFind As Range
Set ws = ThisWorkbook.Sheets("0600")
For i = 5 To 24
Set rngFind = ws.Range("B:B").Find(what:=Sheets("Result").Range("B" & i).Value, MatchCase:=True)
If Not rngFind Is Nothing Then
Sheets("Result").Range("B" & i).Value = rngFind.Offset(0, 1).Value
Sheets("Result").Range("C" & i).Value = rngFind.Offset(0, 2).Value
End If
Next
End Sub
 

asyamonique

Well-known Member
Joined
Jan 29, 2008
Messages
1,186
Office Version
  1. 2013
Platform
  1. Windows
Range("A5:A24").Value doesn't make sense -- try entering =A5:A24 in a cell -- you'll either get #VALUE! or the intersection of A5:A24 and the cell you enter it into (if you enter =A4:A24 in cell F22, you'll see the value of A22 there.)
You don't have to do this one by 1, you could put it in a loop, something like:

Sub try()
Dim ws As Worksheet, rngFind As Range
Set ws = ThisWorkbook.Sheets("0600")
For i = 5 To 24
Set rngFind = ws.Range("B:B").Find(what:=Sheets("Result").Range("B" & i).Value, MatchCase:=True)
If Not rngFind Is Nothing Then
Sheets("Result").Range("B" & i).Value = rngFind.Offset(0, 1).Value
Sheets("Result").Range("C" & i).Value = rngFind.Offset(0, 2).Value
End If
Next
End Sub

Bob,
How can i add a code to get the sum values of cells 5/24 into the cell 25?
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,879
Messages
5,834,215
Members
430,263
Latest member
abz54

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
Top