+ Reply to Thread
Results 1 to 9 of 9

Vlookup In Vba

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Vlookup In Vba

    Hi everybody,

    I have the following code which is working perfectly on retrieving data from another sheet for VLOOKUP Wroksheet Function:

    Private Sub Worksheet_Activate()
    
    On Error GoTo MyErr
    
    For C = 2 To 20
    If Cells(C, 1) <> "" Then
    Cells(C, 2) = Application.WorksheetFunction.VLookup(Cells(C, 1), Sheet1.[A2:B20], 2, 0)
    End If
    Next
    Exit Sub
    
    MyErr:
      If Err <> 0 Then
        MsgBox "No Data Available ", vbExclamation, "Missing Data"
      Else
        Resume
      End If
    
    End Sub
    I want to develop this code to retrieve data from another workbook >> I tried the following code but it didn't work well with me >> it always gives me an error .. Can You please help me in this case??

    Private Sub Worksheet_Activate()
    
    On Error GoTo MyErr
    
    For C = 2 To 20
    If Cells(C, 1) <> "" Then
    Cells(C, 2) = Application.WorksheetFunction.VLookup(Cells(C, 1), ActiveWorkbook.Path & "\Test.xls" &  Sheet1.[A2:B20], 2, 0)
    End If
    Next
    Exit Sub
    
    MyErr:
      If Err <> 0 Then
        MsgBox "No Data Available ", vbExclamation, "Missing Data"
      Else
        Resume
      End If
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,080
    Letting us know what the error is would be a start
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  3. #3
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280
    When I activate the concern sheet >> error # 13 is appearing and the type of this error is mismatching >> and when I debug it >> the following line is highlighted

    Cells(C, 2) = Application.WorksheetFunction.VLookup(Cells(C, 1), ActiveWorkbook.Path & "\Test.xls" & Sheet1.[A2:B20], 2, 0)

    I think the problem with the path of the workbook Test >> in other words >> exactly with the following part:

    ActiveWorkbook.Path & "\Test.xls" & Sheet1

    Can you please check if this way of writing the path for a sheet in another workbook is correct or not.

    Thank you again.

  4. #4
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi,

    Could it be that you are only missing the exclamation mark !

    ActiveWorkbook.Path & "\Test.xls!" & Sheet1

    HTH
    Cheers
    Carim

  5. #5
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280
    It didn't work

  6. #6
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    try this:

    =VLOOKUP(B6,[Test.xls]Sheet1!$A$2:$B$20,2,False)
    Regards,
    Simon

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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