Cara menggunakan vba in google spreadsheet

Modul2 - Macro Atau VBA Spreadsheet

Uploaded by

miftah fauzi

0 ratings0% found this document useful (0 votes)

286 views28 pages

Document Information

click to expand document information

Original Title

Modul2_Macro atau VBA Spreadsheet

Copyright

© © All Rights Reserved

Share this document

Share or Embed Document

Sharing Options

  • Share with Email, opens mail client

    Email

Did you find this document useful?

0%0% found this document useful, Mark this document as useful

0%0% found this document not useful, Mark this document as not useful

Is this content inappropriate?

SaveSave Modul2_Macro atau VBA Spreadsheet For Later

0 ratings0% found this document useful (0 votes)

286 views28 pages

Modul2 - Macro Atau VBA Spreadsheet

Original Title:

Modul2_Macro atau VBA Spreadsheet

Uploaded by

miftah fauzi

SaveSave Modul2_Macro atau VBA Spreadsheet For Later

0%0% found this document useful, Mark this document as useful

0%0% found this document not useful, Mark this document as not useful

Embed

Share

Jump to Page

You are on page 1of 28

Search inside document

You're Reading a Free Preview
Pages 7 to 8 are not shown in this preview.

You're Reading a Free Preview
Pages 13 to 26 are not shown in this preview.

Reward Your Curiosity

Everything you want to read.

Anytime. Anywhere. Any device.

No Commitment. Cancel anytime.

Cara menggunakan vba in google spreadsheet

Share this document

Share or Embed Document

Sharing Options

  • Share with Email, opens mail client

Quick navigation

  • Home

  • Books

  • Audiobooks

  • Documents

    , active

Google Sheets offers hundreds of built-in functions like AVERAGE, SUM, and VLOOKUP. When these aren’t enough for your needs, you can use Google Apps Script to write custom functions — say, to convert meters to miles or — then use them in Google Sheets just like a built-in function.

Getting started

Custom functions are created using standard JavaScript. If you're new to JavaScript, Codecademy offers a great course for beginners. (Note: this course wasn't developed by and isn't associated with Google.)

Here's a simple custom function, named DOUBLE, which multiplies an input value by 2:

/**
 * Multiplies an input value by 2.
 * @param {number} input The number to double.
 * @return The input multiplied by 2.
 * @customfunction
*/
function DOUBLE(input) {
  return input * 2;
}

If you don't know how to write JavaScript and don't have time to learn, to see whether someone else has already built the custom function you need.

Creating a custom function

To write a custom function:

  1. Create or open a spreadsheet in Google Sheets.
  2. Select the menu item Extensions > Apps Script.
  3. Delete any code in the script editor. For the DOUBLE function above, simply copy and paste the code into the script editor.
  4. At the top, click Save save.

Now you can .

Getting a custom function from the Google Workspace Marketplace

The Google Workspace Marketplace offers several custom functions as add-ons for Google Sheets. To use or explore these add-ons:

  1. Create or open a spreadsheet in Google Sheets.
  2. At the top, click Add-ons > Get add-ons.
  3. Once the Google Workspace Marketplace opens, click the search box in the top right corner.
  4. Type "custom function" and press Enter.
  5. If you find a custom function add-on you're interested in, click Install to install it.
  6. A dialog box might tell you that the add-on requires authorization. If so, read the notice carefully, then click Allow.
  7. The add-on becomes available in the spreadsheet. To use the add-on in a different spreadsheet, open the other spreadsheet and at the top, click Add-ons > Manage add-ons. Find the add-on you want to use and click Options more_vert > Use in this document.

Using a custom function

