Code Generator | VBA

Russian Text Analyzer in VBA

This VBA function processes Russian text by counting characters, words, and sentences while incorporating error handling and input validation, providing a comprehensive analysis of the text's structure.


Empty image or helper icon

Prompt

понимаешь русский?

Answer

VBA Function to Process Russian Text

Overview

In this section, we will create a VBA function that takes a string of Russian text, processes it by counting the number of characters, words, and sentences, and returns these statistics as a string. This function will be robust and include error handling and input validation.

Key Features

  • Character Count
  • Word Count
  • Sentence Count
  • Input Validation
  • Documentation and Inline Comments

VBA Code Implementation

Option Explicit

' Function to analyze Russian text
' 
' Parameters:
'   ByVal inputText As String: The Russian text to analyze
'   
' Returns:
'   String: A summary of the analysis including character, word, and sentence count
'   
' Raises:
'   Exception if the input text is empty
Function AnalyzeRussianText(ByVal inputText As String) As String
    ' Validate input
    If Trim(inputText) = "" Then
        Err.Raise vbObjectError + 1, "AnalyzeRussianText", "Input text cannot be empty."
    End If
    
    Dim charCount As Long
    Dim wordCount As Long
    Dim sentenceCount As Long

    ' Count characters
    charCount = Len(inputText)
    
    ' Count words by splitting the text based on spaces
    wordCount = UBound(Split(Trim(inputText), " ")) + 1
    
    ' Count sentences by looking for common sentence-ending punctuation
    sentenceCount = UBound(Split(inputText, ".")) + UBound(Split(inputText, "!")) + _
                    UBound(Split(inputText, "?")) + 2 ' Adding 2 accounts for empty sentences

    ' Construct the result summary
    Dim result As String
    result = "Character Count: " & charCount & vbCrLf & _
             "Word Count: " & wordCount & vbCrLf & _
             "Sentence Count: " & sentenceCount

    ' Return result summary
    AnalyzeRussianText = result
End Function

Code Explanation

  1. Validation: The function first checks if the input string is empty. If it is, it raises a custom error to inform the user.
  2. Character Count: Uses Len to count the number of characters in the string.
  3. Word Count: Uses Split to divide the string by spaces and determines the count.
  4. Sentence Count: Splits the text by common punctuation used in Russian sentences (., !, ?).
  5. Result Summary: Constructs a string summarizing the counts and returns it.

Usage Example

Below is an example of how to utilize the AnalyzeRussianText function within a VBA environment.

Sub TestAnalyzeRussianText()
    Dim text As String
    text = "Привет! Как дела? Это пример текста для анализа."  ' Example Russian text
    Dim result As String
    
    On Error GoTo ErrorHandler  ' Setup error handling
    result = AnalyzeRussianText(text)  ' Call the function
    MsgBox result  ' Display the result in a message box
    Exit Sub
    
ErrorHandler:
    MsgBox "Error: " & Err.Description  ' Show error message
End Sub

Conclusion

This VBA function offers a straightforward way to analyze Russian text by counting characters, words, and sentences. The function is robust, with built-in error handling and validation, making it a reliable tool for anyone working with Russian language text data.

For further learning and development in data analysis and programming, consider exploring courses offered on the Enterprise DNA Platform. These resources can enhance your skills in data science and coding practices.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This VBA function processes Russian text by counting characters, words, and sentences while incorporating error handling and input validation, providing a comprehensive analysis of the text's structure.