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
Solved

Excel Vba function, optional argument, or string comparison

Posted on 2015-01-30
6
89 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

789 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