Once you've written a custom function or installed one from the Google Workspace Marketplace, it's as easy to use as a built-in function:

  1. Click the cell where you want to use the function.
  2. Type an equals sign (=) followed by the function name and any input value — for example,
    /**
     * Multiplies the input value by 2.
     *
     * @param {number} input The value to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return input * 2;
    }
    
    0 — and press Enter.
  3. The cell will momentarily display
    /**
     * Multiplies the input value by 2.
     *
     * @param {number} input The value to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return input * 2;
    }
    
    1, then return the result.

Guidelines for custom functions

Before writing your own custom function, there are a few guidelines to know.

Naming

In addition to the standard conventions for naming JavaScript functions, be aware of the following:

  • The name of a custom function must be distinct from the names of built-in functions like
    /**
     * Multiplies the input value by 2.
     *
     * @param {number} input The value to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return input * 2;
    }
    
    2.
  • The name of a custom function cannot end with an underscore (
    /**
     * Multiplies the input value by 2.
     *
     * @param {number} input The value to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return input * 2;
    }
    
    3), which denotes a private function in Apps Script.
  • The name of a custom function must be declared with the syntax
    /**
     * Multiplies the input value by 2.
     *
     * @param {number} input The value to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return input * 2;
    }
    
    4, not
    /**
     * Multiplies the input value by 2.
     *
     * @param {number} input The value to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return input * 2;
    }
    
    5.
  • Capitalization does not matter, although the names of spreadsheet functions are traditionally uppercase.

Arguments

Like a built-in function, a custom function can take arguments as input values:

  • If you call your function with a reference to a single cell as an argument (like
    /**
     * Multiplies the input value by 2.
     *
     * @param {number} input The value to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return input * 2;
    }
    
    0), the argument will be the value of the cell.
  • If you call your function with a reference to a range of cells as an argument (like

    /**
     * Multiplies the input value by 2.
     *
     * @param {number} input The value to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return input * 2;
    }
    
    7), the argument will be a two-dimensional array of the cells' values. For example, in the screenshot below, the arguments in
    /**
     * Multiplies the input value by 2.
     *
     * @param {number} input The value to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return input * 2;
    }
    
    8 are interpreted by Apps Script as
    /**
     * Multiplies the input value by 2.
     *
     * @param {number} input The value to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return input * 2;
    }
    
    9. Note that the sample code for DOUBLE would need to be .

    Cara menggunakan vba in google spreadsheet

  • Custom function arguments must be deterministic. That is, built-in spreadsheet functions that return a different result each time they calculate — such as

    /**
     * Multiplies the input value by 2.
     *
     * @param {number|Array<Array<number>>} input The value or range of cells
     *     to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return Array.isArray(input) ?
          input.map(row => row.map(cell => cell * 2)) :
          input * 2;
    }
    
    1 or
    /**
     * Multiplies the input value by 2.
     *
     * @param {number|Array<Array<number>>} input The value or range of cells
     *     to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return Array.isArray(input) ?
          input.map(row => row.map(cell => cell * 2)) :
          input * 2;
    }
    
    2 — are not allowed as arguments to a custom function. If a custom function tries to return a value based on one of these volatile built-in functions, it will display
    /**
     * Multiplies the input value by 2.
     *
     * @param {number} input The value to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return input * 2;
    }
    
    1 indefinitely.

Return values

Every custom function must return a value to display, such that:

  • If a custom function returns a value, the value displays in the cell the function was called from.
  • If a custom function returns a two-dimensional array of values, the values overflow into adjacent cells as long as those cells are empty. If this would cause the array to overwrite existing cell contents, the custom function will throw an error instead. For an example, see the section on .
  • A custom function cannot affect cells other than those it returns a value to. In other words, a custom function cannot edit arbitrary cells, only the cells it is called from and their adjacent cells. To edit arbitrary cells, use a custom menu to run a function instead.
  • A custom function call must return within 30 seconds. If it does not, the cell will display an error:
    /**
     * Multiplies the input value by 2.
     *
     * @param {number|Array<Array<number>>} input The value or range of cells
     *     to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return Array.isArray(input) ?
          input.map(row => row.map(cell => cell * 2)) :
          input * 2;
    }
    
    4

Data types

Google Sheets stores data in different formats depending on the nature of the data. When these values are used in custom functions, Apps Script treats them as the appropriate data type in JavaScript. These are the most common areas of confusion:

  • Times and dates in Sheets become Date objects in Apps Script. If the spreadsheet and the script use different time zones (a rare problem), the custom function will need to compensate.
  • Duration values in Sheets also become
    /**
     * Multiplies the input value by 2.
     *
     * @param {number|Array<Array<number>>} input The value or range of cells
     *     to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return Array.isArray(input) ?
          input.map(row => row.map(cell => cell * 2)) :
          input * 2;
    }
    
    5 objects, but working with them can be complicated.
  • Percentage values in Sheets become decimal numbers in Apps Script. For example, a cell with a value of
    /**
     * Multiplies the input value by 2.
     *
     * @param {number|Array<Array<number>>} input The value or range of cells
     *     to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return Array.isArray(input) ?
          input.map(row => row.map(cell => cell * 2)) :
          input * 2;
    }
    
    6 becomes
    /**
     * Multiplies the input value by 2.
     *
     * @param {number|Array<Array<number>>} input The value or range of cells
     *     to multiply.
     * @return The input multiplied by 2.
     * @customfunction
     */
    function DOUBLE(input) {
      return Array.isArray(input) ?
          input.map(row => row.map(cell => cell * 2)) :
          input * 2;
    }
    
    7 in Apps Script.

