How to use vlookup properly in VBA

gorgon777

Board Regular
Joined
Mar 12, 2012
Messages
58
Hi,

I have the below code for a Loop
Code:
Do While strCurrentFile <> ""
    If strCurrentFile <> "No_PO_Expense.xlsm" Then
        'Create Excel file in memory
        Dim XL As Excel.Application
        Dim WBK As Excel.Workbook
        Dim result As String
        Dim a As String
        Dim myRange As Range
        Set XL = CreateObject("Excel.Application")
        Set WBK = XL.Workbooks.Open(filePath & strCurrentFile)
        a = WBK.Sheets("Expenses").Range("M2")
        Set myRange = ThisWorkbook.Sheets("TowerList").Range("A2:A61")
        'MsgBox WBK.Sheets("Expenses").Range("A2").value
        result = WBK.Application.WorksheetFunction.VLookup([a], [myRange], 4, False)
        'folderName = WBK.Sheets("Expenses").Range("M2") & " " & WBK.Sheets("Expenses").Range("O2")
        folderName = WBK.Sheets("Expenses").Range("M2") & " " & result
            With CreateObject("Scripting.FileSystemObject")
                If Not .FolderExists(filePath & folderName) Then .CreateFolder (filePath & folderName)
            End With
        DoEvents
        WBK.Application.Quit
        XL.Application.Quit
        DoEvents
        FileCopy filePath & strCurrentFile, filePath & folderName & "\" & strCurrentFile
        strCurrentFile = Dir
    End If
Loop

a - this is a String text to search for.
myRange - this is the range I am looking in.

However, I get an error saying "Unable to get the Vlookup property of the Worksheet function class" on the line where the Vlookup occurs. Have i done something wrong with the syntax?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Dim Result as Variant instead of String and remove Worksheets as JS411 suggested.

Oh, and I really don't think there's a need to create a new instance of Excel for this.

Just open the file in the current instance with Workbooks.Open.
 
Upvote 0

Forum statistics

Threads
1,216,750
Messages
6,132,499
Members
449,730
Latest member
SeanHT

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