VBA2017 (Student)
VBA2017 (Student)
1
VBA Programming
Alt + f11
VBA Programming Execution
VBA Programming
End Sub
It starts from Sub ****() to ends at End Sub.
A=B : not A equals to B but Substitute B into A
VBA Programming
Addition
A = Sheet2.Cells(1, 1).Value
B = Sheet2.Cells(1, 3).Value
C = A+ B
Sheet2.Cells(1, 5).Value = C
End Sub
Addition of Matrix
b b 2 4ac
x
2a
Sub test3() ' a quadratic equation
A = Sheet3.Cells(1, 1).Value
B = Sheet3.Cells(1, 2).Value
C = Sheet3.Cells(1, 3).Value
x1 = (-B - (B ^ 2 - 4 * A * C) ^ (1 / 2)) / (2 * A)
x2 = (-B + (B ^ 2 - 4 * A * C) ^ (1 / 2)) / (2 * A)
Sheet3.Cells(2, 1).Value = x1
Sheet3.Cells(2, 2).Value = x2
End Sub
VBA Programming
-a quadratic equation-
c0 x any
ax bx c 0
2
b0
c0 x none
a0
c
b0 x
b
b 2 4ac 0 b b 2 4ac
x
2a
b
a0 b 2 4ac 0 x
2a
b 2 4ac 0 x none
If A = 0 Then If A = 0 Then
Sheet4.Cells(2, 1).Value = A=0 Sheet4.Cells(2, 1).Value = A=0
End If Else
Sheet4.Cells(2, 1).Value = A0
End If
If A = 0 Then
If B = 0 Then
Sheet4.Cells(2, 1).Value = A=0, B=0
End If
End If
If A = 0 Then
If B = 0 Then
Sheet4.Cells(2, 1).Value = A=0, B=0
Else
Sheet4.Cells(2, 1).Value = A=0, B 0
End If
Else
If B = 0 Then
Sheet4.Cells(2, 1).Value = A 0, B=0
Else
Sheet4.Cells(2, 1).Value = A 0, B 0
End If
End If
Sub test4() ' a quadratic equation
A = Sheet4.Cells(1, 1).Value
B = Sheet4.Cells(1, 2).Value
C = Sheet4.Cells(1, 3).Value
For i = 1 To N
For j = 1 To N
A(i, j) = Sheet6.Cells(i, j).Value
B(i, j) = Sheet6.Cells(i, j + 4).Value
C(i, j) = 0
Next j
Next i
For i = 1 To N For i = 1 To N
For j = 1 To N For j = 1 To N
For k = 1 To N
C(i, j) = C(i, j) + A(i, k) * B(k, j) C(i, j) = A(i, j) + B(i, j)
Next k
Sheet6.Cells(i, j + 8).Value = C(i, j) Sheet6.Cells(i, j + 8).Value = C(i,
j)
Next j Next j
Next i Next i
16
End Sub
Multiplication of Matrix
c11=a11b11+a12b21+a13b31
c23=a21b13+a22b23+a23b33
cij=ai1b1j+ai2b2j+ai3b3j
3
cij aik bkj
k 17
Main-routine and Sub-routine
Sub test7() ''Main routine
A = Sheet7.Cells(1, 1).Value
B = Sheet7.Cells(1, 2).Value
Call addition(A, B, C)
Sheet7.Cells(1, 3).Value = C
End Sub
z=x+y
End Sub
18
Home Work
Produce Person Trip Data by Random Number.
Make a program that sum-ups the above trips
and makes an OD table.
1 2 3 4 5 Oi
1
2
3 xij
4
5
Dj
19