+ Reply to Thread
Results 1 to 10 of 10

Difficulty With User Defined Functions (UDF) - Case Statements for String & Int

  1. #1
    Registered User
    Join Date
    10-10-2017
    Location
    West Coast
    MS-Off Ver
    2016
    Posts
    12

    Difficulty With User Defined Functions (UDF) - Case Statements for String & Int

    Hi All,

    I cannot seem to get this function going - It's been years since i've done any VBA programming so I'm sure there are many issues:

    I am trying to have an UDF that looks at a string in column A (country), and a value in column C (this is a f(x)=COUNTA value) - then provides an output in column 'Bx, where 'x' is the corresponding row (ie, a1,b1,c1... a9,b9,c9).

    So it would go something like: if cell A1 contains 'russia' or 'China (P.R.)', display "Group A" in B1. If not, and cell C1 meets range of 1 to 29, display value "Group C", etc..

    Please Login or Register  to view this content.
    Thank you for your help.

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

    Re: Difficulty With User Defined Functions (UDF) - Case Statements for String & Int

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Difficulty With User Defined Functions (UDF) - Case Statements for String & Int

    One possible problem is that the function is an integer, but getting assigned a string by the code... your dim statements are commented out, but you'd have trouble trying to Dim STED as String when the function is already named STED and declared as an Integer.
    Additionally this is not how Select Case works... you are selecting the Case Country_test but then not actually checking for a value, instead checking other conditions...

    Here is a possible alternative:

    Please Login or Register  to view this content.
    do note that if count_test is 1 or 29 or 30 or 69 then they are not included... you may want the => =< operators instead... but wasn't sure that is true.
    Last edited by Arkadi; 10-16-2017 at 03:57 PM.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  4. #4
    Registered User
    Join Date
    10-10-2017
    Location
    West Coast
    MS-Off Ver
    2016
    Posts
    12

    Re: Difficulty With User Defined Functions (UDF) - Case Statements for String & Int

    Thank you both for your help, I have gotten it working a little.

    Another question:

    What is the correct syntax for determining if a specific cell in the row has a value? I will be adding another check under the 4th case(ie. Number is between 30-77 (if cell H1 is: yes - Group C; no = group D)

    Please Login or Register  to view this content.
    Last edited by Deyeski; 10-16-2017 at 04:11 PM. Reason: context

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

    Re: Difficulty With User Defined Functions (UDF) - Case Statements for String & Int

    If you are going to test another value, then it should be passed as an argument to the function. Otherwise, Excel sees no dependency and won't recalculate the function if it changes.

  6. #6
    Registered User
    Join Date
    10-10-2017
    Location
    West Coast
    MS-Off Ver
    2016
    Posts
    12

    Re: Difficulty With User Defined Functions (UDF) - Case Statements for String & Int

    Quote Originally Posted by shg View Post
    If you are going to test another value, then it should be passed as an argument to the function. Otherwise, Excel sees no dependency and won't recalculate the function if it changes.

    Can you clarify? Are you saying I should add an additional cell at the top during function defining, or and additional case argument?

    ie:
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.

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

    Re: Difficulty With User Defined Functions (UDF) - Case Statements for String & Int

    I'm saying a function should receive all its arguments.

    You wouldn't write a function that calculates the sum of one number passed to it plus some other random cell it finds on the worksheet, right?
    Last edited by shg; 10-16-2017 at 04:46 PM.

  8. #8
    Registered User
    Join Date
    10-10-2017
    Location
    West Coast
    MS-Off Ver
    2016
    Posts
    12

    Re: Difficulty With User Defined Functions (UDF) - Case Statements for String & Int

    okay thank you, but what is the best way to determine if a cell has a value? this is what I have done and it returns all 'False'

    Please Login or Register  to view this content.

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

    Re: Difficulty With User Defined Functions (UDF) - Case Statements for String & Int

    If requirement is a cell, it should be of type Range, not integer. IsEmpty is a fine test, but it applies only to data type Variant (which is what a cell contains).
    Last edited by shg; 10-16-2017 at 06:30 PM.

  10. #10
    Registered User
    Join Date
    10-10-2017
    Location
    West Coast
    MS-Off Ver
    2016
    Posts
    12

    Re: Difficulty With User Defined Functions (UDF) - Case Statements for String & Int

    thank you for the help, this has been resolved.

+ 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] I have had difficulty in creating user defined functions in Excel
    By MichaelG in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 PM
  2. [SOLVED] I have had difficulty in creating user defined functions in Excel
    By MichaelG in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 10:05 AM
  3. I have had difficulty in creating user defined functions in Excel
    By DerbyJim1978 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-06-2005, 07:05 AM
  4. [SOLVED] I have had difficulty in creating user defined functions in Excel
    By DerbyJim1978 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-06-2005, 04:05 AM
  5. I have had difficulty in creating user defined functions in Excel
    By MichaelG in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  6. I have had difficulty in creating user defined functions in Excel
    By MichaelG in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM
  7. I have had difficulty in creating user defined functions in Excel
    By MichaelG in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 11:05 PM
  8. [SOLVED] I have had difficulty in creating user defined functions in Excel
    By MichaelG in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 10: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