pctechguide.com

  • Home
  • Guides
  • Tutorials
  • Articles
  • Reviews
  • Glossary
  • Contact

Dealing with Excel VBA Macros

In this tutorial we will provide an overview of the topic of VBA Functions and User-defined Functions (UDF). We will mention the practices in approaching macros without arguments, functions with one argument and functions with two arguments. We will see some examples of functions with each of these scenarios to help you understand how to work with them.

The example that contains two arguments, one of them will be optional. In case of optional arguments, I will show you how to evaluate whether the argument is entered or not, using the VBA.IsMissing function.

VBA Functions


Let’s review some topics on using functions in VBA for Excel. First, we need to provide a definition of a function.

A function is a procedure that will take arguments and return a value or an array of values.
Functions can be called from procedures or from cells.
There are functions without arguments such as TODAY or NOW.
Public functions are available to all procedures in the file and for use in cells.
Private functions are only available in procedures of the same module.
Custom functions UDF (User Defined Function)
A custom function can be public or private. If it is public we can invoke it from a cell in Excel, but if it is private, it can only be called from procedures.

These functions can be found in the Insert Function dialog box, in the User Defined category and we can have a graphical interface to insert the arguments.

When we enter the equals sign “=” in a cell, we will be able to visualize the UDF, as long as the file or add-in that contains them is open. Excel has more than 450 functions, plus the ones you develop.

Function without arguments
I share with you 3 examples of UDF functions where we do not require arguments to return a result. In the first one we return the name of the active sheet, in the next one the Excel version and in the last one we show the Excel user, which you can find in the Excel Options.

Option Explicit

Function SheetName()
Application.Volatile

SheetName = ActiveSheet.Name

End Function

Function Version()
Application.Volatile

Version = Application.Version

End Function

Function User()
Application.Volatile

User = Application.UserName

End Function
Function with one argument
In this function value we are going to have as argument the Sales value and depending on the quantity, we are going to return a discount. We will use the If-Then-Else statement to evaluate the quantities and MsgBox to show a well elaborated message.

Option Explicit

Function Discount(Sales)
Application.Volatile

If Sales < 10 Then
Discount = 0
ElseIf Sales < 20 Then
Discount = 0.1
Else
Discount = 0.2
End If

End Function

Sub CalculateDiscount()
Dim SalesValue As Integer
Dim Message As String

SalesValue = InputBox(“Enter sales”, “Sales”)

If SalesValue = 0 Then Exit Sub

Message = “Sales are: ” & vbTab & SalesValue
Message = Message & vbNewLine & “The discount is:” & vbTab & SalesValue
Message = Message & vbTab & VBA.Format(Discount(SalesValue), “0%”)

MsgBox Message, vbInformation, “EXCELeINFO”

End Sub
Function with two arguments. One optional
The following function will be valid only for use in an Excel cell. We will achieve that if the user enters the value 1, the entered text will be converted to UPPERCASE, if we enter 2, to lowercase, and if the Type parameter is not entered, the text will be returned as is. The Type parameter is optional, so we will evaluate with VBA.IsMissing if the parameter is entered or not.

Option Explicit

Function CText(Text As String, Optional Type As Variant)

If VBA.IsMissing(Type) Then
Type = 0
CText = Text
Else
Select Case Type
Case 1
CText = VBA.UCase(Text)
Case 2
CText = VBA.LCase(Text)
Case Else
CText = VBA.CVErr(xlErrValue)
End Select
End If

End Function

Filed Under: Articles

Latest Articles

Flat Panel ALiS Technology

Fujitsu has developed a new plasma display type that overcomes the low-resolution restrictions of conventional PDPs. Called Alternate Lighting of Surfaces (ALiS), the technique uses interlaced rather than progressive scans. In a … [Read More...]

NetBurst – Pentium 4 7th generation x86 CPU microarchitecture

NetBurst is the name Intel gave to the new architecture that succeeded its P6 microarchitecture. The concept behind NetBurst was to improve the throughput, improve the efficiency of the out-of-order execution engine, and to create a processor that can reach much higher frequencies with … [Read More...]

Access Quickbooks Remotely

3 Methods for Remote Quickbooks Access

Quickbooks is one of the most popular accounting packages for small business. But, people who are in charge of keeping the books do not want to always be tied to a computer working on their financials. Therefore, there are some remote access alternatives they should be aware of. This article will … [Read More...]

Revolutionize Your Internet Experience with Orbi 960 – The Ultimate WiFi System

In a world where seamless connectivity is essential, slow and unreliable internet connections are a major problem. Whether you are running a business, … [Read More...]

Do You Need a VPN When Trading Cryptocurrency?

There’s no doubt that the biggest global industries in 2023 are tech-driven, while there remains a significant crossover between many of these … [Read More...]

Goodbye Bitcoin: the 3 alternative cryptocurrencies that have great upside potential, according to experts

Bitcoin has been a very lucrative investment for people that got into it early. One report from The Motley Fool pointed out that $10 of bitcoin … [Read More...]

Self-driving cars face their Achilles’ heel and may be targets of hackers

The market for self-driving cars is booming. Customers spent $22.22 billion on these autonomous vehicles in 2021 and they will likely spend more in … [Read More...]

How to avoid scams with bitcoin and other cryptocurrencies

Cryptocurrencies got a bad reputation when scams multiplied like ants on a piece of cake. Even today many people associate bitcoin and other … [Read More...]

WHAT IS CLOUD COMPUTING AND WHAT ARE ITS MAIN BENEFITS?

Users are Increasingly using cloud computing to store their information, which is replacing local storage. The business digitization process goes … [Read More...]

Guides

  • Computer Communications
  • Mobile Computing
  • PC Components
  • PC Data Storage
  • PC Input-Output
  • PC Multimedia
  • Processors (CPUs)

Recent Posts

FlexATX motherboard form factor

The FlexATX is a natural evolution of the Intel's microATX form factor which was first unveiled in late 1999. The FlexATX addendum to the microATX … [Read More...]

Best DNS of 2020: the fastest, safest and most private (Part 1)

DNS servers are on every computer, including yours. It's the technology that means that when you type Xataka.com in your browser, it connects to our … [Read More...]

TheTorntv V10 Ads Removal

The viruses and those internet parasites are gaining population and the usual question of the users will be about protection, capabilities and the … [Read More...]

[footer_backtotop]

Copyright © 2023 About | Privacy | Contact Information | Wrtie For Us | Disclaimer | Copyright License | Authors