ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

VBA: Basic Data Normalization Macro

VBA (Visual Basic for Applications)

Goal -- WPM

Ready
Exercise Algorithm Area
1Sub NormalizeDataRange(inputRange As Range)
2' Cleans and normalizes data within a specified Excel range.
3' Removes leading/trailing spaces and converts text to uppercase.
4' Handles empty cells and invalid input ranges.
5
6Dim cell As Range
7Dim dataRange As Range
8
9' Validate input range
10If inputRange Is Nothing Then
11MsgBox "Input range cannot be empty.", vbExclamation
12Exit Sub
13End If
14
15On Error Resume Next ' To catch potential errors with range properties
16If inputRange.Cells.Count = 0 Then
17MsgBox "Input range must contain at least one cell.", vbExclamation
18On Error GoTo 0
19Exit Sub
20End If
21On Error GoTo 0
22
23' Set the range to process
24Set dataRange = inputRange
25
26' Loop through each cell in the range
27For Each cell In dataRange.Cells
28' Check if the cell is not empty before processing
29If Not IsEmpty(cell.Value) Then
30' Trim leading/trailing spaces and convert to uppercase
31cell.Value = UCase(Trim(cell.Value))
32End If
33Next cell
34
35MsgBox "Data normalization complete.", vbInformation
36
37End Sub
Algorithm description viewbox

VBA: Basic Data Normalization Macro

Algorithm description:

This VBA macro automates a common data cleaning task in Excel. It iterates through each cell in a user-defined range, removing any extraneous whitespace from the beginning and end of the cell's content, and then converts the remaining text to uppercase. This is essential for ensuring data consistency, especially when preparing data for analysis, imports, or comparisons.

Algorithm explanation:

The `NormalizeDataRange` subroutine accepts an `inputRange` as a `Range` object. It begins with validation to ensure the `inputRange` is not `Nothing` and contains at least one cell, providing user feedback via `MsgBox` if validation fails. The core logic involves a `For Each` loop that iterates over every `cell` within the `dataRange`. Inside the loop, it checks if the `cell.Value` is not `IsEmpty`. If it's not empty, the cell's value is updated by first applying the `Trim` function to remove leading/trailing spaces and then the `UCase` function to convert it to uppercase. This process ensures that all text data within the specified range is standardized. The time complexity is O(N), where N is the number of cells in the `inputRange`, as each cell is visited and processed once. The space complexity is O(1) as only a few variables are used regardless of the input size. Edge cases handled include an invalid or empty input range and cells that are already empty.

Pseudocode:

SUB NormalizeDataRange(inputRange)
  IF inputRange is invalid OR empty THEN
    DISPLAY error message
    EXIT SUB
  END IF

  FOR EACH cell IN inputRange.Cells DO
    IF cell is NOT empty THEN
      SET cell.Value = UPPERCASE(TRIM(cell.Value))
    END IF
  END NEXT cell

  DISPLAY success message
END SUB