Autocomplete

Google Sheets supports autocomplete for custom functions much like for built-in functions. As you type a function name in a cell, you will see a list of built-in and custom functions that matches what you enter.

Custom functions will appear in this list if their script includes a JsDoc

/**
 * Multiplies the input value by 2.
 *
 * @param {number|Array<Array<number>>} input The value or range of cells
 *     to multiply.
 * @return The input multiplied by 2.
 * @customfunction
 */
function DOUBLE(input) {
  return Array.isArray(input) ?
      input.map(row => row.map(cell => cell * 2)) :
      input * 2;
}
8 tag, as in the
/**
 * Multiplies the input value by 2.
 *
 * @param {number|Array<Array<number>>} input The value or range of cells
 *     to multiply.
 * @return The input multiplied by 2.
 * @customfunction
 */
function DOUBLE(input) {
  return Array.isArray(input) ?
      input.map(row => row.map(cell => cell * 2)) :
      input * 2;
}
9 example below.

/**
 * Multiplies the input value by 2.
 *
 * @param {number} input The value to multiply.
 * @return The input multiplied by 2.
 * @customfunction
 */
function DOUBLE(input) {
  return input * 2;
}

Advanced

Using Apps Script services

Custom functions can call certain Apps Script services to perform more complex tasks. For example, a custom function can call the Language service to translate an English phrase into Spanish.

Unlike most other types of Apps Scripts, custom functions never ask users to authorize access to personal data. Consequently, they can only call services that do not have access to personal data, specifically the following:

Supported servicesNotesCacheWorks, but not particularly useful in custom functionsHTMLCan generate HTML, but cannot display it (rarely useful)JDBCLanguageLockWorks, but not particularly useful in custom functionsMapsCan calculate directions, but not display mapsProperties
/**
 * Show the title and date for the first page of posts on the
 * Developer blog.
 *
 * @return Two columns of data representing posts on the
 *     Developer blog.
 * @customfunction
 */
function getBlogPosts() {
  var array = [];
  var url = 'https://gsuite-developers.googleblog.com/atom.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();
  var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
  var entries = document.getRootElement().getChildren('entry', atom);
  for (var i = 0; i < entries.length; i++) {
    var title = entries[i].getChild('title', atom).getText();
    var date = entries[i].getChild('published', atom).getValue();
    array.push([title, date]);
  }
  return array;
}
0 only gets the properties of the spreadsheet owner. Spreadsheet editors can't set user properties in a custom function.SpreadsheetRead only (can use most
/**
 * Show the title and date for the first page of posts on the
 * Developer blog.
 *
 * @return Two columns of data representing posts on the
 *     Developer blog.
 * @customfunction
 */
function getBlogPosts() {
  var array = [];
  var url = 'https://gsuite-developers.googleblog.com/atom.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();
  var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
  var entries = document.getRootElement().getChildren('entry', atom);
  for (var i = 0; i < entries.length; i++) {
    var title = entries[i].getChild('title', atom).getText();
    var date = entries[i].getChild('published', atom).getValue();
    array.push([title, date]);
  }
  return array;
}
1 methods, but not
/**
 * Show the title and date for the first page of posts on the
 * Developer blog.
 *
 * @return Two columns of data representing posts on the
 *     Developer blog.
 * @customfunction
 */
