Simple Vlookup not working within VBA code

heathball

Board Regular
Joined
Apr 6, 2017
Messages
133
Office Version
  1. 365
Platform
  1. Windows
Im trying to find out why this is not working, and google is not having a good day.

i have a simple vba code, without any complications, and i"m just wondering what I'm missing.

Im in cell "O2" and i just cannot get this up and running......if anyone knows why i would really appreciate it.



Range("O2") = Application.WorksheetFunction.VLookup(N2,'month-year look list'!A:B,2,false)
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try:

Range("O2") = Application.WorksheetFunction.VLookup(Range("N2"), Sheets("month-year look list").Range("A:B"), 2, False)

or

Range("O2") = Evaluate("VLookup(N2,'month-year look list'!A:B,2,false)")


If you use the WorksheetFunction version, you need to give it the parameters in the VBA format. If you use Evaluate, you can just give it the same formula you'd use on the sheet. Both ways have their place.
 
Last edited:
Upvote 0
Im doing this

Range("O2").Select
Range("O2") = Application.WorksheetFunction.VLookup(Range("N2"), Sheets("month-year look list").Range("A:B"), 2, False)
ActiveCell.AutoFill Range(ActiveCell, ActiveCell.Offset(0, -1).End(xlDown).Offset(0, 1))


its producing the correct result in cell O2, but it seems to be pasting the value, not leaving the formula in the cell, so when the autofill operates, its pasting the value in O2-- in every cell down the page?

do you know why that would be occurring?
 
Upvote 0
Well, yes, I thought you just wanted the value inserted. If you want to actually insert a formula, you need to use the .Formula method. Try this:

Code:
Range(Range("O2"), Range("N2").End(xlDown).Offset(, 1)).Formula = "=VLOOKUP(N2,'month-year look list'!A:B,2,FALSE)"

This handles all 3 lines of your last post.
 
Upvote 0
this is producing the actual formula in text - each cell down the page is looking like this =VLOOKUP(N2,'month-year look list'!A:B,2,FALSE)

how do i get the cell to act like a normal Vlookup does, with the result showing in the cell, but the cell containing the formula?
 
Upvote 0
Sounds like you have column O formatted as Text. Change it to General, delete everything in it, then run the macro again.
 
Upvote 0
ahh thanks very much

interesting what you did by adding the End(xlDown).Offset(, 1)) within the formula. i will check this out

thanks for your time!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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