跟一个从零开始学excel vba的新手的对话过程(简单程序实例)

 请教编写vba程序要用到函数吗?

函数和程序是姐妹关系,在程序中经常用到函数来实现某些功能

能举个例子说明吗?

比如:if [a1].value="编程太难"  then msgbox "a1单元格是“编程太难”"

上面程序中的 if 就是个最基本函数,用于条件判断

看不懂,好像是天书,是什么意思啊?

上面代码意思是:如果a1单元格中输入的是编程太难 ,那么就会弹出一个消息框,显示

 a1单元格是“编程太难“  这句话

如果不是“编程太难“,就不会弹出这个框框

请问上面的代码如何实现功能?

你可以在工作表里面添加一个按钮,用菜单的的控件工具箱的一个 按钮 形状的东东,单击一下然后画出一个 命令按钮

然后双击按钮,进入代码编辑 界面,把那句代码复制过来,放在中间位置,如下:

Private Sub CommandButton1_Click()

if [a1].value="编程太难"  then msgbox "a1单元格是“编程太难”"

End Sub

下面进行测试:

在a1单元格输入”编程太难“,然后点击按钮,看一下出现了什么。

请问,我删除了单元格汉字,怎么没有 消息框 呢?

我开始讲过,if函数本身就是条件判断,如果条件为真,即成立,就会执行后面的代码

你删除变成空格,条件就不成立了呀,当然没框 了

我终于做出了第一个程序。。。。。。。。。。。。。

接着你可以把按钮改名,办法是 控件工具箱上第一个按钮(绿色的),点击一下进入可编辑状态

看到了,按钮周围出现了一些小圆圈。。。。

然后 按钮上点击右键,选择  属性

找到caption 的这个属性,双击CommandButton1改为”编程不难,不会炒鱿鱼的“

请教如果a1单元格是1,就跳出“是1”如果a1单元格是2,就跳出“是2”  ,怎么办?

你按照上面的方法,再做第二个按钮,输入复制下面代码:


aa = [a1].Value
If [a1] = 1 Then MsgBox "是:" & aa
If [a1] = 2 Then MsgBox "是:" & aa

把按钮改名为”单元格值的判断“

麻烦您解释一下,aa=[a1].value 是什么意思?

aa=[a1].value 是什么意思:

这里的aa是个变量,表示你在a1单元格中输入的值,接着下面就能引用变量的值了

msgbox "是:aa", 这样写可以吗?

不可以的

 MsgBox "是:aa"   结果只能显示      是:aa
变量如果包括在双引号之内,就不再是变量,只是普通字符aa

 "是:" & aa

这里的&起到了连接作用,左边是字符,右边是变量

而变量aa的值就是你在单元格输入的值,而不是aa字符

请问:比如单元格a1 ,这里的行次1我想让它成为可变, 怎么办?

这里你先知道单元格的几种表示方法:

同样是a1单元格,有下面 几种常用的表示方法:

cells(1,1)

range("a1")

[a1]

我先介绍这三种,它们都表示a1单元格,只是形式不同

太好了,我过去就是不知道怎么引用单元格,只知道a1 、 b1等。。。。。。

cells(行,列),如

cells(2,3)

表示第2行,第3列,即C2单元格

现在讲怎么用变量表示其中的行或列

先看:认识一下for循环赋值:

for aa=1 to 10

cells(aa,1).value=aa

next

老师,这个代码什么作用?

你先另外做个按钮,把代码复制过去,看一下达到什么效果。

单元格自动出现了10个数,太灵验了,这是我经常碰到但束手无策的。。。。。。

这就是今后常用到的循环赋值在A列前10个单元格自动产生数字1-10

那请老师介绍一下代码的意思?

for aa=1 to 10            '1是初值,10是终值

cells(aa,1).value=aa

next

其中for循环结构是  for 变量=初始值 to 终值 step 步长

这里的步长没写,默认为1

步长是什么意思?

如果我改为 for aa=1 to 10 step 2

加上步长2,则单元格产生数字就不连续了,这里的aa值就不是1,2,3,4,...10

而是1,3,5,7,9

你在学校里肯定会跳远吧,如果步长值 是2,则是二级跳,如果是3,则是三步一跳

补充一下 for aa=1 to 10 step 2  只会在a1,a3,a5,a7,a9这几个单元格自动产生值,即隔行赋值

for aa=1 to 10

cells(aa,1).value=aa

next

这里的红色代码是赋值语句,cells(aa,1)表示行是变量aa,列是1,即第1列

其中的aa变量值、个数已经由for aa=1 to 10限定,共有10个变量值

注意for循环后面应该有next ,即通俗讲,next是执行下一个变量值的意思,比如第一次循环时,aa是1,next以后aa变量是2,依次类推

老师,对了,我不用cells表示,用range?

用range表示方法是  range("a" & aa)

例如:

Private Sub CommandButton5_Click()
For aa = 1 To 10 Step 2

Range("a" & aa) = aa
Next
End Sub

 

OfficeTips Home || VBA Section || General Section || Download Section || Privacy Policy Useful PowerPoint VBA code snippets More Sharing Services Share | Share on gmail Share on google Share on facebook Share on twitter Determine the current slide in the Slide View mode: Sub SlideIDX() MsgBox "The slide index of the current slide is:" & _ ActiveWindow.View.Slide.SlideIndex End Sub Determine the current slide in Slide Show mode: Sub SlideIDX() MsgBox "The slide index of the current slide is:" & _ ActivePresentation.SlideShowWindow.View.Slide.SlideIndex End Sub Difference between SlideIndex property and SlideNumber property: The SlideIndex property returns the actual position of the slide within the presentation. The SlideNumber property returns the PageNumber which will appear on that slide. This property value is dependent on "Number Slide from" option in the Page Setup. Go to Page Setup and Change the value of "Number Slide from" to 2 and then while on the 1st slide in Slide View run the following Macro Sub Difference() MsgBox "The Slide Number of the current slide is:" & _ ActiveWindow.View.Slide.SlideNumber & _ " while the Slide Index is :" & _ ActiveWindow.View.Slide.SlideIndex End Sub Macro to exit all running slide shows: Sub ExitAllShows() Do While SlideShowWindows.Count > 0 SlideShowWindows(1).View.Exit Loop End Sub Code to refresh current slide during the slide show: Sub RefreshSlide() Dim lSlideIndex As Long lSlideIndex = SlideShowWindows(1).View.CurrentShowPosition SlideShowWindows(1).View.GotoSlide lSlideIndex End Sub Code to reset animation build for the current slide during the slide show: Sub ResetSlideBuilds() Dim lSlideIndex As Long lSlideIndex = SlideShowWindows(1).View.CurrentShowPosition SlideShowWindows(1).View.GotoSlide lSlideIndex, True End Sub Insert a slide after current slide Sub InsertSlide() Dim oView As View With ActivePresentation.Slides Set oView = ActiveWindow.View oView.GotoSlide .Add(oView.Slide.SlideIndex + 1, _ ppLayoutTitleOnly).SlideIndex Set oView = Nothing End With End Sub Copyright 1999-2011 (c) Shyam Pillai. All rights reserved.
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值