I'm currently designing a macro that auto-populates a table in Excel from a drop-down list. Basically, in one sheet, there is a table of 200 companies. This has a field named Manager (for people looking after the company) which contains a drop-down list with all 12 possible managers' names. When I select a manager, it populates the company name to their section of the table.
Only problem is that I'm trying to use a VLookup in VBA to retrieve the company name. So for example, if I select a manager from row 123, I want to assign the company name in column 'C' to the companyName variable (which then gets inserted into the auto-populating table.
Here is my VLookup code so far:
companyName = Application.WorksheetFunction.VLookup(ActiveCell.Address, Sheets("Companies").Range("C8:K208"), 1, False)
I've seen a number of different ways to do VLookups and generally I find them quite confusing. Whenever it tries to do the lookup it just says "Run-time error 1004: Unable to get the VLookup property of the WorksheetFunction class".
Any help on what's going wrong here would be much appreciated. It's been about 6 years since I last looked at Visual Basic in any sort, and I haven't been working with Excel macros that long - so sorry if I'm missing something blindly obvious!
ActiveCell
approach is never recommended.. it fraught with danger. Can you share either the full code or relevant code? I suspect thatActiveCell.Address
might either have an invalid value or empty – Zac