Code Generator | VBA

Suboptimal VBA Worksheet Automation Example

This example showcases a suboptimal VBA function for Excel that fills cells with random data, colors them based on values, inserts SUM formulas, and formats cells. It highlights inefficiencies for future optimization.


Empty image or helper icon

Prompt

Can you please create an example of some code that runs an automation on a worksheet think of a detail example. Could you also make the code very verbose so it does a lot of things and suboptimal ways I’m using this as an example of how we can optimise it eventually

Answer

VBA Code Example: Suboptimal Worksheet Automation

This example demonstrates a suboptimal VBA function that performs multiple tasks on an Excel worksheet. The goal is to illustrate areas for potential optimization later.

Function Overview

  • Purpose: The function executes several tasks such as filling cells with data, formatting cells, and inserting formulas in a verbose and suboptimal manner.
  • Tasks:
    1. Fill cells with random data.
    2. Update cell colors based on value.
    3. Insert formulas to calculate sums.
    4. Format cells with specific borders.

VBA Code

Option Explicit

Sub SuboptimalWorksheetAutomation()
    ' Declare variables
    Dim ws As Worksheet
    Dim i As Integer, j As Integer
    Dim sumRange As Range
    Dim randomValue As Integer
    
    ' Set the worksheet to the active sheet
    Set ws = ActiveSheet
    
    ' Fill cells with random data
    For i = 1 To 10
        For j = 1 To 10
            ' Generate a random number between 1 and 100
            randomValue = Int((100 - 1 + 1) * Rnd + 1)
            ' Fill the cell with the random number
            ws.Cells(i, j).Value = randomValue
        Next j
    Next i
    
    ' Update cell colors based on value
    For i = 1 To 10
        For j = 1 To 10
            If ws.Cells(i, j).Value > 50 Then
                ' Color cells green if value > 50
                ws.Cells(i, j).Interior.Color = RGB(0, 255, 0)
            Else
                ' Color cells red if value <= 50
                ws.Cells(i, j).Interior.Color = RGB(255, 0, 0)
            End If
        Next j
    Next i
    
    ' Insert formulas to calculate row sums
    For i = 1 To 10
        ' Create a range for the row
        Set sumRange = ws.Range(ws.Cells(i, 1), ws.Cells(i, 10))
        ' Insert the SUM formula at the 11th column of each row
        ws.Cells(i, 11).Formula = "=SUM(" & sumRange.Address & ")"
    Next i
    
    ' Format cells with borders
    For i = 1 To 11
        For j = 1 To 11
            With ws.Cells(i, j).Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = 0
            End With
            With ws.Cells(i, j).Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = 0
            End With
        Next j
    Next i
    
    ' Notify user that the automation is complete
    MsgBox "Worksheet automation is complete.", vbInformation
End Sub

Usage Example

To use the function, simply open your Excel workbook and press ALT + F11 to open the VBA editor. Insert a new module and paste the above code. Close the VBA editor and run the SuboptimalWorksheetAutomation macro from the Developer > Macros menu in Excel.

Key Points for Optimization

  1. Efficiency: The nested loops can be optimized to reduce the number of iterations.
  2. Cell Coloring: Combined with conditional formatting for better performance.
  3. Formulas: Handles with array formulas or SUM across Ranges directly.
  4. Formatting: Borders and other formatting can be applied more efficiently.

Summary

This suboptimal VBA function serves as an example for future optimization. By identifying inefficiencies such as repeated nested loops and redundant operations, we can improve the code performance and readability.

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 example showcases a suboptimal VBA function for Excel that fills cells with random data, colors them based on values, inserts SUM formulas, and formats cells. It highlights inefficiencies for future optimization.