Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert VBA script to Google Apps Script automatically?

VBA is the language, in which excel macros were written. Google sheets only supports Google apps script as a macro language. Is there a direct or automatic way to convert a VBA script to a Google Apps Script script without rewriting the code?

Here is a sample VBA script, that I am trying to convert:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rngStart As Range, rngCrit As Range
    Set rngStart = Range("A3")
    Set rngCrit = Range("B1")
    On Error GoTo ExitPoint
    Application.EnableEvents = False
    If Not Intersect(Target, rngCrit) Is Nothing Then
        Range(rngStart, Cells(Rows.Count, rngStart.Column).End(xlUp)).ClearContents
        If Val(rngCrit) > 0 Then
            With rngStart.Resize(Val(rngCrit))
                .Value = "=ROW()-" & rngStart.Row - 1
                .Value = .Value
            End With
        End If
    End If
ExitPoint:
    Set rngStart = Nothing
    Set rngCrit = Nothing
    Application.EnableEvents = True
End Sub

What I've tried:

I've tried to substitute some of the commands with some of the equivalent Google Scripts. My issue is I am thinking too linear. I am trying to code the same exact way as VBA and just trying to switch keywords from VBA to ones in Google Script. I was hoping there was a direct method.

like image 781
user1772434 Avatar asked Aug 28 '26 23:08

user1772434


1 Answers

There are a few examples of such "conversions" out there - but they are typically re-implementations, not conversions. It's a subtle difference, but important. While you could feasibly translate from one language to another, the real challenge is in the differences between the underlying object models and available methods in Excel/VBA vs. Google Spreadsheets / Apps Script / javascript.

Your best chance for success is to use the VBA Sub as a kind of specification; if you can clearly explain what functionality is implemented by a VBA Sub, you have the basis for a specification for a new function implemented in your google spreadsheet.

like image 135
Mogsdad Avatar answered Aug 31 '26 06:08

Mogsdad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!