VB可以在窗體模組的通用宣告段中宣告:
1)宣告一個全域性(非陣列)變數。
Option Explicit
Public a As Double
它可在其它窗體以該窗體名.變數名使用。
https://iknow-pic.cdn.bcebos.com/b3b7d0a20cf431adf00e472d4d36acaf2fdd98c1
2)宣告區域性變數或陣列變數,見下面程式碼。
Public a As Double "宣告全域性變數
Dim b(10) As Single "宣告區域性陣列變數
Dim C As String "宣告區域性變數
Const Pi = 3.141592654 "宣告常量
3)宣告常量,見上述程式碼最後行。
4)宣告Api函式,見以下程式碼。
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
5)宣告Object 資料型別,Object 變數儲存為 32 位(4 個位元組)的地址形式,其為物件的引用。利用 Set 語句,宣告為 Object 的變數可以賦值為任何物件的引用。見以下程式碼,可呼叫Excel電子表格物件。
Dim xlapp As Object
Dim xlBook As Object
Dim xlSheet As Object
Private Sub Command1_Click()
Set xlapp = CreateObject("excel.application")
xlapp.Visible = True
Set xlBook = xlapp.Workbooks.Add
Set xlSheet = xlBook.worksheets(1)
End Sub
VB可以在窗體模組的通用宣告段中宣告:
1)宣告一個全域性(非陣列)變數。
Option Explicit
Public a As Double
它可在其它窗體以該窗體名.變數名使用。
https://iknow-pic.cdn.bcebos.com/b3b7d0a20cf431adf00e472d4d36acaf2fdd98c1
2)宣告區域性變數或陣列變數,見下面程式碼。
Option Explicit
Public a As Double "宣告全域性變數
Dim b(10) As Single "宣告區域性陣列變數
Dim C As String "宣告區域性變數
Const Pi = 3.141592654 "宣告常量
3)宣告常量,見上述程式碼最後行。
4)宣告Api函式,見以下程式碼。
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
5)宣告Object 資料型別,Object 變數儲存為 32 位(4 個位元組)的地址形式,其為物件的引用。利用 Set 語句,宣告為 Object 的變數可以賦值為任何物件的引用。見以下程式碼,可呼叫Excel電子表格物件。
Option Explicit
Dim xlapp As Object
Dim xlBook As Object
Dim xlSheet As Object
Private Sub Command1_Click()
Set xlapp = CreateObject("excel.application")
xlapp.Visible = True
Set xlBook = xlapp.Workbooks.Add
Set xlSheet = xlBook.worksheets(1)
End Sub