Solved

Excel Vba function, optional argument, or string comparison

Posted on 2015-01-30
6
96 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

622 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