+ Reply to Thread
Results 1 to 20 of 20

Combining 3 VBA functions into a single VBA sub

  1. #1
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Combining 3 VBA functions into a single VBA sub

    Hi,

    I have a workbook that uses 3 separate functions to create a fairly complex calculation. When the developer created this workbook for me more than a dozen years ago, I didn't know to tell him that I wanted a single VBA sub and not 3 functions to perform the calculation. The current 3 VBA function approach works but I want to simplify. The largest of the VBA functions has about 125 lines of working code; the other two have only 20 to 30 lines of code.

    Now, years later, I have a use for the calculation that the VBA functions perform but don't have enough VBA skills to modify the code. If someone in the Forum wants to tackle this, I will post the workbook.

    Thanks

    Art

  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,425

    Re: Combining 3 VBA functions into a single VBA sub

    Post the workbook, or a redacted copy, to the forum.


    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.
    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
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Re: Combining 3 VBA functions into a single VBA sub

    I didn't receive a notification by email that you responded to my post; always did in the past. My email hasn't changed, thanks.

    Currently, my workbook has about 200 datapoints; that apparently exceeds the posting guidelines. Also, don't know what you mean by a mocked up solution. This so much code, that I couldn't even begin to mock up a solution.

    I can append the data series but this will take some time; should I post as is?

  4. #4
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Re: Combining 3 VBA functions into a single VBA sub

    oops, meant to say truncate the data series. I am old and it's late here.

  5. #5
    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,425

    Re: Combining 3 VBA functions into a single VBA sub

    If the data is not personal, private or sensitive, post it as it is. The guideline is to stop people posting huge files with thousands of rows.

  6. #6
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Re: Combining 3 VBA functions into a single VBA sub

    The 3 VBA functions that I want to combine into a single VBA sub are in Module 1 and are linked to the radiobutton. The code in Module 2 creates an EMA for demo purpose. I don't care about that. I did my best to remove unneeded declarations code that didn't impact the Jurik Filter code.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Re: Combining 3 VBA functions into a single VBA sub

    Hi TMS, are you making any progress?

  8. #8
    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,425

    Re: Combining 3 VBA functions into a single VBA sub

    I had not seen your reply with the file. Having looked at it, I would not know where to start. Unfortunately, I don't have time to work out what each of the functions does and how it does it, let alone combine them.

    I think we'll need to see if someone else can jump in.

  9. #9
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Re: Combining 3 VBA functions into a single VBA sub

    I'm amenable to letting someone else jump in and you said you are. Hopefully we'll get a taker. Thanks, Art

  10. #10
    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,425

    Re: Combining 3 VBA functions into a single VBA sub


  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Combining 3 VBA functions into a single VBA sub

    Any reason why you want to move the 2 functions in to the main subroutine?

    Please Login or Register  to view this content.
    I have added a prefix to some of the variable names to avoid potential conflicts with existing variables.
    This seems to generate the same values.
    Cheers
    Andy
    www.andypope.info

  12. #12
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Re: Combining 3 VBA functions into a single VBA sub

    Hi Andy
    Thanks for taking this on! Why combine the 3 into a single code? I prefer consolidated code. There's still one more part which is to convert this consolidated function code into a VBA sub. I've never worked with functions but have worked with subroutines.

    I did today load your code into a new worksheet. It did compile and stopped at:
    Please Login or Register  to view this content.
    The error stated that the sub or function was not defined.

    Do you have an idea what might be wrong?
    Thanks,
    Art

  13. #13
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Re: Combining 3 VBA functions into a single VBA sub

    Oops should have said: It did not compile and stopped at:

  14. #14
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Re: Combining 3 VBA functions into a single VBA sub

    Hi Andy just tried commenting out that Debug line. The code compiles and runs fine! Many thanks! Can you easily convert this to a VBA sub? Thanks.

  15. #15
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Re: Combining 3 VBA functions into a single VBA sub

    To convert your Function code to Sub code, thought that it might be as simple replacing the label Function with Sub. Apparently it's not quite that easy- received multiple compile errors. What needs to be changed in the Function code to create Sub code? Thanks.

  16. #16
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Combining 3 VBA functions into a single VBA sub

    If you just copied that code in to a new module you will need to remove the Debug,Print line that was still using the old functions in order to confirm results where the same.

    Some time it can be as simple as changing the Sub/Function definition.
    In your case the main routine JurikFilter can be as it returns no value. The other two functions which I incorporated into the main code did return a value.

    Functions and subs are the same except the function can return a value directly. Hence the use of

    variable = myfunction

    A sub routine can only return values via the variables passed in to it, if they are passed byref.

  17. #17
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Re: Combining 3 VBA functions into a single VBA sub

    Hi Andy,
    Thanks for yor thoughts and comments. I see that converting this function to a sub involves setting up the input parameters and input data range within the sub.

    What is confusing too is that the button click sub passes the input information to the function. Usually a button click sub simply calls the main sub which contains the input variable and range assignmets.

    Please Login or Register  to view this content.
    I think that the function has the output cell assignment mapped with the code:
    Please Login or Register  to view this content.
    I think that if I can move the input variables and input data range from the button click sub to the main code, the sub should work. Do you have a suggestion for this? I am going to try out a few ideas.

    Thanks,

    Art

  18. #18
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Combining 3 VBA functions into a single VBA sub

    If you move the variables to within the single routine it would be something like this at the start.

    Please Login or Register  to view this content.

  19. #19
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Re: Combining 3 VBA functions into a single VBA sub

    Hi Andy, Thanks. I did all those things and got the sub working but somehow 'misplaced' the completed file. :-( Before I dig into reconstructing the sub. I will look around more carefully. Again, thanks!

  20. #20
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Re: Combining 3 VBA functions into a single VBA sub

    Hi Andy, could not find the file but managed to cobble together the pieces that you posted to create a working Jurik file. I'll post it here so that it doesn't get lost again! Thanks again! -Art
    Attached Files Attached Files

+ 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. Combining 'VLookUp' & 'CountA' Functions In A Single Formula
    By Ron Grehl in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 03-16-2022, 06:17 PM
  2. Replies: 1
    Last Post: 01-29-2020, 05:19 PM
  3. [SOLVED] IF AND OR functions combining
    By Alphabex in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-23-2016, 06:43 PM
  4. Combining IF and OR functions
    By H28Sailor in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 08-29-2015, 04:54 AM
  5. Combining multiple functions>lookup/sum functions
    By mush106 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-22-2013, 07:47 AM
  6. Combining functions
    By rangerel in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 12-01-2010, 07:43 AM
  7. Excel 2007 : Combining IF Functions together....
    By qhx398 in forum Excel General
    Replies: 6
    Last Post: 12-11-2009, 03:35 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