Prompt
Answer
VBA Function for Separating Arabic Letters/Symbols from Numbers
This document provides a VBA function that separates Arabic letters or symbols from numbers within a given string. The code includes necessary imports, a detailed docstring, input validation, commentary, and an example of usage.
Function Overview
The function SeparateArabicFromNumbers
takes an input string and returns two separate strings: one containing Arabic letters/symbols and another containing numbers.
Function Signature
Function SeparateArabicFromNumbers(inputString As String) As Collection
Parameters:
inputString
(String): The input string containing Arabic letters, symbols, and numbers.
Returns:
- A
Collection
containing two elements:- Arabic characters and symbols (String)
- Numbers (String)
Exceptions:
- Raises an error if the input is not a string.
Code Implementation
Option Explicit
Function SeparateArabicFromNumbers(inputString As String) As Collection
' Create a collection to store the results: Arabic and Numbers
Dim result As Collection
Set result = New Collection
' Validate input
If Not VarType(inputString) = vbString Then
Err.Raise vbObjectError + 1, "SeparateArabicFromNumbers", "Input must be a string."
End If
' Variables to hold separated results
Dim arabicCharacters As String
Dim numbers As String
Dim i As Integer
Dim currentChar As String
' Loop through each character in the input string
For i = 1 To Len(inputString)
currentChar = Mid(inputString, i, 1) ' Get the current character
' Check if the character is an Arabic letter or symbol
If IsArabicCharacter(currentChar) Then
arabicCharacters = arabicCharacters & currentChar ' Append to Arabic result
ElseIf IsNumeric(currentChar) Then
numbers = numbers & currentChar ' Append to Numbers result
End If
Next i
' Add results to the collection
result.Add arabicCharacters
result.Add numbers
Set SeparateArabicFromNumbers = result ' Return the result
End Function
' Helper function to check if a character is Arabic
Private Function IsArabicCharacter(character As String) As Boolean
IsArabicCharacter = (AscW(character) >= &H60 && AscW(character) <= &H7F)
End Function
Explanation of the Code
Imports and Options: The code utilizes
Option Explicit
to enforce variable declaration.Collection Initialization: A
Collection
is initialized to hold the separated Arabic characters and numbers.Input Validation: Checks if the input is a string and raises an error if not.
Loop Through Input String: Each character of the string is evaluated:
- If it's an Arabic character, it's added to
arabicCharacters
. - If it's a numeric character, it's added to
numbers
.
- If it's an Arabic character, it's added to
Return Result: The function returns the collection containing the two separate components.
Helper Function:
IsArabicCharacter
checks if a character falls within the Unicode range designated for Arabic characters.
Example Usage
Below is an example demonstrating how to use the SeparateArabicFromNumbers
function.
Sub TestSeparatingFunction()
Dim inputStr As String
Dim result As Collection
Dim arabicPart As String
Dim numberPart As String
' Example input string
inputStr = "12345 سلام, 678 عزيزي"
' Call the function
Set result = SeparateArabicFromNumbers(inputStr)
' Get Arabic and Number parts from the result
arabicPart = result(1)
numberPart = result(2)
' Output results
Debug.Print "Arabic Characters: " & arabicPart
Debug.Print "Numbers: " & numberPart
End Sub
Summary
This VBA function effectively separates Arabic characters from numbers in a provided string, promoting robust code practices, including proper documentation, input validation, and clear commentary throughout.
For further learning and development in data science and advanced VBA coding, consider utilizing the Enterprise DNA platform.
Description
This document presents a VBA function, SeparateArabicFromNumbers
, to extract Arabic letters and numbers from a string, returning them as a collection. It includes input validation, detailed code implementation, and example usage.