Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel Vba function, optional argument, or string comparison

Posted on 2015-01-30
6
Medium Priority
?
97 Views
Last Modified: 2016-02-10
Hi expert,

Here is one simple VBA function with optional argument, please help to see what is best to make it work.

Function arith(x, y, Optional operation As String)
    If operation = addition Then
            arith = x + y
        ElseIf operation = subtract Then
            arith = x - y
        ElseIf operation = multiply Then
            arith = x * y
        Else
            arith = x + y      
    End If        
End Function


The function use addition as default operation.

If we set operation = "subtract", then in the input we use =arith(3, 5, "subtract"), it does work. However, we will like to just use =arith(3, 5, substract), what is the best way to make it work.

Thanks,
RDB
0
Comment
Question by:ResourcefulDB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 2000 total points
ID: 40581210
Hi,

This is one solution (with the below code within a Public Code Module):

Option Explicit

Private Enum eNum_Operation
  addition
  subtract
  multiply
End Enum
Private Function arith(x, y, Optional operation As eNum_Operation = addition)

  If operation = addition Then
     arith = x + y
  ElseIf operation = subtract Then
         arith = x - y
  ElseIf operation = multiply Then
         arith = x * y
  Else
     arith = x + y
  End If
  
End Function
Public Sub Test()

  MsgBox CStr(arith(3, 5, subtract)), vbOKOnly, ThisWorkbook.Name
  
End Sub

Open in new window

0
 
LVL 4

Expert Comment

by:scsyme
ID: 40582982
The solution above will work fine if you are calling arith from other code. But if, as it seems, you are trying to achieve this so that it works for functions input into cells of a workbook, then you're asking for a lot. I am not sure why you need to do this without the quotes, but one suggestion I have is to consider adding another function like this:

Public Function addition()
    addition = "addition"
End Function

Open in new window


You can then call your original arith function with the following:
=arith(7,6,addition())

Open in new window


Without knowing the underlying reason for your request it is difficult to know whether this could be of any help.
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 40583762
How about setting your Operation function in a separate cell and then referring to that cell rather than typing it in.

I believe you would set the Operation as a range rather than a string:

Function arith(x, y, Optional operation As Range)

Thanks
Rob H
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ResourcefulDB
ID: 40587024
I tried out codes by fanpages, it gives an error message "variable not defined" and the cursor points to the code "eNum_Operation = addition".

what could be the problem.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 40587182
Can you post the code that fails, & details about which version of MS-Excel you are using, please?

A sample workbook (with any sensitive information removed) would be ideal.
0
 

Author Comment

by:ResourcefulDB
ID: 40592042
ok, I started Excel fresh and input only your code and it works. It is Excel 2010.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question