function getBlogPosts() {
  var array = [];
  var url = 'https://gsuite-developers.googleblog.com/atom.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();
  var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
  var entries = document.getRootElement().getChildren('entry', atom);
  for (var i = 0; i < entries.length; i++) {
    var title = entries[i].getChild('title', atom).getText();
    var date = entries[i].getChild('published', atom).getValue();
    array.push([title, date]);
  }
  return array;
}
2).
Cannot open other spreadsheets (
/**
 * Show the title and date for the first page of posts on the
 * Developer blog.
 *
 * @return Two columns of data representing posts on the
 *     Developer blog.
 * @customfunction
 */
function getBlogPosts() {
  var array = [];
  var url = 'https://gsuite-developers.googleblog.com/atom.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();
  var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
  var entries = document.getRootElement().getChildren('entry', atom);
  for (var i = 0; i < entries.length; i++) {
    var title = entries[i].getChild('title', atom).getText();
    var date = entries[i].getChild('published', atom).getValue();
    array.push([title, date]);
  }
  return array;
}
3 or
/**
 * Show the title and date for the first page of posts on the
 * Developer blog.
 *
 * @return Two columns of data representing posts on the
 *     Developer blog.
 * @customfunction
 */
function getBlogPosts() {
  var array = [];
  var url = 'https://gsuite-developers.googleblog.com/atom.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();
  var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
  var entries = document.getRootElement().getChildren('entry', atom);
  for (var i = 0; i < entries.length; i++) {
    var title = entries[i].getChild('title', atom).getText();
    var date = entries[i].getChild('published', atom).getValue();
    array.push([title, date]);
  }
  return array;
}
4).URL FetchUtilitiesXML

If your custom function throws the error message

/**
 * Show the title and date for the first page of posts on the
 * Developer blog.
 *
 * @return Two columns of data representing posts on the
 *     Developer blog.
 * @customfunction
 */
function getBlogPosts() {
  var array = [];
  var url = 'https://gsuite-developers.googleblog.com/atom.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();
  var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
  var entries = document.getRootElement().getChildren('entry', atom);
  for (var i = 0; i < entries.length; i++) {
    var title = entries[i].getChild('title', atom).getText();
    var date = entries[i].getChild('published', atom).getValue();
    array.push([title, date]);
  }
  return array;
}
5, the service requires user authorization and thus cannot be used in a custom function.

To use a service other than those listed above, create a custom menu that runs an Apps Script function instead of writing a custom function. A function that is triggered from a menu will ask the user for authorization if necessary and can consequently use all Apps Script services.

Sharing

Custom functions start out bound to the spreadsheet they were created in. This means that a custom function written in one spreadsheet can't be used in other spreadsheets unless you use one of the following methods:

  • Click Extensions > Apps Script to open the script editor, then copy the script text from the original spreadsheet and paste it into the script editor of another spreadsheet.
  • Make a copy of the spreadsheet that contains the custom function by clicking File > Make a copy. When a spreadsheet is copied, any scripts attached to it are copied as well. Anyone who has access to the spreadsheet can copy the script. (Collaborators who have only view access cannot open the script editor in the original spreadsheet. However, when they make a copy, they become the owner of the copy and can see the script.)
  • Publish the script as a Google Sheets add-on.
. This means that anyone who has permission to edit the spreadsheet can edit any Apps Script code attached to it.

Optimization

Each time a custom function is used in a spreadsheet, Google Sheets makes a separate call to the Apps Script server. If your spreadsheet contains dozens (or hundreds, or thousands!) of custom function calls, this process can be quite slow.

Consequently, if you plan to use a custom function multiple times on a large range of data, consider modifying the function so that it accepts a range as input in the form of a two-dimensional array, then returns a two-dimensional array that can overflow into the appropriate cells.

For example, the

