Solved

Vlookup not Inserting Correctly

Posted on 2014-01-29
3
192 Views
Last Modified: 2014-02-03
I having problems with this formula.  It does not enter correctly in C3.  I either does not copy down to the next row correctly or it adds B2 reference not B3.  

Sheet1.Range("C3:C" & Lastrow).FormulaR1C1 = "=(VLOOKUP(B3,'Vendor'!$E$2:G9,2,FALSE))"
_____________________________________________________________

Sub InsertVS()
    Dim Lastrow As Long

    Application.ScreenUpdating = False
   

    Sheet27.Range("C:D").EntireColumn.Insert
     
    Lastrow = Sheet27.Cells(Sheet27.Rows.Count, "B").End(xlUp).Row
 Sheet27.Range("C3").Select
    Sheet27.Range("C3:C" & Lastrow).FormulaR1C1 = "=(VLOOKUP(B3,'Vendor'!$E$2:G497,2,FALSE))"
     
    Sheet27.Range("D3:D" & Lastrow).FormulaR1C1 = "=VLOOKUP(B3,'Vendor'!$E$2:H497,3,FALSE)"
 
 
 Sheet27.Columns("C:D").Select
        Selection.Replace What:="=", Replacement:="=", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
       
   
       
    Application.ScreenUpdating = True
   
 
 
End Sub
vlookup.zip
0
Comment
Question by:leezac
3 Comments
 
LVL 22

Expert Comment

by:Dreamboat
ID: 39819431
Sheet1.Range("C3:C" & Lastrow).Formula = "=(VLOOKUP(B3,Vendor!$E$2:$G$9,2,FALSE))"
   
    Sheet1.Range("D3:D" & Lastrow).Formula = "=VLOOKUP(B3,Vendor!$E$2:$H$9,3,FALSE)"
 

(Remove "R1C1" and put $ on your entire lookup range. Not sure which one did it, prolly R1C1)
0
 

Author Comment

by:leezac
ID: 39819454
Ok - I changed, but put B3 in C3 and C4.  Need for it to copy down B3, B4 B5 etc.
0
 
LVL 31

Accepted Solution

by:
Rob Henson earned 500 total points
ID: 39820308
In your script you are effectively telling the formula to be string of text which Excel then recognises as a formula. If you rewrite the formula to R1C1 notation, even as a string of text excel will then evaluate correctly.

Formula for C3 would be:

=VLOOKUP(RC[-1],Vendor!R2C5:R9C7,2,FALSE)

Formula for D3 would be:

=VLOOKUP(RC[-2],Vendor!R2C5:R9C7,3,FALSE)

So scripts would be:

Sheet1.Range("C3:C" & Lastrow).Formula ="=VLOOKUP(RC[-1],Vendor!R2C5:R9C7,2,FALSE)"
Sheet1.Range("D3:D" & Lastrow).Formula ="=VLOOKUP(RC[-2],Vendor!R2C5:R9C7,3,FALSE)"

Open in new window


Thanks
Rob H
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now