Converting Excel with Macros to Google Spreadsheets

One of my recent project involved converting a finance related Microsoft Excel Spreadsheet into a web based Google Spreadsheet that can be accessed from a web browser. The Excel sheet had VBA Macros that were converted into corresponding functions using Google Apps Scripts.

Here’s one such VBA routine that scraped Yahoo Finance and was rewritten in Google Apps. In Visual Basic, you create an XMLHttpRequest object to connect to an external server which can be replaced with URLFetch in Apps Script. The debug.print methods can be replaced with Logger.log while the regular JavaScript functions can be used for text manipulation.

The StatusBar property of the Application object in Excel VBA displays the macro progress in the Excel UI and you can replace it with the .toast() method of SpreadsheetApp class in Google Scripts.

Function GetFundName(symbol As String) As String

    Dim Inet1 'As Inet
    Dim fndSym As Integer, endCnt As Integer, begCnt As Integer
    Dim bFound As Boolean

    Application.StatusBar = "Getting Fund Name for " & symbol

    Set Inet1 = CreateObject("Microsoft.XMLHTTP")
    sStockPage = "http://finance.yahoo.com/q/hp?s=" & symbol
    With Inet1
        .Open "GET", sStockPage, False
        .send
        sStockPage = Inet1.ResponseText
    End With
    Set Inet1 = Nothing

    fndSym = InStrRev(sStockPage, "(" & symbol) // .indexOf in JS
    endCnt = fndSym - 2
    bFound = False
    Do Until bFound
        fndSym = fndSym - 1
        'Debug.Print (Mid(sStockPage, fndSym, 1)) // Logger.log in Google Scripts
        bFound = (Mid(sStockPage, fndSym, 1) = ">")
    Loop

    GetFundName = Mid(sStockPage, fndSym + 1, endCnt - fndSym + 1) // .substring() in JS

    Application.StatusBar = False

End Function
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.