Converting from VBA to Java script

Hi Can someone help me converting this macro to java? I need to share a file with a command button and the only way to share it with multiple users it is through a google sheet. These are the macros:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim TWs As Worksheet
    Dim TLr As Long
    'Things that mean you don't want to run the code:

    If Target.Cells.Count > 1 Then Exit Sub

    If Intersect(Target, Me.Range("Q:Q")) Is Nothing Then Exit Sub

    If LCase(Target.Value) <> "closed" Then Exit Sub

    Set TWs = Worksheets("Closed")

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    TLr = TWs.Range("A" & Rows.Count).End(xlUp).Row
    Target.EntireRow.Copy TWs.Range("A" & TLr + 1).EntireRow
    Target.EntireRow.Delete
    Application.ScreenUpdating = True
    Application.EnableEvents = True

    End Sub

Sorry there is one more

Option Explicit

Sub TransferDataBasedOnHeaders()
    Dim TLr As Long, SLr As Long, i As Long, j As Long
    Dim TWs As Worksheet, SWs As Worksheet
    Dim WsArr As Variant
    Dim v As Variant
    
    WsArr = Array("MARIA CASTRO", "MIKE REINER", "MONICA SANJUAN", "SARA JOHANSEN", "CECILIA PERALTA")
    
    Application.ScreenUpdating = False
    Set TWs = Worksheets("Master")
    TLr = TWs.Range("A" & Rows.Count).End(xlUp).Row
    If TLr > 89 Then
        TWs.Rows("89:" & TLr).Clear
    End If
    For i = LBound(WsArr) To UBound(WsArr)
        TLr = TWs.Range("A" & Rows.Count).End(xlUp).Row
        Set SWs = Worksheets(WsArr(i))
        SLr = SWs.Range("A" & Rows.Count).End(xlUp).Row
        If SLr > 1 Then
            For j = 1 To TWs.UsedRange.Columns.Count
                v = Application.Match(TWs.Cells(18, j).Value, SWs.Rows(1), False)
                If Not IsError(v) Then
                    
                    SWs.Range(SWs.Cells(2, v), SWs.Cells(SLr, v)).Copy
                    
                    TWs.Cells(TLr + 1, j).PasteSpecial (xlPasteAll)
                End If
            Next j
        End If
    Next i
ExitSub:
    Application.CutCopyMode = False
    Application.ScreenUpdating = False
End Sub

The following conversion of the code is all valid JavaScript code:

function worksheetChange(target) {
    var tws;
    var tlr;

    // Things that mean you don't want to run the code:
    if (target.cells.count > 1) {
        return;
    }
    if (!intersect(target, me.range("Q:Q"))) {
        return;
    }
    if (target.value.toLowercase() !== "closed") {
        return;
    }

    tws = worksheets("Closed");

    application.screenUpdating = false;
    application.enableEvents = false;
    tlr = tws.range("A", rows.count).end(xlUp).row;
    target.entireRow.copy(tws.range("A", tlr + 1).entireRow);
    target.entireRow.delete();
    application.screenUpdating = true;
    application.enableEvents = true;
    return;
}
function transferDataBasedOnHeaders() {
    var tlr;
    var slr;
    var tws;
    var sws;
    var wsArr = ["MARIA CASTRO", "MIKE REINER", "MONICA SANJUAN", "SARA JOHANSEN", "CECILIA PERALTA"];
    var v;

    application.screenUpdating = false;
    tws = worksheets("Master");
    tlr = tws.range("A", rows.count).end(xlUp).row;
    if (tlr > 89) {
        tws.rows("89:", tlr).clear();
    }
    wsArr.forEach(function (worksheet) {
        tlr = tws.range("A", rows.count).end(xlUp).row;
        sws = worksheets(worksheet);
        slr = sws.range("A", rows.count).end(xlUp).row;
        if (slr > 1) {
            tws.usedRange.columns.forEach(function (col, j) {
                v = application.match(tws.cells(18, j).value, sws.rows(1), false);
                if (!isError(v)) {
                    sws.range(sws.cells(2, v), sws.cells(slr, v)).copy();
                    tws.cells(tlr + 1, j).pasteSpecial(xlPasteAll);
                }
            });
        }
    });
    application.cutCopyMode = false;
    application.screenUpdating = false;
}

As stated, it’s all valid JavaScript code, but as there’s no way to test it, there’s no telling if it’s going to work for you.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.