0% found this document useful (0 votes)
44 views

VBA2017 (Student)

This document provides an overview of VBA programming concepts including: 1. How to execute VBA macros using Alt + F11 and writing simple subroutines to manipulate cell values. 2. Demonstrates basic programming structures like if/then statements, for loops, and arrays for tasks like calculating averages and variances of data. 3. Introduces more advanced concepts like matrix multiplication using nested for loops, select case statements, and defining main and sub routines. 4. Provides two examples for homework assignments - generating random trip data and creating an origin-destination table from the trip summaries.

Uploaded by

AsifNawaz
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

VBA2017 (Student)

This document provides an overview of VBA programming concepts including: 1. How to execute VBA macros using Alt + F11 and writing simple subroutines to manipulate cell values. 2. Demonstrates basic programming structures like if/then statements, for loops, and arrays for tasks like calculating averages and variances of data. 3. Introduces more advanced concepts like matrix multiplication using nested for loops, select case statements, and defining main and sub routines. 4. Provides two examples for homework assignments - generating random trip data and creating an origin-destination table from the trip summaries.

Uploaded by

AsifNawaz
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

VBA Programming

1
VBA Programming

Alt + f11
VBA Programming Execution

VBA Programming

Sub test1() 'test1 is just name of this program

Sheet1.Cells(1, 1).Value = 11 '11 is substituted into [ A1 cell]


Sheet1.Cells(1, 2).Value = 12
Sheet1.Cells(2, 1).Value = 21
Sheet1.Cells(2, 2).Value = 22B 'You can display character by "
"

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

Sub test2() ' 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

Sub test21() addition of matrix


X11 = Sheet2.Cells(1, 1).Value
X12 = Sheet2.Cells(1, 2).Value
X21 = Sheet2.Cells(2, 1).Value
X22 = Sheet2.Cells(2, 2).Value
Y11 = Sheet2.Cells(1, 4).Value
Y12 = Sheet2.Cells(1, 5).Value
Y21 = Sheet2.Cells(2, 4).Value
Y22 = Sheet2.Cells(2, 5).Value
Z11 = X11+Y11 6

Sheet2.Cells(1, 7).Value = Z11


End Sub
VBA Programming
-a quadratic equation-
ax bx c 0
2

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

If A = 0 Then = means A is equal to 0 in if then statements.


If B = 0 Then
If C = 0 Then
Sheet4.Cells(2, 1).Value = "Any"
Else
Sheet4.Cells(2, 1).Value = "No Solution"
End If
Else
Sheet4.Cells(2, 1).Value = -C / B
End If
Else
D = B ^ 2 - 4 *A* C
If D >= 0 Then
Sheet4.Cells(2, 1).Value = (-B - D ^ 1 / 2) / (2 * A)
Sheet4.Cells(2, 2).Value = (-B + D ^ 1 / 2) / (2 * A)
Else
Sheet4.Cells(2, 1).Value = "No Solution"
End If
End If
End Sub
Select Statement
Select Case intPoint
Case Is >= 80 if intPoint >=
Select Case D 80
Case Is >0 if D >0 Sheet4.Cells(2, 1).Value =A
Sheet4.Cells(2, 1).Value = Case Is >= 70
Sheet4.Cells(2, 2).Value = Sheet4.Cells(2, 1).Value =
Case Is =0 B+
Sheet4.Cells(2, 1).Value = Case Is >= 60
Case Else Sheet4.Cells(2, 1).Value =
Sheet4.Cells(2, 1).Value = B
End Select Case Else
Sheet4.Cells(2, 1).Value = C
End Select
11
Average and Variance
Sub test6()
N = Sheet6.Cells(1, 1).Value
x1 = Sheet6.Cells(1, 2).Value
x2 = Sheet6.Cells(2, 2).Value
x3 = Sheet6.Cells(3, 2).Value
x4 = Sheet6.Cells(4, 2).Value
x5 = Sheet6.Cells(5, 2).Value
x6 = Sheet6.Cells(6, 2).Value
x7 = Sheet6.Cells(7, 2).Value
x8 = Sheet6.Cells(8, 2).Value
x9 = Sheet6.Cells(9, 2).Value
x10 = Sheet6.Cells(10, 2).Value
Sum = x1 + x2 + x3 + + x10
ave = Sum / N
Sheet6.Cells(10, 1).Value = ave
End Sub 12
Average and Variance (for next)
Sub test6() Sub test62()
N = Sheet6.Cells(1, 1).Value N = Sheet6.Cells(1, 1).Value
x1 = Sheet6.Cells(1, 2).Value
x2 = Sheet6.Cells(2, 2).Value Sum=0
x3 = Sheet6.Cells(3, 2).Value For i= 1 to N
x4 = Sheet6.Cells(4, 2).Value x = Sheet6.Cells(i, 2).Value
x5 = Sheet6.Cells(5, 2).Value Sum = Sum +x
x6 = Sheet6.Cells(6, 2).Value Next I
x7 = Sheet6.Cells(7, 2).Value
x8 = Sheet6.Cells(8, 2).Value
x9 = Sheet6.Cells(9, 2).Value
x10 = Sheet6.Cells(10, 2).Value
Sum = x1 + x2 + x3 + + x10
Ave = Sum / N Ave = Sum / N
Sheet6.Cells(10, 1).Value = ave Sheet6.Cells(10, 1).Value = Ave
End Sub End Sub 13
Average and Variance
Sub test62()
(for next)
N = Sheet6.Cells(1, 1).Value
Sum=0
Sum2=0
For i= 1 to N
x = Sheet6.Cells(i, 2).Value
Sum = Sum +x
Next I
ave = Sum / N
Sheet6.Cells(10, 1).Value = ave
For i= 1 to N
x = Sheet6.Cells(i, 2).Value
Sum2 = Sum2 +(x-ave)^2
Next I
var = Sum2 / N
Sheet6.Cells(11, 1).Value = var
14
4.46? End Sub
For-Next Statement and Array (1)
Sub test5()
Sub test62()
N = Sheet6.Cells(1, 1).Value Dim X(1000) Array
Sum=0 Sum = 0
Sum2=0 Sum2 = 0
For i= 1 to N N = Sheet5.Cells(1, 1).Value
x = Sheet6.Cells(i, 2).Value For i = 1 To N
Sum = Sum +x X(i) = Sheet5.Cells(i, 2).Value
Sum = Sum + X(i)
Next I Next i
Ave = Sum / N
Ave = Sum / N
For i= 1 to N
x = Sheet6.Cells(i, 2).Value For i = 1 To N
Sum2 = Sum2+(X(i) - Ave) ^ 2
Sum2 = Sum2 +(x-Ave)^2 Next i
Next I
Var = Sum2 / N
var = Sum2 / N
Sheet6.Cells(10, 1).Value = Ave Sheet5.Cells(1, 3).Value = Ave
Sheet5.Cells(2, 3).Value = Var
Sheet6.Cells(11, 1).Value = var
End Sub End Sub
15
For-Next Statement and Array (2)
Sub test6() 'Multiplication of Matrix

Dim A(3, 3), B(3, 3), C(3, 3)


N=3

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

Sub addition(x, y, z) 'Subroutine

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

You might also like