You are on page 1of 41

VBA 程式語言 Class #3

110-1 四工一甲、乙
Agenda

• 範圍參照
• 運算子
• 算數運算子
• If 陳述式與比較 / 邏輯運算子
• Select Case 陳述式
範圍 (Range) 參照
指定範圍(物 標準寫法
件)
[D5] Range(“D5”)
Cells(5,4), Cells(5,”D”)

[A1:D5] Range(“A1:D5”)
Range(Cells(1,1):Cells(5,4))
Cells(1,1).Resize(5,4)

[A1:D5, G6:H17] Range(“A1:D5”, “G6:H17”)

表 1 的 [D5] Worksheets(“ 表 1”).Range(“D5”)


Application > Workbook > Worksheet > Range > Cells
應用程式 > 工作簿 > 工作表 > 範圍
> 儲存格
範圍參照

• 使用 Offset 屬性來指定範圍
[ 例 ] Range(“A3”).Offset(4,5) Range(“F7”)
Range(“C3:D4”).Offset( ,-2)
• 使用 Resize 屬性來改變範圍大小
[ 例 ] Range(“A2:C5”).Resize(2,1)
• 使用 Rows 和 Columns 屬性來指定範

[ 例 ] Cells(Rows.Count,3).End(xlUp).Select
• 使用 Union 和 Intersection 方法建立新
範圍
If 陳述式
單向選擇的條件控制結構

If 條件
If 條件式 Then
VBA 指令列 1
Yes / True VBA 指令列 2
Then :
End If
VBA 指令列 1
VBA 指令列 2

No / False
If..Then 陳述式 +Else
雙向選擇的條件控制結構

No / False
If 條件
If 條件式 Then
VBA 指令列 1
Yes / True VBA 指令列 2
Then Else :
Else
VBA 指令列 1 VBA 指令列 1 VBA 指令列 1’
VBA 指令列 2 VBA 指令列 2 VBA 指令列 2’

End If
流程圖範例一

• 若用電在 100 度以內,每度 3 元。


• 100 度以上,超過的部份每度 4 元。
流程圖範例二
• 畫一流程圖說明輸入 A 、 B 、 C 三種,而輸出為
最大值的演算法。

Not structured flowchart


認識運算子
• 指定運算子:「 = 」
[ 例 ] Range("B4").Value=1
Range("B4").Font.Color=vbRed
• 字串連結運算子:「&」
[ 例 ] Cells(2,3).Value=“ 陳 XX”
Cells(3,5).Value=82.5
MsgBox(Cells(2,3)&“ 同學的成績是” &Cells(3,5)&“ 分
” ) →“ 陳 XX 同學的成績是 82.5 分”
• 算數運算子
• 比較運算子
• 邏輯運算子
算數運算子
Ex. A = 5
B=2
space
運算子 意義 例 結果
+ 加 A + B 7
- 減 A - B 3
* 乘 A * B 10
/ 除 A / B 2.5
^ 次方 A ^ B 25
\ 商數 A \ B 2
mod 餘數 A mod B 1
比較 / 邏輯運算子
A=5, B=2, C=“True”, D=“False”
運算子 意義 例 結果
= 運算子左等於右 A=B False
< 運算子左小於右 A<B False
<= 運算子左小於等於右 A<=B False
> 運算子左大於右 A>B True
>= 運算子左大於等於右 A>=B True
<> 運算子左不等於右 A<>B True
And 左右都 True 為 True ,否 C And False
D
則為 False
Or 左右有一 True 即為 C Or D True
True ,兩者皆為 False
才為 False
範例一練習:計算用電費用

• 撰寫函數 Function Fee(N) ,計算並回傳


用電 N 所需費用

[ 例 ] Fee(135) → 440
範例二練習:決定 A 、 B 、 C 之最
大值
• 分別由 A1 、 B1 、 C1 儲存格讀入
A 、 B 、 C 之值
• 根據範例二流程,撰寫決定 A 、 B 、 C 之
最大值
• 以 MsgBox 顯示所得結果:

[ 例 ] “ 最大值為儲存格 B1 之 12.6”
練習題

1. 寫一個函數,判斷輸入是否為正偶數
2. 寫一個函數,回傳函數值
If..Then 陳述式 +Elseif..Then+Else
No / False If ( 條件式 A) Then
If 條件 VBA 指令列 1
A VBA 指令列 2
False :
Yes / True ElseIf
ElseIf ( 條件式 B)
條件 B
Then Then
True VBA 指令列 1’
Then ElseIf VBA 指令列 2’
條件 C
VBA 指令列 VBA 指令列 :
1 1 True False ElseIf ( 條件式 C)
VBA 指令列 VBA 指令列 2 Then Else Then
2 VBA 指令列 1”
VBA 指令列
1 VBA 指令列 2”
VBA 指令列 2 :
Else
VBA 指令列
1 VBA 指令列 1”’
VBA 指令列 2 VBA 指令列 2”’

Select Case 陳述式

