vba学习笔记

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("请输入一个函数:")

MsgBox i, vbYesNoCancel

ActiveCell.Value = i

End Sub

Sub tex2()

'inputbox函数和方法的使用

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

'命名参数:=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

'内置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

'对单元中某一件时间进行条件的判断

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

'复数计算利息account profits

Public Sub HowMuchMoney()

Dim iNumberOfYears As Integer '给一个年数的自变量

'定义一个存钱金额的自变量

Dim cSaving As Currency

'定义一个从1到计iNumberOfYears As Integer数的自变量

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 = ""

' 输出一个对话框输入函数请输入的你的名字并且把它存入变量sname中eo

sName = InputBox("please enter your name:")

' 用一个if语句判断

If sName = "" Then

' 用输出给输出一个带是或者否对话框并且判断结果付给变量iresponse

iResponse = MsgBox("do you wish to quit?", vbYesNo)

' 再判断iresponse自变量是yes或者是no

If iResponse = vbYes Then

' 这时候如果irsponse判断为真,则退出,否则继续判断上面的一个继续输出上面的一个if语句是或者否

Exit Do

End If

End If

'用结束dowhile循环

Loop

End Sub

Public Sub ListofName()

'Dowhile-Loop循环判断

Dim icount As Integer

Dim sName() As String

Dim iResponse As Integer

Dim i As Integer

'给定义的变量附加一个初始值

iResponse = vbYes

'判断条件是否为真

Do While iResponse = vbYes

'给自定义计数变量增加1

icount = icount + 1

'    重新改变定义数组变量的值

ReDim Preserve sName(icount) As String

'    输出一个对话框输出,输入函数提示请输入一个名字,并把输入的结果赋值给数组

sName(icount) = InputBox("please enter a name:")

'判断数组变量是否为空字符

If sName(icount) = "" Then

'    若为空字符输出一个是或者否的对话框,并把结果传递给变量

iResponse = MsgBox("Do you wish to continue:", vbYesNo)

'        判断结果为是或者否

If iResponse = vbYes Then

'            如果结果为是,则提示输入一个名字,并且把名字传递给变量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

你可能感兴趣的:(vba学习笔记)