• Tech
  • Business
  • Health
  • Sports
  • Contact Us!
  • LinkedIn
  • YouTube
  • Twitter
  • Instagram
March 18 2024

AI for VBA Excel script: Gemini vs ChatGPT vs Copilot Comparison

Dilek ISIK AKCAKAYA Health, Tech Ai, Artificial Intelligence

Image created by DALLE-3 - Edited by Qubilinx
Image created by DALLE-3 – Edited by Qubilinx

We tested VBA scripting capabilities of three AI systems (Copilot, Gemini and ChatGPT) and compared the ease of using prompts.

According to the test using the same prompt here is our list of their usefulness: 

  1. Copilot
  2. ChatGPT
  3. Gemini AI

Prompt used in this comparison is: 

Lets create a VBA code that can copy an excel sheet named Formula to a new sheet and take the new sheet name from cell A2 of Formula sheet, if the new sheet names is already taken create a new version adding a number to the end of the A2 cell entry.

The objective was to create a VBA script that can copy an excel sheet contents into a new one named after the entry in cell A2 of the initial excel sheet. 

Copilot was amazing, did what was asked for at the first trial!

ChatGPT had to go through 3 iterations to get it right, after indicating what the error was it corrected the code and it just ran fine.

Gemini, unfortunately failed us! Even after 30 minutes of retries, corrections, new prompt trials it still did not give us a functioning code. Image of the error is at the bottom of this article.

1 Copilot

# VBA code to copy a sheet and handle duplicate sheet names


Sub CopySheetWithUniqueName()
    Dim wsFormula As Worksheet
    Dim wsNew As Worksheet
    Dim newName As String
    Dim i As Integer
    
    ' Set reference to the Formula sheet
    Set wsFormula = ThisWorkbook.Sheets("Formula")
    
    ' Get the new sheet name from cell A2 of Formula sheet
    newName = wsFormula.Range("A2").Value
    
    ' Check if the sheet name already exists
    If SheetExists(newName) Then
        ' If exists, add a version number
        i = 1
        Do
            i = i + 1
            newName = wsFormula.Range("A2").Value & " (" & i & ")"
        Loop While SheetExists(newName)
    End If
    
    ' Copy the Formula sheet to a new sheet with the unique name
    wsFormula.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    Set wsNew = ActiveSheet
    wsNew.Name = newName
    
    ' Print a success message
    MsgBox "Sheet copied to " & newName & " successfully!"
End Sub

Function SheetExists(sheetName As String) As Boolean
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = ThisWorkbook.Sheets(sheetName)
    On Error GoTo 0
    SheetExists = Not ws Is Nothing
End Function

' Call the main subroutine
Sub Main()
    CopySheetWithUniqueName
End Sub

 

Here’s how the code works:

  1. It sets a reference to the “Formula” sheet.
  2. Retrieves the new sheet name from cell A2 of the “Formula” sheet.
  3. Checks if the sheet name already exists. If it does, it appends a version number.
  4. Copies the “Formula” sheet to a new sheet with the unique name.
  5. Displays a message box with the success notification.

Feel free to customize this code to fit your specific requirements! 🚀

From <https://copilot.microsoft.com/?form=WSBSH2&cvid=0ac33d73c21145f8b3db09a07b11253e&nclid=D1DDF2364A2DA95855CA24F0C725A0D5&ts=1710138107317&nclidts=1710138107&tsms=317>

2 ChatGPT

   
Sub CopySheetWithVersion()
    Dim ws As Worksheet
    Dim newSheetName As String
    Dim newName As String
    Dim versionNumber As Integer
    Dim sheetExistsFlag As Boolean
    
    ' Get the active sheet
    Set ws = ActiveSheet
    
    ' Get the new sheet name from cell A2
    newSheetName = ws.Range("A2").Value
    
    ' Check if sheet with new name already exists
    sheetExistsFlag = False
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name = newSheetName Then
            sheetExistsFlag = True
            Exit For
        End If
    Next ws
    
    ' If sheet with new name already exists, create a new version
    If sheetExistsFlag Then
        versionNumber = 1
        newName = newSheetName & " - " & versionNumber
        Do While CustomSheetExists(newName)
            versionNumber = versionNumber + 1
            newName = newSheetName & " - " & versionNumber
        Loop
        newSheetName = newName
    End If
    
    ' Copy the "Formula" sheet to a new sheet with the new name
    ThisWorkbook.Sheets("Formula").Copy After:=Sheets(Sheets.Count)
    ActiveSheet.Name = newSheetName
End Sub

   

 

You can run this code by opening the VBA editor (Alt + F11), inserting a new module (Insert > Module), and pasting the code into the module. Then, you can run the CopySheetWithVersion subroutine to copy the “Formula” sheet to a new sheet with the specified name.

 

From <https://chat.openai.com/c/cd9c0d21-f489-44ff-ab21-e07578caccb7>

