FormulaCamp
Completed

6. Office Scripts vs. VBA: Which to Choose

Lesson 6 of 6
    Lesson theory

    The same task, making a header bold and writing a sum, in two languages:

    ' VBA
    Sub Totals()
        With Worksheets("Sheet1")
            .Range("A1:D1").Font.Bold = True
            .Range("D8").Formula = "=SUM(D2:D7)"
        End With
    End Sub
    // Office Scripts
    function main(workbook: ExcelScript.Workbook) {
      const sheet = workbook.getWorksheet("Sheet1");
      sheet.getRange("A1:D1").getFormat().getFont().setBold(true);
      sheet.getRange("D8").setFormula("=SUM(D2:D7)");
    }
    VBAOffice Scripts
    LanguageVisual Basic, properties: .Value = TypeScript, methods: getValue() / setValue()
    Where it runsDesktop Excel (Windows, Mac)Excel for the web, and desktop Excel with Microsoft 365
    Where it's storedInside an .xlsm workbookAn .osts file in OneDrive; the workbook stays .xlsx
    Automatic startEvents: Workbook_Open, Worksheet_ChangeNo workbook events; schedules and triggers come from Power Automate
    User interfaceUserForm, MsgBox, InputBoxNo forms or dialogs; input comes from parameters or a table
    Reach outsideFiles on the computer, other Office appsThe workbook only; anything else goes through Power Automate actions
    IndexesCells(1, 1) starts at onegetCell(0, 0); arrays start at zero

    How to choose. If you need forms, work with files on disk, or your company's existing macros, use VBA. If the workbook lives in the cloud, people use it in a browser, and you need scheduled runs and links to email, Teams or SharePoint, use Office Scripts with Power Automate. There is no automatic converter from VBA to Office Scripts. You rewrite the logic by hand, which is a good chance to simplify it.

    The simulator doesn't run scripts. Run the code from the lesson in Excel on the web (Automate tab › Code Editor). Here you learn to read code and check yourself with formulas and a quiz.

    Test yourself

    1. A team works with a report only in Excel for the web and wants it emailed every Friday. What should they choose?
    2. How do you write the VBA line Range("B2").Value = 100 in Office Scripts (sheet is the right worksheet)?
    3. You need a form with fields and a Save button that opens on an accountant's computer. What fits?
    4. The VBA code Cells(1, 1) refers to A1. Which Office Scripts call points to the same cell?
    5. You need a timestamp added whenever a cell in the workbook changes, all inside the file. What does that call for?
    6. Where is an Office Script stored?