20
Excel/VBAによるプログラム入門 はじめに VBA(Visual Basic for Applications)Microsoft社により開発されたプログラミング言語で、 おもに業務の自動化・効率化のために利用されています。Microsoft Oceアプリケーショ ンにはVBAの開発環境が付属しているため、特別なソフトのインストールなしにプログラ ミングを手軽に学習することができます。ここでは、Microsoft Oce ExcelVBAを利用し てプログラミングの基礎を理解することを目標とします。 VBAの提供する機能は膨大で、そのすべてを網羅することはこのテーマの範囲を超えてい ます。興味のある方は専門書を参照してください。 最初のプログラム VBAの利用方法を理解するために、簡単なプログラムをつくってみましょう。 まず、好きな場所で新しいExcelファイルを作成し、開いてください。 プログラムの作成には、VBE(Visual Basic Editor)を使います。VBEの起動方法は、Excel 20032007ではすこし手順が異なります。 Excel 2003の場合、「ツール」「マクロ」Visual Basic Editor」と選択します。 ナノ物性科学実験 I テーマA ページ 1 / 20 テキスト

A Programming

Embed Size (px)

DESCRIPTION

a P

Citation preview

  • Excel/VBA

    VBA(Visual Basic for Applications)Microsoft

    Microsoft Oce

    VBA

    Microsoft Oce ExcelVBA

    VBA

    VBA

    Excel

    VBE(Visual Basic Editor)VBEExcel

    20032007

    Excel 2003Visual Basic Editor

    I

    A

    1 / 20

  • Excel 2007Visual Basic

    Excel 2007Oce Excel

    VBE

    I

    A

    2 / 20

  • Sub test()

    Call MsgBox("Hello, world")

    End Sub

    Call MsgBox()()

    Excel

    Excel 2003

    I

    A

    3 / 20

  • Excel 2007

    test

    I

    A

    4 / 20

  • VBA

    Sub ()

    End Sub

    SubEnd Sub

    (

    )

    Sub test()

    Call MsgBox ("Hello, world")

    End Sub

    VBE

    I

    A

    5 / 20

  • Sheet1A1(

    )

    Worksheets(Sheet1).Range(A1).Value = Hello

    Worksheets(Sheet1).Range(A1).Value = 1.5

    A1Hello1.5

    Range(A1:A5)

    Worksheets(Sheet1).Range(A1:A5).Value = Hello

    A1A5Hello

    Worksheets().

    D224Cells

    Worksheets(Sheet1).Cells(2,4)=1.5

    Sheet1D24

    Worksheets(Sheet1).Cells(2,4)=1+3

    + 7+2 9

    - 7-2 5

    * 7*2 14

    / 7/2 3.5

    Mod 7Mod2 1

    () 72 3

    I

    A

    6 / 20

  • ^ 7^2 49

    ()

    ()

    100/5^2-3=1

    VBA

    Call MsgBox (Sqr(2))

    Call MsgBox(Rnd)

    201

    Sin, Cos, Tan

    Sqr

    Exp, Log ()

    Rnd 01

    Abs

    WorksheetFunction.Pi

    VBE

    I

    A

    7 / 20

  • n

    (Integer)

    Sub test()

    n

    Dim n As Integer

    n5

    n = 5

    n

    Call MsgBox(n)

    End Sub

    DimAs

    Dim As

    n=5n5n5

    =

    =

    n=n+2n2(n2()n()

    )

    Dim n As Integer, m As Integer

    Integer (-32768+32767)

    Single (4)

    Double (8)

    String

    I

    A

    8 / 20

  • Boolean True()False()

    Single(4)Double(8)

    Single7Double16Double

    ()n(=100)

    Const n As Integer = 100

    Const As =

    5%

    1.05

    100

    score001, score002,...,score100

    (

    )100

    scores

    I

    A

    9 / 20

  • Dim scores(99) As Integer

    scores(0) = 88

    scores(1) = 91

    scores(2) = 81

    ()

    scores(99) = 38

    Dim ( - 1) As

    ()(-1)10

    (0), (1),...

    100 x 8()a

    Dim a (99, 7) As Double

    (

    )

    ()

    Sub test()

    Dim n As Integer

    Dim a() As Double

    A1

    n = Worksheets("Sheet1").Cells(1, 1)

    ReDim a(n - 1)

    End Sub

    I

    A

    10 / 20

  • ReDim

    Dim () As

    ReDim ( - 1)

    1:

    Sheet1A10

    Sub test()

    Dim x As Double

    Dim msg As String

    x = Worksheets(Sheet1).Cells(1, 1)

    If x < 0 Then

    msg = x & =0

    End If

    Call MsgBox (msg)

    End Sub

    Stringmsg&

    If

    If Then

    ()

    Else

    ()

    End If

    I

    A

    11 / 20

  • Else ...

    ElseIf

    If 1 Then

    1

    ElseIf 2 Then

    2

    Else

    1,2

    End If

    ()

    ,

    = ,

    =

    x05

    If x>=0 And x

  • Sub test ()

    Dim i As Integer

    For i = 1 To 10

    Worksheets(Sheet1).Cells(i, 1) = i

    Next

    End Sub

    i1101ForNext

    iFor Next

    For = To (Step )

    Next

    Step

    1010

    For i = 10 To 0 Step -1

    Exit For10x(9)

    ()i0

    For i = 0 To 9

    If x(i) < 0 Then

    i0 = i

    Exit For

    EndIf

    Next

    ()

    Do While

    Do While

    Do While

    Loop

    I

    A

    13 / 20

  • A1, A2, A3, A4,...,xx

    ()

    Sub test ()

    Dim x As Double

    Dim i As Integer

    x = 1 x

    i = 0

    Do While x > 0

    i = i + 1

    x = Worksheets(Sheet1).Cells(i, 1)

    Loop

    Call MsgBox(i)

    End Sub

    x = 1x(23)

    xDo While

    x>0x

    Do While(

    )

    ()

    CFortran

    A1

    I

    A

    14 / 20

  • Sub test ()

    Dim n As Integer

    n = Worksheets("Sheet1").Range("A1").Value

    Call testinteger(n)

    End Sub

    Sub testinteger(i As Integer)

    Dim msg As String

    If i Mod 2 = 1 Then

    msg = i & " is odd"

    Else

    msg = i & " is even"

    End If

    Call MsgBox(msg)

    End Sub

    testtestinteger

    Sub ()

    Call (1, 2,...)

    End Sub

    Sub (1 As , 2 As ,...)

    End Sub

    I

    A

    15 / 20

  • 2x4a

    Sub test()

    Const n As Integer = 2

    Const m As Integer = 4

    Dim a(n - 1, m - 1) As Integer

    Dim i As Integer, j As Integer

    a

    Call set2darray(a)

    a

    For i = 0 To n - 1

    For j = 0 To m - 1

    Cells(i + 1, j + 1) = a(i, j)

    Next

    Next

    End Sub

    Sub set2darray(x() As Integer)

    Dim i As Integer, j As Integer

    For i = LBound(x, 1) To UBound(x, 1)

    For j = LBound(x, 2) To UBound(x, 2)

    x(i, j) = j + i * 4

    Next

    Next

    End Sub

    ()

    Sub ( () As )

    I

    A

    16 / 20

  • ()

    LBound, UBound

    LBound( [, ])

    UBound( [, ])

    LBound, UBound

    **1

    Sub test()

    Dim i As Integer

    i = 0

    ()

    Call Add1ByRef(i)

    Call MsgBox("i = " & i) i=1

    ()

    Call Add1ByVal(i)

    Call MsgBox("i = " & i) i=1

    End Sub

    Sub Add1ByRef(i As Integer)

    i = i + 1

    End Sub

    Sub Add1ByVal(ByVal i As Integer)

    i = i + 1

    End Sub

    I

    A

    17 / 20

    1 **

  • Add1ByRef, Add1ByVal1

    (i)

    ByValByRef

    Sub (ByRef As )

    Sub (ByVal As )

    (Function)

    Sin(x), Cos(x)VBA

    Function

    a, b Rectangle

    Sub test2()

    Dim a As Double, b As Double, s As Double

    A1, B1

    a = Cells(1, 1)

    b = Cells(1, 2)

    s = Rectangle(a, b)

    Call MsgBox("s = " & s)

    End Sub

    Function Rectangle(a As Double, b As Double) As Double

    Rectangle = a * b

    End Function

    I

    A

    18 / 20

  • Function (1 As , ...) As

    =

    End Function

    **

    ()

    num0num1, num2

    num0

    Dim num0 As Integer

    Sub test1 ()

    num1

    Dim num1 As Integer

    num0

    num0 = 1

    num1 = 1

    End Sub

    Sub test2 ()

    Dim num2 As Integer

    num0 = 2

    num2 = 2

    End Sub

    num0test1, test2

    num1test2(num2test1

    )

    I

    A

    19 / 20

  • (

    )

    DimPublic

    Public As

    DimPrivate

    Dim As

    Private As

    ()

    I

    A

    20 / 20