Converting Excel VBA code to Javascript for use in Google Sheets: A step-by-step guide

Hello everyone, I am looking to convert a VBA code that I wrote into javascript and integrate it into Google Sheets. Does anyone have any suggestions on how I can accomplish this task? The VBA code in question is as follows:

Sub Influencers_automacao()
  Sheets(1).Activate
  Range("A1").Select
  Selection.End(xlDown).Select 
  k = Selection.Row
  For t = 2 To Sheets.Count
    j = 2
    For i = 2 To k
      If Sheets(1).Cells(i, 1) = Sheets(t).Name Then
        Sheets(t).Cells(j, 1) = Sheets(1).Cells(i, 2)
        Sheets(t).Cells(j, 2) = Sheets(1).Cells(i, 3)
        j = j + 1
      End If
    Next 
  Next
End Sub

Answer №1

It appears that in order to achieve your desired outcome, you will need to familiarize yourself with JavaScript and the Google Sheets object model. This will enable you to replicate the actions you are currently performing in VBA using JavaScript.

You may want to consider utilizing a macro converter to convert your existing VBA code. Since your VBA code is relatively straightforward, it may work without any modifications. However, there could be some tweaks required. Please let me know if the conversion is successful, or if you encounter any errors that we can address together.

https://developers.google.com/apps-script/guides/macro-converter/convert-files

Answer №2

Give this a shot

function customFunction() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
  var results = []
  spreadsheet.getSheets().forEach(function (sheet, index) {
    if (index > 0) results.push([
      sheet.getName(), sheet.getRange('C1').getValue(), sheet.getRange('D1').getValue()
    ])
  })
  spreadsheet.getSheets()[0].getRange(2, 1, results.length, results[0].length).setValues(results)
}

adjust values in 'C1' and 'D1' as needed

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Top method to create a timer in React/Redux for automatically refreshing authentication tokens

I am implementing an access refresh jwt authentication flow and need the client to automatically send a refresh token after 10 minutes of receiving the access token. I also want to ensure that if the user's device is idle for an hour, a new access tok ...

The jquery error NS_ERROR_XPC_BAD_CONVERT_JS is causing issues on Google Chrome while working fine on Firefox

Currently, I am utilizing jQuery to dynamically add fields to a form. These are considered "repeatable" fields since users can click an "add more" button. Here is the code snippet: $(".add-attacker-scores").click(function() { count = count + 1; ...

Automatically scroll to the end of the data retrieved through ajax

I am currently developing a live chat website for my users and I am using ajax to fetch the chat messages. However, I am facing an issue where whenever I enter a new message, it does get added to the database and displayed, but the scroll doesn't auto ...

Can anyone recommend a high-quality jQuery lightbox replica?

Key Features Needed: Customizable with CSS Capable of handling forms, not just images Extensively documented Please provide any recommendations and suggestions for suitable options. Thank you in advance ...

Capturing the value of an input field within a controller in AngularJS

I have been programming with JSP and Angular JS. Currently, I am faced with a scenario where I need to work with a JSP page containing a hidden input field. To set the value of this input field, I am using a session attribute like so: String policy = (S ...

Setting the Datetimepicker to be used with every input field

I currently have four textboxes identified by their unique ids: title, sdate, edate, and pdate. My goal is to assign a Datetimepicker to the textboxes that contain the word 'date' in their id. This means I want the Datetimepicker to be assigned ...

Is it possible to use jQuery validate for remote parsing with two fields in a single call

Currently, I am facing an issue while trying to parse two values using jQuery's validate plugin to compare with an SQL database. The DateReceived value is successfully parsed, but the CentreID value always appears as null. Below is the code snippet I ...

Maintaining aspect ratio of canvas while ensuring responsiveness

Currently, I am working on a drawing app and have come across an issue that has been challenging for me to resolve. The dilemma lies in resizing the canvas of sketches from the original resolution of 1280 x 720 to the maximum size possible upon opening the ...

Integrating MongoDB data values with Node.js for enhanced functionality

Hey everyone, I'm looking to add two field values {type:Number} from a MongoDB collection using node js and then store the result back in the same collection. To achieve this, I have outlined the steps below: Retrieve the data value from MongoDB wit ...

Change the input field to uppercase using JavaScript but do not use inline JavaScript

Hey there! I have a basic script set up (see below) with an input field allowing users to enter a query. This query is then sent to a socrata webservice to request specific data, which is displayed in an alert box. So far, everything works smoothly. var l ...

"Error in Visual Studio: Identical global identifier found in Typescript code

I'm in the process of setting up a visual studio solution using angular 2. Initially, I'm creating the basic program outlined in this tutorial: https://angular.io/docs/ts/latest/guide/setup.html These are the three TS files that have been genera ...

Upon logging in to the first HTML page, the user will be automatically redirected to the second HTML page while passing values

Is there a way to automatically redirect users to Login2.html when selecting "TEAM" in the dropdown on Login1.html, using the values entered in Login1.html? Similarly, can we redirect them to the Premium page when they select "Premium"? I need a solution u ...

Run a PHP script on the current page upon choosing an option from a dropdown list with the help of Ajax or JavaScript

I'm currently working on a MySQL query that needs to be executed when a user selects options from multiple dropdown lists. What I am looking for is the ability to automatically run a query related to the selected dropdown list option using AJAX/JavaS ...

Guide on reusing javascript to toggle between image sources

I have a simple website with buttons that, when clicked, change the image being displayed. However, I find myself repeating code for each button, and I'm wondering if there is a more efficient solution to this problem. Here is an example of what I cu ...

Should I serialize a 2D array in JSON format and send it as two separate arrays, or is

When it comes to sending a 2-dimensional array (along with several other variables) to PHP using jQuery.ajax(), I have a couple of options in mind: One option is to serialize to json using JSON-js Another option would be to send both arrays as csv string ...

Consecutive pair of JavaScript date picker functions

My issue involves setting up a java script calendar date picker. Here are my input fields and related java scripts: <input type="text" class="text date" maxlength="12" name="customerServiceAccountForm:fromDateInput" id="customerServiceAccountForm:from ...

Refreshing a webpage post initial loading using Next.js

Having trouble with my checkout route ""./checkout"" that displays embedded elements from Xola. The issue arises when using client-side routing as the checkout page requires a manual refresh to load correctly and show the Xola elements on the DOM ...

Capture an image from the webcam without any manual intervention and store it in the system's directories

Looking to automate the process of capturing a picture from a webcam once access is granted, and then saving it without any user intervention. I've managed to capture the image but struggling with: A) Automating the capture process B) Saving the ca ...

Looking for a solution to organize the dynamically generated list items in an HTML page

I am currently working on a movie listing website where all the movies are displayed in sequence based on their #TITLE#. The webpage is generated automatically by the software using a template file. Here is the section of code in the template file that sho ...

Using Python's pandas library, merging data from sheet1 and sheet2 to an existing Excel file

I have successfully extracted data from a website and saved it to sheet 1 in an Excel file using pandas. Now, I am looking to retrieve a specific column of data and save it to sheet 2 within the same Excel file. However, when I run the code, instead of cr ...