= 判斷值 處理
A 1
Select Case (x)
Case 判斷值 1
= 判斷值 處理 處理 1
B 2 Case 判斷值 2
處理 2
…..

….. :
= 判斷值 處理 Case 判斷值 n
n n 處理 n
Case Else
處理其它
其它 預設處 End Select

Select Case 陳述式範例
Select Case Range(“A1”).Value
Case 7
MsgBox(“ 超級大獎” )
Case 10, 20, 30, 40, 50
MsgBox(“ 中獎了” )
Case 1 To 50
MsgBox(“ 沒中” )
Case Is > 50
MsgBox(“ 不在判斷之內的值” )
Case Else
MsgBox(“ 輸入錯誤” )
End Select
Which one would work?
A B C D
X= 0 … 5… 10 … 15 …

Select Case Select Case Select Case


x x x
Case Is <5 Case Is Case Is <5
A >=15 A
Case Is <10 D Case Is
B Case Is >=15
Case Is <15 >=10 D
C C Case Is
Case Else Case Is >=5 >=10
D B C
End Select Case Else Case Else
範例三練習:「運算練習」程式

• 完成如圖計算練習程式,並繪製程式流程

範例三練習:「運算練習」程式說明

• 「 Reset 」按鈕啟動程式重新隨機設定儲存格 A1
~ C1 的資料:
– A1 、 C1 儲存格之值為 1 ~ 20 之整數, C1 為” +, -,
*, /, &” 5 種運算之其中一種
– 將 D1 儲存格 Interior 之顏色清除
• 「檢查答案」按鈕啟動程式檢查 D1 儲存格答案
是否正確
– 答案若是正確,以 MsgBox (" 答案正確 ") 顯示
– 答案若是錯誤,以 MsgBox (" 答案錯誤!正確答案: "
& x)
範例三練習:「計算練習」程式解說
Sub CheckAns()

Sub Reset() Start

Start Dim x As String


[E1].Interior.Pattern
=xlNone

[A1]=1~20
[B1]=“+”/”-”/.. [B1].value
[C1]=1~20
Case “+” Case “-” Case “*”
[E1].Interior.Pattern
=xlNone
x=[A1]+[C1] x=[A1]-[C1] x=[A1]*[C1] ………

True False
x = [E1] ?

