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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容