Solved

Excel Vba function, optional argument, or string comparison

Posted on 2015-01-30
6
93 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 500 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

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!

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

752 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