Convert Vlookup Formula to VBA Code and Select the Most Current Date from Slicer

  • I have vlookup formula that looks up the value in D8, active field name ‘ProductID ‘ in the pivot table named ‘pvt_Production_Summary’ and the returned value divides the value in E8 active field ‘Sum of Cases’. I'm working on the below code convert this formula into VBA. However, I keep getting a Run-Time error message ‘1004’. Application-defined or object-defined error. on ‘For Each si In sl.SlicerItems’


    I know that the SlicerCaches ‘Slicer_ProductionDate’ is associated to the pivot table ‘pvt_Production_Summary. I would greatly appreciate your time and help.

  • Hi and Welcome to the Forum :)


    Your question is not extremely clear :


    1. If you need to update Slicers with a Macro :

    Update Excel Slicers with Macro
    www.contextures.com


    2. If you need to convert a worksheet formula into VBA, you should consider Evaluate

    VBA Evaluate Function - Automate Excel
    In this ArticleEvaluate SyntaxReturn a Result to Excel with EvaluateFormat a Cell with EvaluateShorten Formulas with the Evaluate Function This article will…
    www.automateexcel.com


    Hope this will help :)

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Carim,

    Thank you for looking into this thread. As you can see from the image below, I am using a vlookup to search for product ID in the tbl_Cases_Per_Pallet, and the returned value divides the value in E8 active field ‘Sum of Cases’. One instead of using the vlookup function, I want to use the above VBA code to accomplish this task as well as select the most current date from the slicer, which in this case would be 4/27/23. I would prefer that the VBA lookup code to return the corresponding value when the pivot table is refreshed. I would greatly appreciate your help and time.

  • Hello,


    Without a sample file, it is quite difficult to guess all your constraints and how the Pivot Table operates based on which data ...


    As a general answer, for a simple and easy workaround, you can add a Column to your database which provides the foundation for your Pivot Table ...


    Hope this will help :)

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!