Presentation is loading. Please wait.

Presentation is loading. Please wait.

EXCEL VBA.

Similar presentations


Presentation on theme: "EXCEL VBA."— Presentation transcript:

1 EXCEL VBA

2 檢視 工具列  表單

3 點選您要使用的物件,放到工作表中

4 點選新增可以開始編寫VBA

5 在VBA程式編輯區編輯程式 程式編輯區

6 1. 輸入: Dim new_date As String ' 宣告變數 new_date = Range("B2").Value MsgBox new_date 2. 存檔

7 執行結果

8 EXCEL VBA設計 變數 物件變數 比較運算 判斷式IF的設計 固定迴圈for的設計 非固定迴圈while的設計 程式的流程

9 變數 變數型態 支援九種變數型態 變數宣告 Dim 變數名稱 AS 變數型態 非強迫性

10 變數型態 名稱 說明 大小 Byte 位元組 1 Integer 整數 2 long 長整數 4 single 單精度浮點 Double
倍精度浮點 8 Currency 大範圍的數值 decimal 小數 14 string 字串 字串長度 date 日期

11 範例 Private Sub 按鈕3_Click() Dim new_date As Date ' 宣告變數
new_date = Range("B2").Value MsgBox new_date End Sub ‘ 代表程式註解 Msgbox 代表出現訊息框 Range指定要讀取的工作表位置

12 Workbooks(“book1.xls”).sheets(1).range(“b2”).value=“xxxx”
物件變數 在程式指定物件的時侯,必須要配合物件的層級來命名。假設目前作用中的活頁簿是”book1.xls”,如果要改變”book1.xls”,第一張工作表的儲存格,程式必須寫成: Workbooks(“book1.xls”).sheets(1).range(“b2”).value=“xxxx” VBA提供一種比較特殊的變數,用來取代物件的層級名稱,這種變數稱為”物件變數”, 例如(cell_1):

13 範例-重新設定b2儲存格的內容 Private Sub 按鈕3_Click()
Set cell_1 = Workbooks(“book1.xls").Sheets(1).Range("b2") cell_1.Value = "物件變數的用法" cell_1.ColumnWidth = 18 cell_1.Font.Bold = True End Sub

14 以儲存格的內容進行計算 Private Sub 按鈕3_Click() Dim num as integer
num = [b2] + [c2] MsgBox "計算結果 = " & num End Sub

15 比較運算 “比較運算”用來比對兩個運算元,再依據比對的結果,傳回”true”/”false”,程式則依據傳回的結果,來決定執行的方法。
Example: Private Sub 按鈕3_Click() num = Range("b2").Value If num > 60 Then MsgBox " 過 關 !!!" Else MsgBox "革命尚未成功" End If End Sub

16 判斷式IF的設計 Private Sub CommandButton1_Click() Dim Result As Integer
IF <條件式> Then [程式一] ELSE [程式二] End if Private Sub CommandButton1_Click() Dim Result As Integer Result = Val(InputBox("input a integer")) If Result > 20 Then MsgBox "Input greater than 20" Else MsgBox "Input less than 20" End If End Sub

17 上頁執行結果

18 固定迴圈for的設計 語法:   For 變數名稱 = 起始值  TO  結束值 [程式區塊] Next 例:  試從 1 加到100。

19 從 1 加到100 Private Sub CommandButton1_Click() Total=0 For k=1 to 100
從 1 加到100 Private Sub CommandButton1_Click() Total=0 For k=1 to 100 total=total +k Next Msgbox “ …+ 100 = ” & total End sub

20 Exit for Private sub commandButton_Click() Dim total as integer
For k=1 to 10000 total=total+k if total > 6000 then exit for End if Next Msgbox “次數=” & k & “總合=” &total End Sub

21 非固定迴圈 Do while loop Private Sub CommandButton1_Click() K=1 Total=0
[程式區塊] loop Private Sub CommandButton1_Click() K=1 Total=0 Do while k<=100 total = total +k K=k+1 Loop Msgbox “ …+100=” & total End Sub

22 再插入一個按鈕[顯示表單]到工作表中

23 1.利用巨集來顯示表單 2.表單與工作表的互動 3. 儲存格與表單的互動 4.將資料連續輸入到儲存格。
建立使用者界面(表單) 1.利用巨集來顯示表單 2.表單與工作表的互動 3. 儲存格與表單的互動 4.將資料連續輸入到儲存格。

24 利用巨集來顯示表單 表單的功能介紹:啟動VBA\插入自訂表單。 工具箱的各種元件介紹: Label TextBox、 ListBox,
ComboBox, Button, Frame, RadioButton, Multipage, TabStrip, ScrollBar

25

26 目標 製作一個簡單的表單, 表單中有兩個CommandButton及一個TextBox, 我們可以輸入資料在TextBox空格中, 再按Button新增資料到Excel資料表中。

27

28

29 修改介面

30 constant Value contents vbModal 1 以強制回應顯示自訂表單 vbModeless 以非強制回應顯示自訂表單
Show方法的語法 Object.show model Object…寫上要顯示出來的自訌表單的物件名稱 Modal…boolean值, 以下表的常數來決定是要以[強制回應]或[非強制回應]來顯示自訂表單. constant Value contents vbModal 1 以強制回應顯示自訂表單 vbModeless 以非強制回應顯示自訂表單

31 example Sub 按鈕5_Click() UserForm2.Show End Sub
Show的預設方法是參數modal, 也就是強制回應

32 [取消]按紐的處理 Private Sub CommandButton2_Click() Unload UserForm2 End Sub
Unload object Object……指移除的物件名稱或控制項名稱

33 將資料輸入到儲存格中 利用變數及運算式來存取儲存格。 定義[模組層次變數], 這個變數能讓多個程序來[參照],這個變數能讓多個程序來參照。

34 Userform2:( 一般 ) – (宣告) Dim CelNo as String Dim Pos As Integer Userform2:Userform-Initialize Private Sub UserForm_Initialize() Pos=1 CelNo=“A” & Pos End Sub

35 Userform2: CommandButton1_Click()
Private Sub CommandButton1_Click() With Worksheets("sheet1") .Range(CelNo) = UserForm2.TextBox1.Text Pos = Pos + 1 CelNo = "A" & Pos .Range(CelNo).Activate End With UserForm2.TextBox1.Text = "" UserForm2.TextBox1.SetFocus End Sub


Download ppt "EXCEL VBA."

Similar presentations


Ads by Google