公有與私有
Private Sub test() 'privete私有的,這有在這個模塊下可以被調用,相反為 public公有的 MsgBox "aaa" End Sub --------------------- Sub test1() Call test End Sub
還有一個小知識點
Dim i As Integer ‘將i 定義在外面,那么所有的過程及sub都能對i進行調用 ---------------- '在這種情況下,只能在同一個模塊下可以任意調用 Sub test() i = 1 End Sub ---------------- Sub test1() ‘跨過程調用 MsgBox i End Sub
在這種情況下可以跨模塊進行調用,這個謹慎使用,占用內存,且容易出現錯誤
模塊1 Public i As Integer -------------------- Sub test() i = 1 End Sub ------------------- Sub test1() MsgBox i End Sub ------------------- 模塊2 Sub test1() MsgBox i End Sub
如果非要跨模塊取值的話,可以使用這種方法
模塊1 Dim i As Integer ----------------- Sub test() i = 1 End Sub ----------------- Sub test1() MsgBox i End Sub ----------------- Function qbl() '定義一個函數來取值,具有通用性 qbl = i End Function ------------------ 模塊2 Sub test2() MsgBox i End Sub
可以作為小的存儲空間
Dim str As String '定義在外面那么可以作為一個小存儲空間 --------------------- Sub test() str = InputBox("請輸入考生名") End Sub --------------------- Sub test1() '在執行完test后,數據存儲在str,在最后考生結束考試,可以 MsgBox str ‘點擊test1輸出考生名 End Sub
類模塊
這里主要是講理論,去理解什么是類模塊,它的作用是自己可以創建個對象,以及它的屬性等
例如之前我們學習過的,創建表
Sub test() Dim sht, sht1 As Worksheet For Each sht In Sheets If sht.Name = "一月" Then k = k + 1 End If Next If k = 0 Then Set sht1 = Sheets.Add sht1.Name = "一月" End If End Sub
優化成帶參數的過程:減少代碼
Sub test1() Call test("二月") End Sub ------------------------------- Sub test(str As String) Dim sht, sht1 As Worksheet For Each sht In Sheets If sht.Name = str Then k = k + 1 End If Next If k = 0 Then Set sht1 = Sheets.Add sht1.Name = str End If End Sub
另再增加刪除表
Sub test1() 'Call Sadd("二月") Call Sdelete("二月") End Sub ------------------------------ Sub Sadd(str As String) Dim sht, sht1 As Worksheet For Each sht In Sheets If sht.Name = str Then k = k + 1 End If Next If k = 0 Then Set sht1 = Sheets.Add sht1.Name = str End If End Sub ------------------------------ Sub Sdelete(str As String) Dim sht As Worksheet For Each sht In Sheets If sht.Name = str Then Application.DisplayAlerts = False sht.Delete Application.DisplayAlerts = True End If Next End Sub
以此為切入點講解什么是類模塊:在這里面定義的都是方法
類模塊
Sub Sadd(str As String) Dim sht, sht1 As Worksheet For Each sht In Sheets If sht.Name = str Then k = k + 1 End If Next If k = 0 Then Set sht1 = Sheets.Add sht1.Name = str End If End Sub ------------------------------------ Sub Sdelete(str As String) Dim sht As Worksheet For Each sht In Sheets If sht.Name = str Then Application.DisplayAlerts = False sht.Delete Application.DisplayAlerts = True End If Next End Sub ------------------------------------- Sub Add() Sheets.Add after:=Sheets(Sheets.Count) End Sub
在模塊中再使用 :
Sub test() 'sub是個過程,而方法也是個過程,因此可以通過過程來為類模塊定義方法 Dim aaa As New SuperSheets 'aaa.Sadd "3月" aaa.Add End Sub
屬性:分為兩種只讀屬性,寫入屬性
Sub test() Range("a1") = Sheets.Count '只讀屬性,只能讀取 Sheet1.Name = 999 '可以進行賦值的屬性,寫入屬性 End Sub
在模塊里有sub過程和function函數計算過程,對應的在類模塊,sub對應方法,function對應屬性
Sub test() Range("a1") = Scount() End Sub ------------------------- Function Scount() ;不用參數,直接計算出結果 Scount = Sheets.Count End Function
但是在類模塊中聲明一個屬性用的是Property 聲明它有三個值 get:只讀的;let:寫屬性入的屬性,set:子對象,它就相當於模塊中的function函數
Do While循環