Frage

I'm constantly having to use vlookup to find company names associated with a code. I already have an excel sheet containing the codes and corresponding company names. In order to ease my task I'm trying to create a function i which i only need to indicate the code, and in turn returns the company names.

If possible i would like to have the table in PERSONAL.XLSB sheet1!, since i already have some macros in there. Column A has the company codes, column B the company name.

I'm trying to use the following, but i keep getting nowhere since i can't seem to define the range properly

Function Company_name(Company_Code As Integer)

Company_name = Application.WorksheetFunction.VLookup(Company_Code, [PERSONAL.XLSB]Sheet1!$A:$B, 2, False)

End Function

The way i want it to work is, I select Company_Code = 100, and the function returns company name "Asus". Company_Code = 200 it should return company name "Acer", and so on.

My problem is that i have not been able to make the code work.

Can anyone help me?

Thanks in advance

War es hilfreich?

Lösung

Something like this:

Function Company_name(Company_Code As Integer)
    Dim rv
    rv = Application.VLookup(Company_Code, _
        Workbooks("Personal.xlsb").Sheets("Sheet1").Range("A:B"), 2, False)

    If IsError(rv) Then rv = "???"
    Company_name = rv

End Function
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top