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);         }); }
}

Microsoft 365 and Office | Development | Office JavaScript API
Microsoft 365 and Office | Development | Other
0 comments No comments
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.