+ Reply to Thread
Results 1 to 12 of 12

Adding an additional parameter to a VBA existing code

  1. #1
    Registered User
    Join Date
    09-13-2018
    Location
    houston
    MS-Off Ver
    2010
    Posts
    7

    Adding an additional parameter to a VBA existing code

    I have used the following code and it works perfectly:

    Function GetColorCount(CountRange As Range, CountColor As Range)
    Dim CountColorValue As Integer
    Dim TotalCount As Integer
    CountColorValue = CountColor.Interior.ColorIndex
    Set rCell = CountRange
    For Each rCell In CountRange
    If rCell.Interior.ColorIndex = CountColorValue Then
    TotalCount = TotalCount + 1
    End If
    Next rCell
    GetColorCount = TotalCount
    End Function

    But, now I want to do a little more. For the range of cells, I want to have 4 separate user defined functions.
    1) Count cells IF they are a particular color, as well as the value inside ending in "*o"
    2) Count cells IF they are a particular color, as well as the value inside ending in "*s"
    3) Count cells IF they are a particular color, as well as the value in the cell having 2 text characters in the cell, "??"
    4) Count cells IF they are a particular color, as well as the value in the cell being a number greater than zero, ">0".
    How can I modify the base "GetColorCount" code to incorporate this additional parameter for each instance?

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Adding an additional parameter to a VBA existing code

    Quote Originally Posted by gregpaj View Post
    3) Count cells IF they are a particular color, as well as the value in the cell having 2 text characters in the cell, "??"
    When you say two text characters, do you mean text is only alpha-characters A-Z or can it include numeric characters? Give examples.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Adding an additional parameter to a VBA existing code

    Here are Functions 1, 2 , and 4

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    09-13-2018
    Location
    houston
    MS-Off Ver
    2010
    Posts
    7

    Re: Adding an additional parameter to a VBA existing code

    Thanks! The question you had Is that I have some cells with two digit numerical values, and some cells with 2 digit alpha character values. Maybe there is a way to count both together in one statement? That would be great actually. So total of three functions. the *s plus color count, the *o plus color count, and the containing "AA or 88" plus color count function. BTW I tried your code for *o and I got an error.
    Last edited by gregpaj; 09-13-2018 at 08:09 PM.

  5. #5
    Registered User
    Join Date
    09-13-2018
    Location
    houston
    MS-Off Ver
    2010
    Posts
    7

    Re: Adding an additional parameter to a VBA existing code

    Im too new of a user so I am not allowed to post images, but I wish I can show you what I mean

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Adding an additional parameter to a VBA existing code

    Quote Originally Posted by gregpaj View Post
    and the containing "AA or 88" plus color count function.
    Do you just want to count cells with any two characters? Or do you have something like 8A that you want to exclude?


    BTW I tried your code for *o and I got an error.
    What is the error description?
    What line is highlighted when you click the Debug button on the error dialog?

  7. #7
    Registered User
    Join Date
    09-13-2018
    Location
    houston
    MS-Off Ver
    2010
    Posts
    7

    Re: Adding an additional parameter to a VBA existing code

    Its a poker hand matrix. 13x13 cells, 169 total cells. bottom left side has all offsuit hands , example A2o. Top right section has all suited variants, example A2s, and cutting down the diagonal cells from top left to bottom right are all the pairs, AA through to 22. So I'm trying to build a VBA function to count all the cells i highlight by color I select, as well as if they are offsuit (*o), suited (*s) or paired (AA-22). Maybe you have a more elegant solution? so ultimately if I had one function where I can select the color range, the color reference, and a third (or fourth) argument to allow me to filer for only suited, offsuit or paired hands would be perfect.

  8. #8
    Registered User
    Join Date
    09-13-2018
    Location
    houston
    MS-Off Ver
    2010
    Posts
    7

    Re: Adding an additional parameter to a VBA existing code

    Error is "compile error: argument not optional" and top line is highlighted in the module

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Adding an additional parameter to a VBA existing code

    Quote Originally Posted by gregpaj View Post
    Error is "compile error: argument not optional" and top line is highlighted in the module
    Sound like you missed an argument; the count range or the color range.


    The function GetColorCount2 below has all three in one function. There are now three arguments. The third argument is 1, 2, or 3.
    1 counts "*o"
    2 counts "*s"
    3 counts two paired characters.

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    09-13-2018
    Location
    houston
    MS-Off Ver
    2010
    Posts
    7

    Re: Adding an additional parameter to a VBA existing code

    Awesome!! that is perfect!! Can I add some text to explain what 1,2 or 3 means in the function window so I don't forget?

  11. #11
    Registered User
    Join Date
    09-13-2018
    Location
    houston
    MS-Off Ver
    2010
    Posts
    7

    Re: Adding an additional parameter to a VBA existing code

    I'm a beginner at this stuff but that makes my life soo much easier. so appreciative

  12. #12
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Adding an additional parameter to a VBA existing code

    To be more descriptive, the crit are now "o", "s", or "xx" instead of 1 ,2, 3. Include the surrounding quotes e.g.
    =GetColorCount2(A1:A100, B1, "xx")

    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. [SOLVED] Additional look up is required to an existing VBA code !
    By pmw19800 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 02-26-2018, 06:14 PM
  2. [SOLVED] Additional look up is required to an existing VBA code
    By pmw19800 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-08-2018, 07:45 AM
  3. [SOLVED] Modfication of Existing Code for additional additional cells
    By thilag in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-29-2016, 03:00 AM
  4. Adding an additional IF function to my existing formula to return a blank
    By xmann21 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-29-2015, 09:52 AM
  5. [SOLVED] Add new (additional range) to existing code
    By Blake 7 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-22-2015, 11:43 AM
  6. [SOLVED] Adding additional selection criteria to existing Macro
    By Mogpot1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-23-2013, 10:29 AM
  7. [SOLVED] adding additional parameter to median formula
    By justinhampton81 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-19-2013, 10:17 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