+ Reply to Thread
Results 1 to 7 of 7

VBA - How to create a user defined function?

  1. #1
    Forum Contributor
    Join Date
    06-28-2016
    Location
    Pennsylvania, USA
    MS-Off Ver
    2013
    Posts
    308

    VBA - How to create a user defined function?

    It's probably pretty easy question, but I am not familiar with the syntax of VBA, so I need some help.

    I would like to have a function called vcolor(), where it uses a number to determine a cell's color.

    If the number is <= 30, it's red. 'cell.Interior.ColorIndex = 3'
    If the number is >30 and <= 80 it's yellow. 'cell.Interior.ColorIndex = 7'
    If the number is >80, it's green. 'cell.Interior.ColorIndex = 4'

    I would like it to return the same number, and simply change the cell's color.

    Use the above strings to change the color (I believe that's correct), I just don't know how to add them into it correctly due to syntax, thanks.
    Last edited by TheN; 07-19-2016 at 07:21 PM. Reason: mistake

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA - How to create a user defined function?

    UDFs, when called by a worksheet formula, cannot change a cell's color. All they can do is return a value.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA - How to create a user defined function?

    You can do it via conditional formatting:

    http://www.contextures.com/xlCondFormat01.html
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Contributor
    Join Date
    06-28-2016
    Location
    Pennsylvania, USA
    MS-Off Ver
    2013
    Posts
    308

    Re: VBA - How to create a user defined function?

    would conditional formatting have the colors change, as the contents of said cell changes?

    that's what I'm looking for.

    I also could have sworn I saw somewhere you could do this with UDFs, trying to go through my browsing history now.

    Edit:

    The conditional formatting seems to work how I want, although there is a small limitation.

    I can only have it work as

    <30 red
    >= 30 and <=80 yellow
    >80 green

    That's fairly close to what I wanted, might have to live with it.
    Last edited by TheN; 07-19-2016 at 07:18 PM. Reason: details on proposed solution.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA - How to create a user defined function?

    In versions of Excel later than 2003, you can have as many conditions as you like.

  6. #6
    Forum Contributor
    Join Date
    06-28-2016
    Location
    Pennsylvania, USA
    MS-Off Ver
    2013
    Posts
    308

    Re: VBA - How to create a user defined function?

    Yeah, I've tested it, seems to work close enough to what I initially wanted, I just don't believe you can change which "color" the difining points are, if you understand what I am saying.

    It basically locks in the > instead of allowing >= to be used, etc.

    Unless I am missing something?

    Thanks for the help nonetheless. Learned something new.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA - How to create a user defined function?

    Yes, you're missing a bunch. All of those things are possible.

+ 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. Replies: 1
    Last Post: 07-19-2014, 06:29 AM
  2. Create a User Defined Function in VBA using the IF and "and" as Function
    By Elomaldo in forum Hello..Introduce yourself
    Replies: 2
    Last Post: 07-16-2014, 12:35 PM
  3. Help to create a user defined function with IF and AND
    By Elomaldo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-16-2014, 11:00 AM
  4. Use formula to create a User Defined Function
    By lindomsm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-29-2013, 06:54 AM
  5. How do I create a user-defined function in Excel 2011?
    By dpbsmith in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-22-2012, 06:26 PM
  6. VBA/Excel - how do I create a user-defined function out of a makro?
    By marasmussen in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2007, 08:21 PM
  7. How can I create a user defined function in excel?
    By Martinj in forum Excel General
    Replies: 4
    Last Post: 08-20-2005, 02:05 PM

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