Option Explicit
Public Sub VBF1()
MsgBox "this is my frist programe1"
End Sub
Public Sub MBExercise()
Dim iResult As Integer
iResult = MsgBox("select button", vbYesNoCancel)
MsgBox iResult
End Sub
Sub tex1()
Dim i As Integer
i = InputBox("請輸入一個函數(shù):")
MsgBox i, vbYesNoCancel
ActiveCell.Value = i
End Sub
Sub tex2()
'inputbox函數(shù)和方法的使用
Dim iResult As Integer
iResult = Application.InputBox("please enter your favorite number:", , , , , , , 1)
'等價于
iResult = Application.InputBox("please enter your favorite number:", Type:=1)
MsgBox iResult
ActiveCell.Value = iResult
'命名參數(shù):=1的作用可以省略前面的逗號
End Sub
Sub stringstuff()
Dim sName As String
Dim slongtext As String
sName = InputBox("please enter your name:")
slongtext = "this is an example of using string"
slongtext = slongtext & "concatenation to combine long"
slongtext = slongtext & "string." & vbNewLine
slongtext = slongtext & "the vbnewline constant allows you"
slongtext = slongtext & "to add line breaks to your string."
MsgBox slongtext
End Sub
Public Sub YourInfo()
Dim Name As String
Dim Place As String
Dim Age As Integer
Dim N_P_A As String
Name = InputBox("請輸出你的名字:")
Place = InputBox("請輸出你居住的城市:")
Age = InputBox("請輸出的年齡:")
N_P_A = Name
N_P_A = N_P_A & vbNewLine
N_P_A = N_P_A & Place
N_P_A = N_P_A & vbNewLine
N_P_A = N_P_A & Age
MsgBox N_P_A
End Sub
Public Sub Commission()
Dim sngCommission As Single
If Range("b2") = "No" Then
sngCommission = 0.2
If Range("b3").Value >= 5 And Range("b3") < 10 Then
sngCommission = sngCommission + 0.01
ElseIf Range("b3").Value >= 10 Then
sngCommission = sngCommission + 0.02
End If
If Range("b1").Value = "furniture" Then
sngCommission = sngCommission + 0.01
End If
Else
sngCommission = 0.01
End Sub
Public Sub Scorechoose()
Select Case Range("B1")
Case Is >= 90
MsgBox "you got a on the test."
Case 80 To 89
MsgBox "you got b on the test."
Case 70 To 79
MsgBox "you got c on the test."
Case 60 To 69
MsgBox "you got d on the test."
Case Else
MsgBox "you failed."
End Sub
Public Sub Priceshipping()
Dim Cshipping As Currency
Select Case State
Case "new york"
Cshipping = 5#
Case "georgia", "southcarolina ", "ohnio"
Cshipping = 4#
Case "Florida", "texas"
Cshipping = 3#
Case "alabama", "wa", "ca", "llli"
Cshipping = 2#
Case Else
Cshipping = 1
End Sub
Public Sub SaveNow()
Dim iResponse As Integer
iResponse = MsgBox("Do you wish to save your work?", vbYesNo)
If iResponse = vbYes Then
Application.Dialogs(xlDialogPrint).Show
'內(nèi)置excel打印對話框
End If
End Sub
Public Sub ClickTest()
Dim i As String
Dim a As String
i = MsgBox("Do you wish to continue?", vbOKCancel)
If Range("a1") = "確定" Then
MsgBox "確定"
Else
MsgBox "取消"
End If
End Sub
'對單元中某一件時間進(jìn)行條件的判斷
Public Sub Discount()
Dim State() As Single
Select Case Range("A1")
Case "A"
Range("b1").Value = 0.05
Case "B"
Range("b1").Value = 0.1
Case "C"
Range("b1").Value = 0.15
Case "D"
Range("b1").Value = 0.2
Case Else
End Select
End Sub
Public Sub BeepMe()
Dim iCounter As Double
For iCounter = 1 To 20
Beep
Next
End Sub
'復(fù)數(shù)計算利息account profits
Public Sub HowMuchMoney()
Dim iNumberOfYears As Integer '給一個年數(shù)的自變量
'定義一個存錢金額的自變量
Dim cSaving As Currency
'定義一個從1到計iNumberOfYears As Integer數(shù)的自變量
Dim iCounter As Integer
cSaving = InputBox("Enter amount you are placing in the account:")
iNumberOfYears = InputBox("Enter number of years you are saving the money:")
For iCounter = 1 To iNumberOfYears
cSaving = cSaving * 1.1
Next
MsgBox "in" & iNumberOfYears & "years you'll have" & Format(cSaving, "0.00") & "dollars"
End Sub
Public Sub EnterName()
Dim sName As String
Dim iResponse As Integer
'變量初始化
sName = ""
' 判斷sname變量是否為空
Do While sName = ""
' 輸出一個對話框輸入函數(shù)請輸入的你的名字并且把它存入變量sname中eo
sName = InputBox("please enter your name:")
' 用一個if語句判斷
If sName = "" Then
' 用輸出給輸出一個帶是或者否對話框并且判斷結(jié)果付給變量iresponse
iResponse = MsgBox("do you wish to quit?", vbYesNo)
' 再判斷iresponse自變量是yes或者是no
If iResponse = vbYes Then
' 這時候如果irsponse判斷為真,則退出弓柱,否則繼續(xù)判斷上面的一個繼續(xù)輸出上面的一個if語句是或者否
Exit Do
End If
End If
'用結(jié)束dowhile循環(huán)
Loop
End Sub
Public Sub ListofName()
'Dowhile-Loop循環(huán)判斷
Dim icount As Integer
Dim sName() As String
Dim iResponse As Integer
Dim i As Integer
'給定義的變量附加一個初始值
iResponse = vbYes
'判斷條件是否為真
Do While iResponse = vbYes
'給自定義計數(shù)變量增加1
icount = icount + 1
'? ? 重新改變定義數(shù)組變量的值
ReDim Preserve sName(icount) As String
'? ? 輸出一個對話框輸出者祖,輸入函數(shù)提示請輸入一個名字,并把輸入的結(jié)果賦值給數(shù)組
sName(icount) = InputBox("please enter a name:")
'判斷數(shù)組變量是否為空字符
If sName(icount) = "" Then
'? ? 若為空字符輸出一個是或者否的對話框古程,并把結(jié)果傳遞給變量
iResponse = MsgBox("Do you wish to continue:", vbYesNo)
'? ? ? ? 判斷結(jié)果為是或者否
If iResponse = vbYes Then
'? ? ? ? ? ? 如果結(jié)果為是,則提示輸入一個名字,并且把名字傳遞給變量sname
sName(icount) = InputBox("please enter a name:")
Exit Do
End If
End If
Loop=
For i = 1 To icount - 1
MsgBox ("name#" & i & "is" & sName(i))
Next
End Sub
Public Sub WorkbookEXAMPLE()
Dim WB As Workbook
Set WB = Workbooks.Add
WB.Worksheets("sheet1").Range("a1").Value = 100
WB.SaveAs "hello"
WB.Close
MsgBox "this wb is closed."
End Sub