🦄

JavaScript分かる人のためのExcel VBA書き換え

2022/10/12に公開

拡張子

言語 拡張子
JavaScript .js
Excel VBA マクロ付きExcelは.xlsm
VBAのコードのみ別管理するなら.bas(特に規定なし→Visual Basicの拡張子が相性が良い)

書き始め

HTML
<script src="filename.js"></script>
Excel VBA
Sub procedurename()
End Sub

コメントアウト

JavaScript
// 1行コメント
/*
  複数行コメント
*/
Excel VBA
' 1行コメント

結果出力

JavaScript
console.log("Hello World");
Excel VBA
Debug.Print("Hello World")

変数と型

JavaScript
let str = "文字列";
let i = 100;
Excel VBA
Dim str As String: str = "文字列"
Dim i As Integer: i = 100

定数

JavaScript
const str = "文字列";
const i = 100;
Excel VBA
Const str As String = "文字列"
Const i As Integer = 100

文字列結合

JavaScript
console.log("Hello " + "World");
Excel VBA
Debug.Print("Hello " & "World")

式と演算子

JavaScript
console.log(10 + 5); // 加算
console.log(10 - 5); // 減算
console.log(10 * 5); // 乗算
console.log(10 / 5); // 徐算
console.log(10 % 3); // 剰余
console.log(10 ** 3); // べき乗
Excel VBA
Debug.Print(10 + 5) ' 加算
Debug.Print(10 - 5) ' 減算
Debug.Print(10 * 5) ' 乗算
Debug.Print(10 / 5) ' 徐算
Debug.Print(10 Mod 3) ' 剰余
Debug.Print(10 ^ 3) ' べき乗

条件分岐(if文)

JavaScript
const score = 80;
if (score === 100) {
  console.log("満点");
} else if (score >= 60) {
  console.log("合格");
} else {
  console.log("不合格");
}
Excel VBA
Const score As Integer = 80
If score = 100 Then
  Debug.Print("満点")
ElseIf score >= 60 Then
  Debug.Print("合格")
Else
  Debug.Print("不合格")
End If

条件分岐(switch文)

JavaScript
const country = "Japan";
switch (country) {
  case "Japan":
  case "Korea":
    console.log("Asia");
    break;
  case "Estonia":
    console.log("Europe");
    break;
  default:
    console.log("Earth");
    break;
}
Excel VBA
Const country As String = "Japan"
Select Case country
  Case "Japan", "Korea"
    Debug.Print("Asia")
  Case "Estonia"
    Debug.Print("Europe")
  Case Else
    Debug.Print("Earth")
End Select

比較演算子と論理演算子

JavaScript
console.log(10 == 10); // 等しい
console.log(10 != 9);  // 等しくない
console.log(10 < 11);  // より小さい
console.log(10 <= 10); // 以下
console.log(10 > 9);   // より大きい
console.log(10 >= 10); // 以上
console.log(4 < 5 && 5 <= 5); // 論理積
console.log(4 < 5 || 5 < 5); // 論理和
console.log(!(5 < 5)); // 論理否定
Excel VBA
Debug.Print(10 = 10)  ' 等しい
Debug.Print(10 <> 9)  ' 等しくない
Debug.Print(10 < 11)  ' より小さい
Debug.Print(10 <= 10) ' 以下
Debug.Print(10 > 9)   ' より大きい
Debug.Print(10 >= 10) ' 以上
Debug.Print(4 < 5 And 5 <= 5) ' 論理積
Debug.Print(4 < 5 Or 5 < 5)   ' 論理和
Debug.Print(Not 5 < 5) ' 論理否定

配列(静的)

JavaScript
const scores = [80, 50, 70];
console.log(scores[0]);
console.log(scores[1]);
console.log(scores[2]);
Excel VBA
Dim scores() As Variant: scores = Array(80, 50, 70) ' ※Constは使えない
Debug.Print(scores(0))
Debug.Print(scores(1))
Debug.Print(scores(2))

配列(動的)

JavaScript
let scores = [];
scores.push(80);
scores.push(50);
scores.push(70);
scores.pop(); // 配列の最後の値を削除
scores.shift(); // 配列の最初の値を削除
Excel VBA
Dim scores() As Variant: ReDim scores(0)
ReDim Preserve scores(UBound(scores) + 1): scores(UBound(scores)) = 80
ReDim Preserve scores(UBound(scores) + 1): scores(UBound(scores)) = 50
ReDim Preserve scores(UBound(scores) + 1): scores(UBound(scores)) = 70
ReDim Preserve scores(UBound(scores) - 1) ' 配列の最後の値を削除
' ↓配列の最初の値を削除(※全て前にずらす(実際はfor文使うと良い))↓
scores(1) = scores(2)
ReDim Preserve scores(UBound(scores) - 1)

繰り返し(for文)

JavaScript
for (let i = 0; i < 10; i++) {
  console.log(i);
}
Excel VBA
Dim i As Long
For i = 0 To 9
  Debug.Print(i)
Next i

繰り返し(配列のfor文)

JavaScript
let scores = [];
scores.push(80);
scores.push(50);
for (let i = 0; i < scores.length; i++) {
  console.log(scores[i]);
}
Excel VBA
Dim i As Long
Dim scores() As Variant: ReDim scores(0)
ReDim Preserve scores(UBound(scores) + 1): scores(UBound(scores)) = 80
ReDim Preserve scores(UBound(scores) + 1): scores(UBound(scores)) = 50
For i = 1 To UBound(scores)
  Debug.Print(scores(i))
Next i

オブジェクト

JavaScript
const scores = {
  japanese: 80,
  english: 50,
  math: 70
}
console.log(scores.japanese);
console.log(scores["english"]);
Excel VBA
Dim scores As Object: Set scores = CreateObject("Scripting.Dictionary")
scores.Add "japanese", 80
scores.Add "english", 50
scores.Add "math", 70
Debug.Print(scores("japanese"))
Debug.Print(scores.Item("english"))

関数

JavaScript
function getGreeting(timeZone) {
  if (timeZone === "朝") {
    return "おはよう";
  } else {
    return "こんにちは";
  }
}
console.log(getGreeting("朝"));
Excel VBA
Sub main()
  Debug.Print(getGreeting("朝"))
End Sub
Function getGreeting(ByVal timeZone As String) As String
  If timeZone = "朝" Then
    getGreeting = "おはよう"
  Else 
    getGreeting = "こんにちは"
  End If
End Function

Discussion