0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【VBA入門②】Dimってなに?変数とデータ型のキホンをやさしく解説✨

Last updated at Posted at 2025-06-04

第2章:変数とデータ型

こんにちはっ、まくるです🐰🌟
今回は、VBAの魔法で大事な道具「変数」について学んでいくよ!


🧪 変数ってなに?

変数とは、 「データを一時的に入れておく箱」 のこと!

たとえば…

Dim name As String
name = "まくる"

この例では、 name という変数に"まくる" を入れてるよ。
箱の名前が「name」 、中身が「まくる」って感じ🎁


🔤 変数の宣言方法

VBAでは、変数を使う前に「箱を用意します!」って宣言する必要があるの。

Dim 変数名 As データ型

例:

Dim age As Integer
Dim price As Double
Dim today As Date

📚 よく使うデータ型一覧

データ型 説明
String 文字列 "Hello" "まくる"
Integer 整数 10 -5
Long 大きな整数 100000
Double 小数 3.14
Boolean 真偽値 True / False
Date 日付 #2025/6/1#

📌 String Integer は特によく使うから覚えてねっ💡


✨ 変数を使った例

こんなマクロを書いてみよう👇

Sub SampleMessage()
    Dim userName As String
    Dim message As String

    userName = "まくる"
    message = "こんにちは、" & userName & "さん!"

    MsgBox message
End Sub

▶ 実行すると…
💬「こんにちは、まくるさん!」ってメッセージが表示されるよ🎉


⚠️ 変数の注意ポイント

  • 使う前に宣言(Dim)する!
  • 型に合わないデータを入れるとエラーに⚡
  • Option Explicit を書くと宣言忘れをチェックできるよ(おすすめ!)

📌 まとめ

  • 変数は「データをしまう箱」!
  • Dim を使って宣言しよう
  • As 型 を忘れずに!

次回は、 「第3章:条件分岐と繰り返し構文」
業務の判断ロジックをVBAでどう書くか?をマスターしようねっ🧠💻

フォローしてくれると嬉しいな🐰✨

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?