3 Gemini


     Sub CopyFormulasToNewSheetWithVersioning()

  Dim sourceSheet As Worksheet
  Dim newSheet As Worksheet
  Dim sheetName As String
  Dim versionNumber As Integer
  Dim sheetFound As Boolean

  ' Set source sheet (replace "Formula" with actual name, if needed)
  On Error Resume Next
  Set sourceSheet = ThisWorkbook.Worksheets("Formula")
  On Error GoTo 0

  ' Check if source sheet exists
  If sourceSheet Is Nothing Then
    MsgBox "Source sheet not found. Please check the sheet name.", vbExclamation
    Exit Sub
  End If

  ' Get sheet name from cell A2 of the source sheet
  sheetName = sourceSheet.Range("A2").Value

  ' Check if sheet name needs versioning (empty or problematic)
  If sheetName = "" Or sheetName = "A2" Or Left(sheetName, 4) = "R[1C" Then

    ' Start version number at 1
    versionNumber = 1

    ' Loop until a unique sheet name is found
    Do
      sheetFound = False
      For Each ws In ThisWorkbook.Worksheets
        If ws.Name = sheetName & " " & versionNumber Then
          sheetFound = True
          versionNumber = versionNumber + 1
          Exit For
        End If
      Next ws
    Loop Until Not sheetFound

    ' Append the version number to the sheet name (if needed)
    sheetName = sheetName & " " & versionNumber
  End If

  ' Create the new sheet
  Set newSheet = ThisWorkbook.Sheets.Add

  ' Copy used range with formulas from source sheet
  sourceSheet.UsedRange.Copy newSheet.Range("A1")

  ' Rename the new sheet with the final sheet name
  newSheet.Name = sheetName

End Sub
   

 

To use this code:

  • Paste it into a VBA module within your Excel workbook:
  • Open the VBA editor (Alt + F11).
  • Right-click on the workbook name in the Project Explorer and select “Insert” > “Module”.
  • Paste the code into the module’s code window.

Ensure the source sheet is named correctly:

  • If your source sheet is not named “Formula”, replace “Formula” with the actual name in the line Set sourceSheet = ThisWorkbook.Worksheets(“Formula”).

Provide a name in cell A2:

  • Enter the desired name for the new sheet in cell A2 of the source sheet.

Run the macro:

  • Go back to the Excel workbook.
  • Open the “Macros” dialog box (Alt + F8).
  • Select the “CopyFormulasToNewSheetWithVersioning” macro and click “Run”.

The code will create a new sheet with the name from cell A2, adding a version number if the name is already taken.

From <https://gemini.google.com/app/568522f2e456cb94>

Integrating and Leveraging Prompts for AI Productivity: A Comprehensive Guide! Transformer Architecture: Most Effective Deep Learning Model for Text

Related Posts

Title: COPPA - Children and Teens’ Online Privacy Protection Act Creator: Microsoft Copilot Date: January 6, 2025 Description: AI-generated image

Tech

COPPA – Children and Teens’ Online Privacy Protection Act

After digitization, we started using online platforms in every area of our lives. We have used websites for shopping, we have used online banking to make online transactions, and we have watched videos from streaming sites. We have submitted our data many times to many online platforms. The privacy of our data is a growing […]

Title: Use of Cyber Risk Assessments in Developing Cyber Operations and Resiliency Policies, Metrics, Testing, and Security Solutions for an Organization Creator: Microsoft Copilot Date: January 6, 2025

Tech

Use of Cyber Risk Assessments in Developing Cyber Operations and Resiliency Policies, Metrics, Testing and Security Solutions for an Organization

Cyber-attacks are inevitable that is the truth of the day. With the information digitalization, we transform all our data and store them in information systems. Information in our systems is in constant danger of being stolen or damaged. The information security is crucial for the continuity and profitability of the business.  The initial step for securing […]

Title: Anthropomorphic and Cognitive Models in Human-Computer Interaction and AI Creator: Microsoft Copilot Date: January 5, 2025 URL: Microsoft Copilot

Tech

Artificial Intelligence: Human-Computer Interaction Methodologies

HCI METHODOLOGIES & AI   Introduction Human perception and decision-making processes are the key players in HCI methods development.    The most widely used and time-tested four methods known to deliver usable products are:    Anthropomorphic:  Keeps interactions between human and computer like human-to-human interactions.  Creates a human-like perception towards computers Designers need to use […]

Recent Posts

  • Title: COPPA - Children and Teens’ Online Privacy Protection Act Creator: Microsoft Copilot Date: January 6, 2025 Description: AI-generated imageCOPPA – Children and Teens’ Online Privacy Protection Act
  • Title: Use of Cyber Risk Assessments in Developing Cyber Operations and Resiliency Policies, Metrics, Testing, and Security Solutions for an Organization Creator: Microsoft Copilot Date: January 6, 2025Use of Cyber Risk Assessments in Developing Cyber Operations and Resiliency Policies, Metrics, Testing and Security Solutions for an Organization
  • Title: Anthropomorphic and Cognitive Models in Human-Computer Interaction and AI Creator: Microsoft Copilot Date: January 5, 2025 URL: Microsoft CopilotArtificial Intelligence: Human-Computer Interaction Methodologies
  • Human-Computer Interaction Concept Creator: Microsoft Copilot Date: January 4, 2025 URL: Microsoft CopilotWhat is Human Computer Interaction and Where does Artificial Intelligence stand in HCI?
  • Qubilinx AI Tech Series Issue 1ISSUE 1

About

  • About
  • Engadget
  • Our Ads
  • Brand Kit
  • Advertise
  • Buyers Guide
  • Contact Us

News

  • World
  • US
  • Politics
  • Business
  • Tech
  • Science
  • Sports

Technology

  • Review
  • Gear
  • Gaming
  • Multimedia
  • Entertainment
  • Lifestyle
  • Social

Culture

  • Music
  • Movies
  • Fashion
  • Humor
  • Entertainment
  • Critics
  • Cultural Comments
@2024 Qubilinx.com - All Rights Reserved