Sub variantdemo()
myvar = "123"
myvar = myvar + myvar
myvar = "answer:" & myvar
MsgBox myvar
End Sub
測試結果:
dim 聲明數據類型
Sub variantdemo()
Dim x As Integer
Dim first As Long
Dim interestrate As Single
Dim todaysdate As Date
Dim username As String
Dim myvalue
'-the procedure's code goes here-
End Sub
Integer(整數)
模塊所有過程均可以使用變量
Dim currentvalue As Long
Sub procedure1()
End Sub
Sub procedure2()
End Sub
均有權訪問 currentvalue
靜態變量 static
sub mysub()
static counter as long
end sub
常量 const語句 常量聲明某種特定的數據類型
const numquarters as integer =4
const rate=0.0725,period =12
const modname as string="budget macros"
public const appname as string="budget application"
對常量進行賦值時不被允許的。
字符串規定:
dim mystring as string *50 最大五十
dim yourstring as stting
賦值語句:
進行數學計算並將結果賦給某個變量或對象
x=1
x=x+1
x=(y*2)/(z*2)
fileopen =true
fileopen =not fileopen
rang("theyear").value=2010「
數組 一組擁有相同名稱的同類元素。
終於找到我出問題的部分了 哇咔咔
聲明數組
dim 或 public
dim myarry(1 to 100)as integer
必須指定上界
dim myarry(100)as integer
效果一樣
聲明多維數組
dim myarry(1 to 10,1to 10)as integer
10*10 矩陣
聲明動態數組
dim myarray()as integer
dim myarray (1 to X)
對象變量:
書上是這樣寫的
然而我無法運行 !!!!!
經過我百度和浪費時間後,我得到了正確的個格式:
Function noobjvar()
Dim mycell As Range
Set mycell = Worksheets("sheet2").Range("A1")
mycell.Value = 124
mycell.Font.Bold = True
mycell.Font.Italic = True
mycell.Font.Size = 14
mycell.Font.Name = cambria
End Function
開始語句不一樣!
用戶自定義數據類型
需要創建一個customerinfo 自定義數據類型。
dim customers(1 to 100) as customerinfo
內置函數
Sub showroot()
Dim myvalue As Double
Dim squareroot As Double
myvalue = 25
squareroot = Sqr(myvalue)
MsgBox squareroot
End Sub
sqr 函數等同於excel 中的sqrt工作表函數。
emmmmm 書上的和我寫的 我他娘 具體原因我也不知道
msgbox 是最有的vba函數之一
簡單自定義對話框的一個很好替代品。顯示一個對話框,代表了用戶對該對話框的反應。
msgbox 函數正式的語法包含5個參數。(方括號中的參數是可選的)
msgbox(prompt [,buttons] [,tittle] [, helpfile,context])
prompt (必需的):該消息顯示在彈出的對話框中。
buttons (可選的):指定在消息框長出現哪些按鈕和圖標的值。使用內置常量,如 vbYesNo
title( 可選的):出現在消息框標題欄中的文本,默認值為Microsoft excel
helpfile (可選的)與消息框關聯的幫助文件的名稱。
context (可選的) 幫助主題上下文id 它表示要顯示的某個特定的幫助主題,如果使用了context參數,還必須使用helpfile參數
處理對象和集合:
VBA 提供了兩個重要的結構,這些結構可以簡化對象和集合的處理。
with-end with 結構
for each-next 結構
with-end with 結構:允許在單個對象上執行多項操作。
Sub changefont1()
Selection.Font.Name = "cambria"
Selection.Font.Bold = True
Selection.Font.Italic = True
Selection.Font.Size = 12
Selection.Font.Underline = xlUnderlineStyleSingle
Selection.Font.ThemeColor = xlThemeColorAccent1
End Sub
可以直接替換成下文格式:
Sub changefont2()
With Selection.Font
.Name = "cambria"
.Bold = True
.Italic = True
.Size = 12
.Underline = xlUnderlineStyleSingle
.ThemeColor = xlThemeColorAccent1
End With
End Sub
for each-next 要對集合所有對象求值並在特定條件下採取動作
語法結構:
運行時顯示 類型不匹配
Sub countsheets()
Dim cnt As Integer
Dim win As Window
cnt = 0
For Each win In Windows
If Not win.Visible Then cnt = cnt + 2
Next win
MsgBox cnt & " hidden windows."
End Sub
for each-next 結構常見一個用法是遍歷單元格區域中的所有單元格。
Sub selectnegative()
Dim cell As Range
For Each cell In Range("1:1")
If cell.Value < 0 Then
cell.Select
Exit For
End If
Next cell
End Sub
插入一個if-then 結構檢查單元格是否為負值 ,是 結束循環
控制代碼的執行。
goto 語句
VBA只有在一種情況下必須使用Goto語句 那就是進行錯誤處理時。
if-then 結構
基本語法:
if condition then true_instructions [else false_instructions]
excel 中 if(規則,是,否) 【類比記憶】
Sub greetmel()
If Time < 1.5 Then MsgBox "good morning"
End Sub
Sub greetme2()
If Time < 0.5 Then MsgBox "good morning"
If Time >= 0.5 Then MsgBox "good afternoon"
End Sub
可以修改成 if-then else 句子
Sub greetme3()
If Time < 0.5 Then MsgBox "good morning" Else MsgBox "goodafternoon"
End Sub
高階版本:
VBA的iif函數
語法格式:iif(expr,truepart,falsepart)
exper(必需的) 需要求值的表達式
truepart(必需的) 當exper返回為true時,返回的值或表達式。
falsepart (必需的) 當expr 返回為false時,返回的值或表達式。
select case 結構
Function greetme()
Dim meg As srring
Select Case Time
Case Is < 0.5
msg = "good morning"
Case 0.5 To 0.75
msg = "good afternoon"
Case Else
msg = "good evening"
End Select
MsgBox msg
End Function
for-next 循環
for counter =starts to end [step stepval ]
[instructions]
[exit for]
[instructions]
next [counter]
Sub sumsquareroots()
Dim sum As Double
Dim count As Integer
sum = 0
For count = 1 To 100
sum = sum + Sqr(count)
Next count
MsgBox sum
End Sub
do while 循環 :只有在滿足指定條件(true)時才會執行 do while 循環。
語法格式:
do [while conditon]
[instruction]
[exit do]
[instructions]
loop '可能從來不執行循環內容
或
do
[instruction]
[exit do]
[instructions]
loop [while conditon] 『至少執行一次循環內容
do until 語句 一直執行循環,直至循環條件值未true
語法格式:
do [until condition]
[instructions]
[exit do]
[instructions]
loop
或
do
[instructions]
[exit do]
[instructions]
loop [until condition]