+ Reply to Thread
Results 1 to 14 of 14

Using vlookup in VBA based on autofilter criteria

  1. #1
    Registered User
    Join Date
    04-02-2018
    Location
    Perth, Australia
    MS-Off Ver
    2016
    Posts
    11

    Unhappy Using vlookup in VBA based on autofilter criteria

    For a work project, I need to output a certain value from a table - based on the criteria of one particular field in an autofilter. That value then needs to fill a particular cell. Also when the autofilter is only filtering blanks out then an "All" value needs to be outputted.

    Because I know that there will not be any more criteria than there already are in that field, I can make a static VBA macro to do the job (which can be called upon as I need to). For proprietary reasons, I have constructed a similar sheet with:

    Worksheet name - Outputs - Autofilter Worksheet name - Outputs - Output of Vlookup, Button 1 for Calling Subroutine Worksheet name - Inputs - Table of Value
    rnGPwjF.png usLOyUm.png VPhk0Me.png

    I have the following formula working, tried it to make sure my input arguments were correct:
    Please Login or Register  to view this content.
    Rationale being that if I compare the number of visible cells in range to the number of visible cells with a certain criteria, I could determine the value of the autofilter and use vlookup as required. If I just look for the number of visible cells with a certain criteria being >0 then the case when "All" needs to be looked up, it wouldn't work.

    This works for "A", "B", "C" ... "I". But I cannot get it to translate to the macro domain.

    Macro so far:
    Please Login or Register  to view this content.
    Error: (At the start of first IF)
    Q3iuSo5.png

    I know that I should probably make a loop to reduce the code but that's step #2.
    Thoughts??
    Attached Images Attached Images
    Last edited by ruhalt; 04-02-2018 at 08:53 AM. Reason: Updating code as per improvements in this post. Contributer(s) = CK76

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Using vlookup in VBA based on autofilter criteria

    You don't have enough argument in your subtotal.
    Please Login or Register  to view this content.
    You need to set calculation type.

    Ex:
    Please Login or Register  to view this content.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    04-02-2018
    Location
    Perth, Australia
    MS-Off Ver
    2016
    Posts
    11

    Re: Using vlookup in VBA based on autofilter criteria

    Nice spotting (duh! - how stupid of me).

    However I get a different error now -
    Q3iuSo5.png

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Using vlookup in VBA based on autofilter criteria

    At which line do you get that error.

    That error often happens, when you are trying to assign incompatible data type to variable.

    It would be easier to help, if you can upload sample workbook with enough data and manually inputted expected result.

    To upload workbook, find "Go Advanced" button in quick reply menu and click. Find "Manage Attachment" hyperlink.
    Click on it and it will launch another tab/window for managing file upload.

  5. #5
    Registered User
    Join Date
    04-02-2018
    Location
    Perth, Australia
    MS-Off Ver
    2016
    Posts
    11

    Re: Using vlookup in VBA based on autofilter criteria

    The first If statement is where the error is occuring.

    The paperclip option isn't giving me anything in the dropdown menu, cannot close the "managed attachments" tab either. And I guess I haven't posted enough to attach a link to it.

    The images in the Original Post cover all the used cells though.

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Using vlookup in VBA based on autofilter criteria

    Hmm, I suspect below part to be the culprit.
    Please Login or Register  to view this content.
    What are you trying to do in this portion? Rows should have number as it's argument, not string.

    As well, rows will return range object, and you cannot subtract range object.

  7. #7
    Registered User
    Join Date
    04-02-2018
    Location
    Perth, Australia
    MS-Off Ver
    2016
    Posts
    11

    Re: Using vlookup in VBA based on autofilter criteria

    I think I was meant to write:
    Please Login or Register  to view this content.
    instead of
    Please Login or Register  to view this content.
    Sorry. But still same error
    Last edited by ruhalt; 04-02-2018 at 08:53 AM.

  8. #8
    Registered User
    Join Date
    04-02-2018
    Location
    Perth, Australia
    MS-Off Ver
    2016
    Posts
    11

    Re: Using vlookup in VBA based on autofilter criteria

    Please Login or Register  to view this content.
    This is meant to create an array such as:
    {0, 1, 2, 3, ..., n}

    Which when used with the Offset function with A2 as reference, return {"[A2.Value]", "[A3.Value]", "[A4.Value]",...}.
    Then the subtotal with code "3" is meant to apply a 1 for visible and 0 for non-visible cells.
    Therefore you get the classic -
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    04-02-2018
    Location
    Perth, Australia
    MS-Off Ver
    2016
    Posts
    11

    Re: Using vlookup in VBA based on autofilter criteria

    Also tried:
    Please Login or Register  to view this content.
    But at this point I'm clutching at straws

  10. #10
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Using vlookup in VBA based on autofilter criteria

    Hmm, from what I understand, you are just checking if "A" to "F" if it's the only visible cell for the range?

    Likely issue is the difference in how range is treated in Worksheet function and VBA.

    You could try using Evaluate instead.
    Ex:
    Please Login or Register  to view this content.
    Though there are more efficient methods than above.

    Such as using .SpecialCells(xlCellTypeVisible) to check visible cells in range. Though I'm still unclear on what exactly you are after as end result.

  11. #11
    Registered User
    Join Date
    04-02-2018
    Location
    Perth, Australia
    MS-Off Ver
    2016
    Posts
    11

    Re: Using vlookup in VBA based on autofilter criteria

    Before I try out your method, I'll try to clarify what I'm trying to achieve.

    I need to know the filter criteria of the first field in the autofilter in the "Outputs" Sheet. Based on the filter criteria, I wish to use vlookup to find a certain value attached to it. If there was an easy way to output the filter criteria, that would be quickest but it seems a comparison such as between the count of all visible cells in the filter range and the count of all visible cells (obeying the filtered criteria) would give you an indication of the filter criteria used.

    An example would be:
    You are arranging boxes on the ground and each box type has a different footprint (area in sq. something). So the program will allow you to filter box types and some other property like colour (indicated by criteria field 2) and then based on the choice of filter, you can find out the area that box type occupies.

  12. #12
    Registered User
    Join Date
    04-02-2018
    Location
    Perth, Australia
    MS-Off Ver
    2016
    Posts
    11

    Re: Using vlookup in VBA based on autofilter criteria

    But as long as it is possible to macro-tise the really long IFS code as follows, then we're home (as it works):
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    04-02-2018
    Location
    Perth, Australia
    MS-Off Ver
    2016
    Posts
    11

    Re: Using vlookup in VBA based on autofilter criteria

    By the way, your code works like a charm. Thanks for all the help.

    Any ideas on how to add more levels of efficiency to it?

  14. #14
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Using vlookup in VBA based on autofilter criteria

    FYI, you can use something like below to get filter criteria.
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Delete Row Based on Criteria s using Autofilter
    By Biplab1985 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-03-2018, 11:48 AM
  2. VBA to autofilter data based on set criteria into a new sheet
    By indik in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-24-2015, 04:54 PM
  3. [SOLVED] Set autofilter criteria based on changing array
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-15-2015, 11:16 AM
  4. [SOLVED] AutoFilter Criteria Based on Variable Row
    By clattenburg cake in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-14-2013, 08:58 PM
  5. Hide and Autofilter Macro based on Criteria
    By dawnmau in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 09-22-2010, 05:45 PM
  6. AutoFilter based on 2 Fields and 2 Criteria?
    By DISMfish in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-16-2006, 10:15 AM
  7. [SOLVED] Autofilter criteria based on cell value
    By Craig in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-30-2005, 12:05 PM

Tags for this Thread

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