/**
 * Multiplies the input value by 2.
 *
 * @param {number|Array<Array<number>>} input The value or range of cells
 *     to multiply.
 * @return The input multiplied by 2.
 * @customfunction
 */
function DOUBLE(input) {
  return Array.isArray(input) ?
      input.map(row => row.map(cell => cell * 2)) :
      input * 2;
}
9 function shown above can be rewritten to accept a single cell or range of cells as follows:

/**
 * Multiplies the input value by 2.
 *
 * @param {number|Array<Array<number>>} input The value or range of cells
 *     to multiply.
 * @return The input multiplied by 2.
 * @customfunction
 */
function DOUBLE(input) {
  return Array.isArray(input) ?
      input.map(row => row.map(cell => cell * 2)) :
      input * 2;
}

The above approach uses the map method of JavaScript's

/**
 * Show the title and date for the first page of posts on the
 * Developer blog.
 *
 * @return Two columns of data representing posts on the
 *     Developer blog.
 * @customfunction
 */
function getBlogPosts() {
  var array = [];
  var url = 'https://gsuite-developers.googleblog.com/atom.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();
  var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
  var entries = document.getRootElement().getChildren('entry', atom);
  for (var i = 0; i < entries.length; i++) {
    var title = entries[i].getChild('title', atom).getText();
    var date = entries[i].getChild('published', atom).getValue();
    array.push([title, date]);
  }
  return array;
}
7 object to recursively call DOUBLE on every value in the two-dimensional array of cells. It returns a two-dimensional array that contains the results. This way, you can call DOUBLE just once but have it calculate for a large number of cells at once, as shown in the screenshot below. (You could accomplish the same thing with nested AVERAGE0 statements instead of the AVERAGE1 call.)

Cara menggunakan vba in google spreadsheet

Similarly, the custom function below efficiently fetches live content from the Internet and uses a two-dimensional array to display two columns of results with just a single function call. If each cell required its own function call, the operation would take considerably more time, since the Apps Script server would have to download and parse the XML feed each time.

/**
 * Show the title and date for the first page of posts on the
 * Developer blog.
 *
 * @return Two columns of data representing posts on the
 *     Developer blog.
 * @customfunction
 */
function getBlogPosts() {
  var array = [];
  var url = 'https://gsuite-developers.googleblog.com/atom.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();
  var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
  var entries = document.getRootElement().getChildren('entry', atom);
  for (var i = 0; i < entries.length; i++) {
    var title = entries[i].getChild('title', atom).getText();
    var date = entries[i].getChild('published', atom).getValue();
    array.push([title, date]);
  }
  return array;
}

These techniques can be applied to nearly any custom function that is used repeatedly throughout a spreadsheet, although the implementation details will vary depending on the function's behavior.

Apakah Macro dapat digunakan juga di Google Spreadsheet?

Skrip makro dikhususkan untuk sheet individual dan hanya dapat digunakan di Spreadsheet—skrip makro tidak dapat dijalankan di Google Dokumen, Formulir, atau Slide.

Apakah rumus Google Spreadsheet sama dengan Excel?

Anda mungkin bertanya-tanya, apakah rumus Google Sheets dan Microsoft Excel sama mengingat keduanya menjadi aplikasi dengan fungsi yang mirip untuk memudahkan proses pengolahan data dengan perhitungan. Rumus Google Sheets dan Microsoft Excel bisa dibilang sama, tapi tidak mirip karena memiliki perbedaan tertentu.

Bagaimana cara menampilkan module dalam VBA?

Menggunakan Module.
Buka program Microsoft Excel 2016..
Pilih Blank Workbook..
Masuk ke Visual Basic Editor atau gunakan shortcut ALT+F11..
Klik Menu Insert pilih Module..
Module akan di tambahkan pada Project Explorer..

VBA Excel menggunakan bahasa pemrograman apa?

Bahasa yang dipakai pada macro Excel adalah bahasa pemogramman Visual Basic for Application (VBA). VBA adalah bahasa pemrograman berbasis objek untuk memudahkan proses pekerjaan sehari-hari pada Microsoft excel, dari langkah-langkah yang panjang kita dapat lakukan dengan satu langkah mudah.