VBA Excel How to use Function with IF, Case to get the words we need - learnit

Home Top Ad

Post Top Ad

Thursday, September 2, 2021

VBA Excel How to use Function with IF, Case to get the words we need

VBA Excel How to use Function with IF, Case to get the words we need

What is Excel VBA?

VBA is an abbreviation for Visual Basic for Applications. Excel VBA is Microsoft's programming language for Excel and other Microsoft Office applications such as Word and PowerPoint. The Office suite programs are all written in the same programming language.


Why Use Excel VBA?

While VBA does not allow users to directly manipulate the main Excel software, it does allow them to master the art of creating macros to optimize their time in Excel. Excel macros can be created in two The Macro Recorder is the first method. When you activate the recorder, Excel will record all of the steps you take and save them as a "process" known as a macro. When the user exits the recorder, this macro is saved and can be assigned to a button, which when clicked, will repeat the exact same process. This method is relatively simple and does not necessitate any prior knowledge of VBA code. This method will work for simple processes.


Function Excel VBA

You can use VBA to create a custom Function (also known as a User Defined Function) that can be used in worksheets like regular functions.


You can use VBA to create a custom Function (also known as a User Defined Function) that can be used in worksheets in the same way that regular functions are.


MS Excel: How to use the CASE Statement (VBA)

This Excel tutorial includes syntax and examples to demonstrate how to use the Excel CASE statement.


Description

The CASE statement in Microsoft Excel has the functionality of an IF-THEN-ELSE statement.

The CASE statement is an Excel built-in function that is classified as a Logical Function. It can be used in Excel as a VBA function (VBA). You can use this function as a VBA function in macro code written in the Microsoft Visual Basic Editor.


Syntax

The syntax for the CASE statement in Microsoft Excel is:
Select Case test_expression
Case condition_1
result_1
Case condition_2
result_2
...
Case condition_n
result_n
[ Case Else
result_else ]
End Select


Parameters or Arguments

test_expression

A string or a number. You are comparing the value to the list of conditions. (ie: condition_1, condition_2, ... condition_n)


condition_1, ... condition_n

Conditions that are evaluated in the order they are listed. When a condition is found to be true, the corresponding code is executed and the conditions are no longer evaluated.


result_1, ... result_n

When a condition is found to be true, code is executed.


If Then

VBA If Statements allow you to test if expressions are TRUE or FALSE, running different code based on the results.Let’s look at a simple example:

Example

If Range("a2").Value = “M” Then Range("b2").Value = "Male"


Code and Example

Option Explicit
'Main Function
Function NumbertoText(ByVal MyNumber)
Dim Dollars, Cents, Temp
Dim DecimalPlace, Count
ReDim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Million "
Place(4) = " Billion "
Place(5) = " Trillion "
' String representation of amount.
MyNumber = Trim(Str(MyNumber))
' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Convert cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & _
"00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
Select Case Dollars
Case ""
Dollars = "Zero Dollars"
Case "One"
Dollars = "One Dollar"
Case Else
Dollars = Dollars & " Dollars"
End Select
Select Case Cents
Case ""
Cents = " and Zero Cents"
Case "One"
Cents = " and One Cent"
Case Else
Cents = " and " & Cents & " Cents"
End Select
NumbertoText = Dollars & Cents
End Function
' Converts a number from 100-999 into text
Function GetHundreds(ByVal MyNumber)
Dim Result As String
If Val(MyNumber) = 0 Then Exit Function
MyNumber = Right("000" & MyNumber, 3)
' Convert the hundreds place.
If Mid(MyNumber, 1, 1) <> "0" Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If
' Convert the tens and ones place.
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & GetTens(Mid(MyNumber, 2))
Else
Result = Result & GetDigit(Mid(MyNumber, 3))
End If
GetHundreds = Result
End Function
' Converts a number from 10 to 99 into text.
Function GetTens(TensText)
Dim Result As String
Result = "" ' Null out the temporary function value.
If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...
Select Case Val(TensText)
Case 10: Result = "Ten"
Case 11: Result = "Eleven"
Case 12: Result = "Twelve"
Case 13: Result = "Thirteen"
Case 14: Result = "Fourteen"
Case 15: Result = "Fifteen"
Case 16: Result = "Sixteen"
Case 17: Result = "Seventeen"
Case 18: Result = "Eighteen"
Case 19: Result = "Nineteen"
Case Else
End Select
Else ' If value between 20-99...
Select Case Val(Left(TensText, 1))
Case 2: Result = "Twenty "
Case 3: Result = "Thirty "
Case 4: Result = "Forty "
Case 5: Result = "Fifty "
Case 6: Result = "Sixty "
Case 7: Result = "Seventy "
Case 8: Result = "Eighty "
Case 9: Result = "Ninety "
Case Else
End Select
Result = Result & GetDigit
_ (Right(TensText, 1)) ' Retrieve ones place.
End If
GetTens = Result
End Function
' Converts a number from 1 to 9 into text.
Function GetDigit(Digit)
Select Case Val(Digit)
Case 1: GetDigit = "One"
Case 2: GetDigit = "Two"
Case 3: GetDigit = "Three"
Case 4: GetDigit = "Four"
Case 5: GetDigit = "Five"
Case 6: GetDigit = "Six"
Case 7: GetDigit = "Seven"
Case 8: GetDigit = "Eight"
Case 9: GetDigit = "Nine"
Case Else: GetDigit = ""
End Select
End Function

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad