VBA to Apps Script: where a literal translation runs fine and gets the wrong answer
Rewrite an Excel macro into Google Apps Script one line at a time, by hand or by pasting it into an AI chat, and most of it works. But a handful of places produce no error at all and quietly change an amount or a date. Whoever runs the result is the one who has to catch them. Here are 6 cases that show up constantly in business macros, with the output of both versions taken from actually executing the code.
Check whether your macro has any of these — freeRounding an amount
VBA's Round sends halves to the nearest even number. Math.round always rounds halves up, so invoice totals drift by one.
Sub Total()
Range("B1").Value = Round(Range("A1").Value)
Range("B2").Value = Round(Range("A2").Value)
End Subfunction Total() {
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange('B1').setValue(Math.round(sheet.getRange('A1').getValue()));
sheet.getRange('B2').setValue(Math.round(sheet.getRange('A2').getValue()));
}Result: B1 = 1235 · B2 = 1236
function Total() {
const sheet = SpreadsheetApp.getActiveSheet();
const values = sheet.getRange('A1:A2').getValues();
sheet.getRange('B1:B2').setValues(values.map(([v]) => [vbaRound_(v)]));
}
// VBA Round: halves go to the nearest even digit.
function vbaRound_(value, digits = 0) {
const f = Math.pow(10, digits);
const x = Number((value * f).toPrecision(15));
const r = Math.round(x);
return (Math.abs(x % 1) === 0.5 && r % 2 !== 0 ? r - 1 : r) / f;
}Result: B1 = 1234 · B2 = 1236 (same as VBA)
Due date one month later
DateAdd("m", 1, Jan 31) is Feb 28. setMonth(+1) overflows into March.
Sub DueDate()
Range("B1").Value = Format(DateAdd("m", 1, DateSerial(2026, 1, 31)), "yyyy/mm/dd")
End Subfunction DueDate() {
const d = new Date(2026, 0, 31);
d.setMonth(d.getMonth() + 1);
const text = Utilities.formatDate(d, 'Asia/Tokyo', 'yyyy/MM/dd');
SpreadsheetApp.getActiveSheet().getRange('B1').setValue(text);
}Result: B1 = "2026/03/03"
function DueDate() {
const text = Utilities.formatDate(addMonths_(new Date(2026, 0, 31), 1), 'Asia/Tokyo', 'yyyy/MM/dd');
SpreadsheetApp.getActiveSheet().getRange('B1').setValue(text);
}
// Like VBA DateAdd("m"): keeps the day, clamped to the target month's length.
function addMonths_(date, n) {
const d = new Date(date.getFullYear(), date.getMonth() + n, 1);
const last = new Date(d.getFullYear(), d.getMonth() + 1, 0).getDate();
d.setDate(Math.min(date.getDate(), last));
return d;
}Result: B1 = "2026/02/28" (same as VBA)
Date format string
In VBA, mm after yyyy means month. In Utilities.formatDate, mm is minutes and MM is month.
Sub Stamp()
Range("A1").Value = "発行日 " & Format(DateSerial(2026, 4, 1), "yyyy/mm/dd")
End Subfunction Stamp() {
const text = Utilities.formatDate(new Date(2026, 3, 1), 'Asia/Tokyo', 'yyyy/mm/dd');
SpreadsheetApp.getActiveSheet().getRange('A1').setValue('発行日 ' + text);
}Result: A1 = "発行日 2026/00/01"
function Stamp() {
const text = Utilities.formatDate(new Date(2026, 3, 1), 'Asia/Tokyo', 'yyyy/MM/dd');
SpreadsheetApp.getActiveSheet().getRange('A1').setValue('発行日 ' + text);
}Result: A1 = "発行日 2026/04/01" (same as VBA)
Where to add the next row
End(xlUp) looks at one column. getLastRow() looks at every column, so a memo further down another column moves the row.
Sub AddRow()
Dim r As Long
r = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(r, 1).Value = "new"
End Subfunction AddRow() {
const sheet = SpreadsheetApp.getActiveSheet();
const r = sheet.getLastRow() + 1;
sheet.getRange(r, 1).setValue('new');
}Result: A3 = (left empty)
function AddRow() {
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(lastRowInColumn_(sheet, 1) + 1, 1).setValue('new');
}
// Last non-empty row in one column, like End(xlUp).
function lastRowInColumn_(sheet, col) {
const values = sheet.getRange(1, col, Math.max(sheet.getLastRow(), 1), 1).getValues();
for (let i = values.length - 1; i >= 0; i--) if (values[i][0] !== '') return i + 1;
return 0;
}Result: A3 = "new" (same as VBA)
Checking whether text contains a word
InStr returns 0 when nothing is found and positions start at 1. indexOf returns -1, so If InStr(...) > 0 translated literally misses matches at the start.
Sub Flag()
If InStr(Range("A1").Value, "至急") > 0 Then Range("B1").Value = "★"
End Subfunction Flag() {
const sheet = SpreadsheetApp.getActiveSheet();
if (String(sheet.getRange('A1').getValue()).indexOf('至急') > 0) sheet.getRange('B1').setValue('★');
}Result: B1 = (left empty)
function Flag() {
const sheet = SpreadsheetApp.getActiveSheet();
if (String(sheet.getRange('A1').getValue()).indexOf('至急') + 1 > 0) sheet.getRange('B1').setValue('★');
}Result: B1 = "★" (same as VBA)
Mod with decimals
VBA's Mod rounds both numbers to integers first, so 12.6 Mod 5 is 3. JavaScript's % keeps the decimals.
Sub Remainder()
Range("B1").Value = Range("A1").Value Mod 5
End Subfunction Remainder() {
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange('B1').setValue(sheet.getRange('A1').getValue() % 5);
}Result: B1 = 2.5999999999999996
function Remainder() {
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange('B1').setValue(vbaMod_(sheet.getRange('A1').getValue(), 5));
}
function vbaMod_(a, b) {
const toInt = (x) => (Math.abs(x % 1) === 0.5 ? 2 * Math.round(x / 2) : Math.round(x));
return toInt(a) % toInt(b);
}Result: B1 = 3 (same as VBA)
How VBA Migrate handles these
VBA Migrate converts with these VBA-versus-Apps-Script differences built in. Its 23 regression tests — invoices, sheet-to-sheet copies, rounding and date arithmetic — run the converted code and judge pass or fail by the cell values it produces. Every conversion comes with a list of places where the behaviour differs from Excel and of anything you should check by hand.
The compatibility check is free and unlimited. Conversion is $12 per 700 lines, up to 2,000 lines, and on the online converter your card is charged only after the result works.
Every snippet on this page was executed in our test harness; the results shown are what the code actually produced. The code examples are MIT-licensed — copy them freely.
Looking for the quick reference instead? The VBA to Apps Script cheat sheet covers 15 common idioms, each one executed and checked.