excel vba note 2 presentation

27
2014-01-22 EXCEL ADVANCE & VBA Course 2

Upload: hika-iao

Post on 14-Jan-2015

242 views

Category:

Technology


2 download

DESCRIPTION

教授vba 課程的第二課 :)

TRANSCRIPT

Page 1: Excel vba note 2 presentation

2014-01-22

EXCEL ADVANCE & VBACourse 2

Page 2: Excel vba note 2 presentation

Review Course 1

✤ VBA ⽤用途!

✤ 簡單巨集錄制!

✤ VBE 介⾯面操作!

✤ 定義變數

Page 3: Excel vba note 2 presentation
Page 4: Excel vba note 2 presentation

定義變數

✤ 定義正確的資料類型可以:!

✤ 加快程式運⾏行!

✤ 易於管理程式碼!

✤ 定義錯的資料類型可能會:!

✤ 程式無法運⾏行!

✤ 可擴展性低!

✤ ⽤用Variant 類型(即不作設定)可免於錯誤但在較多程式碼時運⾏行可能較慢

資料類型 簡式 空間 範圍

Double # 8

從 -922,337,203,685,477.5808 到 922,337,203,685,477.5807

String $ 字串長度 1 到太約65400

Variant !(數字) 16 約等同Double

Variant !(字串) 22 加字串長度 等同String

Page 5: Excel vba note 2 presentation

Dim  變量名  As  資料類型    Dim  chinese_name  As  string    '例子  1  定義字串變量

Dim  chinese_name$  

Dim  chinese_name  as  String,  salary  as  Double  

Public  變量名  As  資料類型          '所有模組有限    Private    變量名  As  資料類型      '單個模組有限    Static  變量名  As  資料類型          '單個程序有效  

⼀一般寫法

Dim  chinese_name  

簡式寫法!請⾒見表1

多個寫法

不同的有效範圍!

終極懶⼈人寫法

Page 6: Excel vba note 2 presentation

對了!我們還學過。︒。︒

Range("A1")                          //單一選定    Range("A1:C4")                    //區域選定    Range("A1:C4","D4:F4")    //多區域選定    Range("A1:C4","D4:F4")    //多區域選定  Range("A1").Value              //讀取A1格的值    Range("A1").Value  =  1    //設定A1格的值為  1    Range("A1").Formula              //讀取A1格的公式    Range("A1").Formula  =  "=rand()"    //設定A1格的公式為隨機數  

MsgBox  "Hello"    

彈出對話框

Page 7: Excel vba note 2 presentation

運算⼦子及比較運算⼦子

運算⼦子其實就是⼀一般的算述運算符號

Page 8: Excel vba note 2 presentation

Sub example_operator() MsgBox 1 + 1 MsgBox 1 - 1 MsgBox 10 * 2 MsgBox 10 / 2 MsgBox 11 \ 5 MsgBox 10 ^ 2 MsgBox 11 Mod 5 End Sub

Example

可以在即時運算中作測試 ( 必須有 ? 或 print 在最前 )

Sub example_operator() Debug.Print1 + 1 Debug.Print1 - 1 Debug.Print10 * 2 Debug.Print10 / 2 Debug.Print11 \ 5 Debug.Print10 ^ 2 Debug.Print11 Mod 5 End Sub

Page 9: Excel vba note 2 presentation

Sub more_example_operator() MsgBox “1 + 1 = “ & 1 + 1 MsgBox “1 – 1 = “ & 1 - 1 MsgBox “10 * 2 = “ & 10 * 2 MsgBox “10 / 2 = “ & 10 / 2 MsgBox “11 \ 5 = “ & 11 \ 5 MsgBox “10 ^ 2 = “ & 10 ^ 2 MsgBox “11 mod 5 = “ & 11 mod 5 End Sub

結合⽂文字運⽤用

Page 10: Excel vba note 2 presentation

比較運算⼦子則就是⽤用來作比較⽤用

Page 11: Excel vba note 2 presentation

在比較字串的時候我們有另外⼀一些比較的⽅方式

Page 12: Excel vba note 2 presentation

Sub example_com_operator() MsgBox 1 = 1 End Sub

Sub string_operator() MsgBox "David" Like "D*"

MsgBox "David" Like "D*d" MsgBox "李⼩小明" Like "李*" MsgBox "第⼀一章" Like "第?章" MsgBox "第1節" Like "第#章" MsgBox "F" Like " [!A-Z] "

End Sub

Sub string_operator2() MsgBox “aM5b” Like “a[L-P]#[!c-e]”

End Sub

Sub string_operator3() MsgBox "李⼩小明" Like "[李,陳,伍][⼩小,⼤大]*" End Sub

簡單例⼦子

字串⽐比較可以利⽤用萬⽤用字串的⽅方式對原

字串進⾏行⽐比較,在實際運⽤用中經常會使⽤用到

具體例⼦子如: 以上[!A-Z] 可以是以多個字元作

為判別選項 或是 多個字元作判別

Page 13: Excel vba note 2 presentation

練習時間!

試解釋以下各判別句的意義,再寫出輸出為True的字串的程式 (以Msgbox為輸出⽅方式)

!a*a

