5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[vba]VBAの代入ステートメントは二種類ある。

Posted at

VBAは文法上値を代入できる普通の代入ステートメントと、オブジェクトの代入ができるSetを使った代入ステートメントと二種類ある。

substitute.vba
Dim x As Long: x = 10
Dim y As Long: y = 9
setObject.vba
Dim wb As Workbook: Set wb = ThisWorkbook
Dim rng As Range: Set rng = ActiveCell

一方は
<変数名> = <値>
もう一方は
Set <変数名> = <オブジェクト>
と、非対称なやりかたで覚えにくいなと思っていた。ところが、実は上の普通の値を代入するステートメントは省略した形だと判明した。
本来はこう書くべきものらしい。

Let.vba
Dim x As Long: Let x = 10
Dim y As Long: Let y = 9

流石に、いちいち値の代入でこんな冗長な表現をとるのは煩わしいので、こんな書き方をする人はいないと思うが、一つ謎が解消された。

出典:パーフェクトExcel VBA pp.80-84

5
1
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
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?