How to Convert VBA to Office Script?

Daniel Ojeda (MX) 0 Reputation points
2023-06-07T07:09:49.81+00:00

Hi everyone,

I hope you are well, I am new with Office Scripts, any advice is welcome, appreciated and most of all thanked.

I have a code (VBA) that is working correctly in an Excel Desktop (365), but in the online version that I share with my colleagues, it just doesn't work.

I am trying to adjust/convert this VBA code to an Office Script one, but I can't get it to work, it doesn't give me any error but still does what I expect.

I share with you both codes hoping you can help me with this mess. First, the VBA that works correctly and then my Script attempt.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim CeldaActiva As Range
    Set CeldaActiva = Target.Cells(1)
    
    If Not Application.Intersect(CeldaActiva, Range("A5:G10")) Is Nothing Then
      Sheet1.Range("K1").Value = CeldaActiva.Value
    End If
End Sub
OfficeScript.gif

function main(workbook: ExcelScript.Workbook) {
    // function Worksheet_SelectionChange() {     let CeldaActiva = context.workbook.getActiveCell();     CeldaActiva.load("value");         return context.sync()         .then(function () {             let rangeA5G10 = context.workbook.worksheets.getItem("Sheet1").getRange("A5:G10");             let intersectRange = rangeA5G10.getIntersection(CeldaActiva);                         if (intersectRange != null) {                 let k1Range = context.workbook.worksheets.getItem("Sheet1").getRange("K1");                 k1Range.values = CeldaActiva.value;             }         })         .catch(function (error) {             console.log(error);         }); }
}

JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
866 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,480 questions
0 comments No comments
{count} votes