a#?[hello,you] Note # 試計算以下字串⽐比較是True 或是 False

!"aBBBa" Like "a*a"

"F" Like "[A-Z]" "a2a" Like "a#a"

"CAT123khg" Like "B?T*"

設定⼀一字串⽐比較,使得能判別三個中⽂文字中第⼀一個中⽂文字只可以是李或姓,第⼆二,三個中⽂文字符任意皆可,根據以上描述,寫出下⽅方空缺的程式碼.

!Sub string_operator3() MsgBox "陳⼩小春" Like ______ End Sub

Page 14: Excel vba note 2 presentation

邏輯運算⼦子

邏輯運算⼦子可以看成是⼀一些具有特定功能的運算⼦子

Page 15: Excel vba note 2 presentation

Sub logic_operator() ‘-------------------------------------------------------- and MsgBox “This is ‘and’ logical operator” MsgBox 10 > 7 and 10 = 10 MsgBox 10 < 7 and 10 = 11 ‘-------------------------------------------------------- or MsgBox “This is ‘or’ logical operator” MsgBox 1 = 1 or 2 = 3 MsgBox 1 = 2 or 2 = 3 ‘-------------------------------------------------------- Not MsgBox “This is ‘Not’ logical operator” MsgBox Not 1 = 2 MsgBox Not 1 = 1 ‘-------------------------------------------------------- Xor MsgBox “This is ‘Xor’ logical operator” MsgBox 1 = 1 Xor 2 = 3 MsgBox 1 = 1 Xor 2 = 2 ‘-------------------------------------------------------- Eqv MsgBox “This is ‘Eqv’ logical operator” MsgBox 1 = 1 Eqv 2 = 2 MsgBox 1 = 1 Eqv 2 = 3 ‘-------------------------------------------------------- Imp MsgBox “This is ‘Imp’ logical operator” MsgBox 1 = 1 Imp 1 = 1 MsgBox 1 = 1 Imp 2 = 3 End Sub

Example

Page 16: Excel vba note 2 presentation

綜合運⽤用

“Students A” <> “Students B” Eqv “Teacher A” <> “Teacher B” = False Eqv Fasle = False

“Students” & 1234 Like “*4” and 1990 >= 1990 = “Students1234” Like “*4” and 1990 >= 1990 = True and 1990 >= 1990 = True and True = True

100 mod 99 = 0 Eqv 12\5 > 10/5 =1 = 0 Eqv 2 > 2 = False Eqv False = True

1 = 1 and 1=100 or 100/9>0 and 100<91 Xor True =[ (1 = 1 and 1=100) or 100/9>0 ] and [ 100<91 Xor True ] = True

Page 17: Excel vba note 2 presentation

練習時間!

試計算以下運算式為Ture 或 False !“DV” & “D” = “DVD” 100 + 1 = 101 or 100 mod 1 >= 0 “Hurry” Like “###ry” or “abc” = “abc” “MacauCivil” = ?????Civil” and “David” Like “*v*” 999/999 = 1 Imp 9\8 = 1 and True Xor “Peter” Like “*r”

Page 18: Excel vba note 2 presentation

內置 FUNCTION

✤ VB 本⾝身跟其他程式語⾔言⼀一樣內置很多常⽤用函數,可以直接採⽤用 !

✤ 但是由於函數太多,基本上不可能全部背起來,查⽂文件會是⼀一個很好的⽅方法 !

✤ 按 F1 後 或 在 VBE 說明 -> Microsoft Visual Basic for Application 說明 !

✤ 查看各不同函數的說明可以在 Visual Basic 程式語⾔言參考 -> 函數

Page 19: Excel vba note 2 presentation
Page 20: Excel vba note 2 presentation

很多時候 我們不可能知道具體的函數名字

所以我們要利⽤用搜尋功能來查找具體的函數。

Page 21: Excel vba note 2 presentation

Example

Page 22: Excel vba note 2 presentation

流程控制 if…end if

Page 23: Excel vba note 2 presentation

Example

Page 24: Excel vba note 2 presentation

Example

Sub sayhello() If Time < 0.5 Then MsgBox “Morning!” If Time >= 0.5 Then MsgBox “Morning!”

End Sub

' 塊形式 Sub sayhello()

If Time <0.5 Then MsgBox “morning”

Else MsgBox “Afternoon”

End if End Sub

Sub sayhello() If Time <0.5 Then

MsgBox “morning” ElseIf Time > 0.75 Then MsgBox “good night” Else MsgBox “Afternoon” End if

Page 25: Excel vba note 2 presentation

練習時間!

Page 26: Excel vba note 2 presentation

流程控制 Select Case

在⾯面多個判斷時採⽤用IF THEN 語句可能會較不清楚且維護較為困難,⽽而Select Case 在需要眾多判斷時相對較清晰

Sub case_test Select Case Time

Case is < 0.5 Msgbox “morning”

Case is > 0.75 Msgbox “good night”

Case else Msgbox “afternoon” End Select

End Sub

對於有數值範圍的值可以⽤用以下⽅方式作判斷

Sub case_test Select Case 2 Case 1 To 3 Msgbox “It is 2” End Select End Sub

Page 27: Excel vba note 2 presentation

練習時間!