Your SlideShare is downloading. ×
0
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Excel vba note 2 presentation
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Excel vba note 2 presentation

313

Published on

教授vba 課程的第二課 :)

教授vba 課程的第二課 :)

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
313
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. EXCEL ADVANCE & VBA Course 2 2014-01-22
  • 2. Review Course 1 ✤ VBA ⽤用途! ✤ 簡單巨集錄制! ✤ VBE 介⾯面操作! ✤ 定義變數
  • 3. 定義變數 ✤ 定義正確的資料類型可以:! ✤ 加快程式運⾏行! 資料類型 簡式 空間 範圍 從 -922,337,203,685,47 7.5808 到 922,337,203,685,477 .5807 Double ✤ ✤ 易於管理程式碼! 定義錯的資料類型可能會:! ✤ ✤ ✤ 程式無法運⾏行! # 8 String $ 字串長度 1 到太約65400 Variant ! (數字) 16 約等同Double Variant ! (字串) 22 加字串長度 等同String 可擴展性低! ⽤用Variant 類型(即不作設定)可免於錯誤但在較多程式碼時運⾏行可能較慢
  • 4. ⼀一般寫法 簡式寫法! 請⾒見表1 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   不同的有效範圍! 終極懶⼈人寫法
  • 5. 對了!我們還學過。︒。︒ 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"     彈出對話框
  • 6. 運算⼦子及比較運算⼦子 運算⼦子其實就是⼀一般的算述運算符號
  • 7. Example 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 可以在即時運算中作測試 ( 必須有 ? 或 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
  • 8. 結合⽂文字運⽤用 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
  • 9. 比較運算⼦子則就是⽤用來作比較⽤用
  • 10. 在比較字串的時候我們有另外⼀一些比較的⽅方式
  • 11. 簡單例⼦子 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 以上[!A-Z] 可以是以多個字元作 為判別選項 或是 多個字元作判別 Sub string_operator2() MsgBox “aM5b” Like “a[L-P]#[!c-e]” End Sub Sub string_operator3() MsgBox "李⼩小明" Like "[李,陳,伍][⼩小,⼤大]*" End Sub
  • 12. 練習時間! 試解釋以下各判別句的意義,再寫出輸出為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
  • 13. 邏輯運算⼦子 邏輯運算⼦子可以看成是⼀一些具有特定功能的運算⼦子
  • 14. Example 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
  • 15. 綜合運⽤用 “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 125 > 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
  • 16. 練習時間! 試計算以下運算式為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 98 = 1 and True Xor “Peter” Like “*r”
  • 17. 內置 FUNCTION ✤ VB 本⾝身跟其他程式語⾔言⼀一樣內置很多常⽤用函數,可以直接採⽤用 ! ✤ 但是由於函數太多,基本上不可能全部背起來,查⽂文件會是⼀一個很好的⽅方法 ! ✤ 按 F1 後 或 在 VBE 說明 -> Microsoft Visual Basic for Application 說明 ! ✤ 查看各不同函數的說明可以在 Visual Basic 程式語⾔言參考 -> 函數
  • 18. 很多時候 我們不可能知道具體的函數名字 所以我們要利⽤用搜尋功能來查找具體的函數。
  • 19. Example
  • 20. 流程控制 if…end if
  • 21. Example
  • 22. 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
  • 23. 練習時間!
  • 24. 流程控制 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
  • 25. 練習時間!

×