+ Reply to Thread
Results 1 to 5 of 5

Create a custom SUM function with VBA

  1. #1
    Registered User
    Join Date
    11-13-2020
    Location
    Portugal
    MS-Off Ver
    2019
    Posts
    2

    Question Create a custom SUM function with VBA

    Hi there,

    I have a table called ("Vendas2020") that has the daily sales and each row has either "C" or "V" if they're either a purchase or a sale (C/V column is the fifth one and is called "Compra / Venda"). I have inserted a function to only calculate the visible values in the table since the destination worksheet (which is a report called "Relatório") has slicers for the products, salesperson and year. The code for the sumfunction is as follows:

    Please Login or Register  to view this content.
    I'd like to add an if statement (or something like it) that only sums the values if they're purchases ("V") but I can't seem to make it work. It either throws a value error or a spill error. I can't seem to find a thread that has a solution that might apply in this case. I know it's something simple but I'm somewhat new to VBA.
    Thanks in advance!

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Create a custom SUM function with VBA

    Why are you using a volatile VBA function? You could just use SUBTOTAL. For example,
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    11-13-2020
    Location
    Portugal
    MS-Off Ver
    2019
    Posts
    2

    Re: Create a custom SUM function with VBA

    I'm trying not to use a SUBTOTAL because the function must be flexible to assume certain filters as fixed and others that vary from column to column. Since the same function will be used in different column, SUBTOTAL can only sum the visible cells and not select them. What I need is to add to my code a line that selects values based on different criteria (for example, only select "V").

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Create a custom SUM function with VBA

    Please post a sample workbook. I don't understand why SUBTOTAL won't work for what you are describing.

  5. #5
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    273

    Re: Create a custom SUM function with VBA

    Try this for your function:

    Please Login or Register  to view this content.
    Last edited by achammar; 11-14-2020 at 06:56 PM.

+ 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. [SOLVED] Create custom function which contains other functions
    By okela in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-14-2020, 10:11 AM
  2. Custom Function to Create and Assign Button
    By dkang2007 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-30-2014, 02:44 PM
  3. How to create a custom function that creates a list like this {1,2,3,4,5,6,7,...}
    By keenlam in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-14-2012, 02:47 AM
  4. How to create a custom function
    By Marcie in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-02-2006, 12:10 AM
  5. How to create a simple VBA Custom Function
    By Edmund in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-22-2006, 03:55 PM
  6. Create custom function
    By andyiain in forum Excel General
    Replies: 3
    Last Post: 03-31-2006, 11:33 AM
  7. Create Custom Sum Function
    By L2B in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-09-2005, 09:29 AM

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