MsgBox(“ MsgBox(“ 答案錯誤!


Stop 答案正確 正確答案: " & x)
“) [E1].Interior.Color
Stop =vbRed
Sub reset()
With Cells(1, "E")
.ClearContents
.ClearFormats
End With
Cells(1, "A") = WorksheetFunction.RandBetween(1, 20)
Cells(1, "C") = WorksheetFunction.RandBetween(1, 20)
Select Case WorksheetFunction.RandBetween(1, 5)
Case 1
Cells(1, "B") = "+"
Case 2
Cells(1, "B") = "-" = 判斷值 處
A
Case 3 理1

Cells(1, "B") = "*" = 判斷值 處


Case 4 B 理2

…..

…..
Cells(1, "B") = "/"
Case Else = 判斷值 處
n 理n
Cells(1, "B") = "&"
End Select 其它 預設處

End Sub
Sub checkans()
Dim x As String
Cells(1, "E").ClearFormats If x = Cells(1, "E") Then
Select Case Cells(1, "B") MsgBox (" 答案正確 ")
Case "+" Else
x = Cells(1, "A") + Cells(1, "C") Cells(1, "E").Interior.Color =
'MsgBox (" 加法 ") vbYellow
Case "-" MsgBox (" 答案錯誤!正確答案: "
x = Cells(1, "A") - Cells(1, "C") & x)
'MsgBox (" 減法 ")
End If
End Sub
Case "*"
x = Cells(1, "A") * Cells(1, "C")
'MsgBox (" 乘法 ")
= 判斷值 處
Case "/" A 理1
x = Cells(1, "A") / Cells(1, "C")
= 判斷值
'MsgBox (" 除法 ") B

理2
Case "&"

…..

…..
x = Cells(1, "A") & Cells(1, "C")
= 判斷值 處
'MsgBox (" 字串 &") n 理n

Case Else
其它 預設處
'MsgBox (" 運算符號輸入錯誤! ") 理
End Select
2020 Homework Assignment
Homework #1_ 一乙
• 某百貨零售業者欲進行一項促銷方案,商品是「洗髮護髮
系列」。洗髮精( A )與潤髮乳( B )皆分 500ml 與
250ml 兩種大小瓶裝,大瓶 A 與 B 原售價都是 280 元,
小瓶都是 150 元。
促銷專案如下:
1. 凡購買大瓶 A 一瓶,得以六折優待購買小瓶 A 一瓶。
2. 凡購買大瓶 B 一瓶,得以六折優待購買小瓶 B 一瓶。
3. 購買大瓶 A 或 B 任兩瓶,可免費附贈小瓶 A 或 B 一瓶
( 任選其一 ) 。
4. 購買 A 或 B 任六瓶,總價可再打九五折。
( 大瓶裝以免費折扣使用為優先,不得重複與小瓶再進行
搭售折扣 )

請繪製程式的邏輯流程圖並完成這個方案的價格計算程式。
必須在操作頁面( Excel 工作表)中提供適當的文字說明
與輸入介面,以利使用者進行輸入與結帳。
Homework #1_ 一乙
測試方式:
• 假設 1 :一瓶大瓶 A + 一瓶小瓶 A = 370 元。
• 假設 2 :一瓶大瓶 B + 一瓶小瓶 A = 430 元。
• 假設 3 :二瓶大瓶 B + 一瓶小瓶 A = 560 元。
• 假設 4 :一瓶大瓶 B + 二瓶大瓶 A + 二瓶小瓶 A = 930
元。
• 假設 5 :二瓶大瓶 B + 二瓶大瓶 A + 三瓶小瓶 B = 1270
元。
• 假設 6 :二瓶大瓶 B + 四瓶大瓶 A + 二瓶小瓶 A + 二瓶
小瓶 B = 1738 元。
• 假設 7 :四瓶大瓶 A + 一瓶大瓶 B + 二瓶小瓶 A + 二瓶
小瓶 B = 1558 元。
Homework #1_ 一甲
某餐廳平日 ( 星期一至五 ) 中午收費大人 268 元,
小孩 120 元,晚上和例假日收費大人 368 元,小
孩 150 元,另加 10% 服務費。現在該餐廳舉辦週
年促銷,推出三人同行,一人免費活動,若第三人
可為小孩,則算小孩免費。另外,如果 10 人以上
同行,總價再打 95 折。
請完成一個程式,假設結帳時,平日午、晚間或例
假日為已知,你必須於工作表設計適當介面,以利
使用者輸入,方便結帳。
Homework #1_ 一甲

• 測試方式:
狀況 A1 :平日中午,一大一小用餐。
狀況 A2 :平日中午,五大一小用餐。
狀況 A3 :平日中午,四大用餐。
狀況 A4 :平日中午,二大二小用餐。
狀況 A5 :平日中午,六大五小用餐。
狀況 B1 :平日晚間,一大一小用餐。
狀況 B2 :平日晚間,五大一小用餐。
狀況 B3 :平日晚間,四大用餐。
狀況 B4 :平日晚間,二大二小用餐。
狀況 B5 :平日晚間,六大五小用餐。
Self-Practice Problems
Self-Exercise #1
• Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.

Pseudocode:
• Input the length in feet (Lft)
• Calculate the length in cm (Lcm) by
multiplying LFT with 30
• Print length in cm (LCM)
Self-Exercise #1
Flowchart Algorithm
START

• Step 1: Input L_ft


Input L_ft

L_cm  L_ft x 30 • Step 2: L_cm  L_ft x 30

• Step 3: Print L_cm


Print L_cm

STOP
Self-Exercise #2
Write an algorithm and draw a
flowchart that will read the two
sides of a rectangle and calculate
its area.

Pseudocode
 Input the width (W) and Length (L)

of a rectangle
 Calculate the area (A) by

multiplying L with W
 Print A
Self-Exercise #2
START Algorithm

Input W, L
• Step 1: Input
W, L

ALxW
• Step 2: A  L
x W
Print A

• Step 3: Print A
STOP
Self-Exercise #3
• Write an algorithm that reads
two values, determines the
largest value and prints the
largest value with an
identifying message.

ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: If (VALUE1 > VALUE2) Then
MAX  VALUE1
else
MAX  VALUE2
endif
Step 3: Print “The largest value is”,
MAX
START
Self-Exercise #3
Input
VALUE1, VALUE2

Y is
N
VALUE1>VALUE2

MAX  VALUE1 MAX  VALUE2

Print
“The largest value is”, MAX

STOP
Self-Exercise #4
• Draw the flowchart and write an
algorithm that reads three
numbers and prints the value of
the largest number.
Self-Exercise #4
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX  N1[N1>N2, N1>N3]
else
MAX  N3[N3>N1>N2]
endif
else
if (N2>N3) then
MAX  N2[N2>N1, N2>N3]
else
MAX  N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX
Self-Exercise #5

• Write and algorithm and draw a


flowchart to
a) read an employee name (NAME),
overtime hours worked
(OVERTIME), hours absent
(ABSENT) and
b) determine the bonus payment
(PAYMENT).
Self-Exercise #5

Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid

>40 hours $50


>30 but  40 hours $40
>20 but  30 hours $30
>10 but  20 hours $20
 10 hours $10
Self-Exercise #5

Step 1: Input NAME,OVERTIME,ABSENT


Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT  50
else if (OVERTIME–(2/3)*ABSENT > 30) then
PAYMENT  40
else if (OVERTIME–(2/3)*ABSENT > 20) then
PAYMENT  30
else if (OVERTIME–(2/3)*ABSENT > 10) then
PAYMENT 20
else
PAYMENT  10
endif
Step 3: Print “Bonus for”, NAME “is $”, PAYMENT
Self-Exercise #5

• Flowchart: Draw the flowchart


of the above algorithm?

You might also like