Results 1 to 8 of 8

VBA Vlookup function returns wrong values

Threaded View

alicelia VBA Vlookup function returns... 05-28-2015, 04:47 AM
michson Re: VBA Vlookup function... 05-28-2015, 05:02 AM
alicelia Re: VBA Vlookup function... 05-28-2015, 05:27 AM
michson Re: VBA Vlookup function... 05-28-2015, 05:47 AM
alicelia Re: VBA Vlookup function... 05-28-2015, 05:57 AM
michson Re: VBA Vlookup function... 05-28-2015, 06:08 AM
alicelia Re: VBA Vlookup function... 05-28-2015, 06:16 AM
alicelia Re: VBA Vlookup function... 05-29-2015, 12:09 AM
  1. #1
    Registered User
    Join Date
    05-20-2015
    Location
    HK
    MS-Off Ver
    2010
    Posts
    9

    VBA Vlookup function returns wrong values

    Hi I am using vlookup function in VBA codes to import data from one excel file to another. The source file opens successfully. The code runs with no error messages. But the imported value are not correct.

    The expected values imported are attendance percentages from the source file. However, the imported value are all zeros only. I have highlighted what I think is the problematic code in red.

    The table array in the source file is A16:I55 but it can be shorter as number of students varies so it could be A16:I50. I set it as I55 so it will be sure to include everyone as the length of the table will not exceed I55. The lookup value is B4 which is the student name. Since i have to look up many students, I have set up the dim i as integer function to loop the Vlookup function. Could you please suggest what could be wrong with the highlighted code below so that it caused the aforementioned problem?

    Thank you very much!

    Sub button()
    
    ' Get customer workbook...
    Dim customerBook As Workbook
    Dim filter As String
    Dim caption As String
    Dim customerFilename As String
    Dim customerWorkbook As Workbook
    Dim targetWorkbook As Workbook
    
    Set targetWorkbook = Application.ActiveWorkbook
    
    ' get the customer workbook
    filter = "Text files (*.xlsx),*.xlsx"
    caption = "Please Select an input file "
    customerFilename = Application.GetOpenFilename(filter, , caption)
    
    On Error Resume Next
    
    Set customerWorkbook = Application.Workbooks.Open(customerFilename, UpdateLinks:=False, ReadOnly:=True)
    
    
    ' copy data from customer to target workbook
    Dim targetSheet As Worksheet
    Set targetSheet = targetWorkbook.Worksheets(1)
    Dim sourceSheet As Worksheet
    Set sourceSheet = customerWorkbook.Worksheets(1)
    
    
    Dim i As Integer
    For i = 4 To 43
       targetSheet.Cells(i, 11).Value = "=VLOOKUP(targetSheet.cells(i, 2),sourceSheet!R16C1:R55C9, 9, False)"
    Next i
    
    
    
    customerWorkbook.Close SaveChanges:=False
    
    
    End Sub
    Last edited by alicelia; 05-28-2015 at 05:20 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] My Vlookup returns the wrong info
    By dstrdOne in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-16-2013, 01:42 PM
  2. VLookup returns N/A or wrong value
    By vindieselgal in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 08-08-2011, 07:20 AM
  3. VLOOKUP returns wrong values
    By surfol in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-26-2011, 03:26 PM
  4. Row() function returns wrong row and more..
    By Kim in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-10-2006, 09:14 PM
  5. [SOLVED] My Datedif function only returns 0's in the cell what's wrong?
    By Tom in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-13-2006, 02:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1