Share via

Error in argument of Application.WorksheetFunction.Vlookup

Anonymous
2015-05-09T06:21:44+00:00

Error occur in below VBA line of 'Compile Error: Expected: expression'. Kindly solve where's wrong in line to lookup value from another sheet matched with textbox5

TextBox6.Text = application.WorksheetFunction.VLookup(textbox5.Text,('D:\FolderName[FileName.xlsb]Sheet1'!A2:B12100),2,FALSE)

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

OssieMac 48,001 Reputation points Volunteer Moderator
2015-05-12T07:56:08+00:00

My apologies for not getting back to you sooner but I have been away for a few days.

I assuming that if the text boxes are on a Userform then your code is in the Userform code module or if the text boxes are on a worksheet then the code is in the worksheets module. If not one of these then I many need to edit the code for you.

AFAIK you cannot reference a closed workbook with worksheet functions in VBA so the workbook needs to be opened. The code first tests if the workbook is open and if not, then opens it.

When using a worksheet function in VBA, the references are set up as in VBA; not as a reference as used on a worksheet with the function. However, it is possible to insert a worksheet function on the worksheet so it is like as if it was entered directly on the worksheet but the following code does not do that.

    Dim wBook As Workbook

    Dim wShtTableArray As Worksheet

    Dim rngTableArray As Range

    On Error Resume Next

    'Test if file already open

    Set wBook = Workbooks("FileNameSource.xlsb")

    On Error GoTo 0

    If wBook Is Nothing Then    'If nothing then not open so open the workbook.

        Set wBook = Workbooks.Open(Filename:="D:\FolderName\FileName.xlsb")

    End If

    Set wShtTableArray = wBook.Worksheets("Sheet1")

    With wShtTableArray

        Set rngTableArray = .Range(.Cells(2, "A"), .Cells(12100, "B"))

    End With

    Me.TextBox6.Text = Application.WorksheetFunction.VLookup(Me.TextBox5, rngTableArray, 2, False)

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2015-05-09T11:07:27+00:00

    Sorry, it was problem of paste data, real formula is mention below which occur error.

    TextBox6.Text = application.WorksheetFunction.VLookup(textbox5.Text,('D:\FolderName[FileName.xlsb]Sheet1'!A2:B12100),2,FALSE)

    Was this answer helpful?

    0 comments No comments
  2. OssieMac 48,001 Reputation points Volunteer Moderator
    2015-05-09T08:34:20+00:00

    Your example formula is missing some of the formula. See Help for Vlookup to get all of the arguments (parameters) for Vlookup.

    Was this answer helpful?

    0 comments No comments