文档详情

VBA模块代码.docx

发布:2017-12-17约1.41万字共18页下载文档
文本预览下载声明
我这儿excel2007的版本,编写的VBA程序不知道为什么在excel里保存不了,所以,我写到Word里了,VBA打开程序如下:打开一个excel,按alt+F11打开下面的界面双击sheet1在工作区编写代码就行了,Sub ***()*****End sub是固定的都得有,我这儿只有打印版没有电子版,下面是有一些程序,你试着看看。我介绍一下一些简单的语句,像Dim price as integer 就是定义price 作为整型变量,其他的也类似定义,你可以参照下面的例子。If语句不同c语言,他必须有 then ,end if对于for ,得有 to,next详细的你参考例子,有c语言作为基础,学起来不能够不费力,这儿只是一小部分程序,本来是有介绍图形界面的,但是excel2007不给力,没办法。下面一个例子:单引号是注释。要删除在B列中元素为0的元素所在的行,就是删去2,4,6,8,10行,程序为:Sub dele() dele是我们自己写的,可以改。Dim a As Integer 定义a作为整型变量Dim s As Integer 定义s作为整型变量Range(a1).CurrentRegion.Select 这儿我看了没太懂,我认为应该是选定区域,s = Selection.Rows.Count 计算行数Range(b1).Select 选定b1单元格For a = 1 To sIf ActiveCell.Value = 0 Then 判断是否为零ActiveCell.EntireRow.Delete 删除该行ElseActiveCell.Offset(1, 0).Select 选定同列下一个单元格End IfNextEnd Sub点击运行,得到:Sub ifthenand()Dim units As SingleDim price As IntegerConst string1 = To get a rebate you must buy an addtionalConst string2 = price must equal $7.00price = Range(c1).Valueunits = Range(c2).ValueIf price = 7 And units = 50 Thenrebate = (price * units) * 0.1Range(a4).Value = the rebate is:$ rebateEnd IfIf price = 7 And units 50 ThenRange(a4).Value = string1 50 - units units(s)End IfIf price 7 And units = 50 ThenRange(a4).Value = string2End IfIf price = 7 And units 50 ThenRange(a4).Value = you didnt meet the criteriaEnd IfEnd SubSub whattypeofday()Dim response As StringDim question As StringDim string1 As String, string2 As StringDim mydate As Datequestion = enter the date in the format mm/dd/yyystring1 = weekdaystring2 = weekendresponse = InputBox(question)mydate = Weekday(CDate(response))If mydate = 2 And mydate = 6 ThenMsgBox string1ElseMsgBox string2End IfEnd SubSub structure()Dim num, mystrnum = 1If num = 1 Thenmystr = number equal 1Debug.Print mystrElsemystr = number equal 2End IfEnd SubSub enterdata()Dim cell As ObjectDim string1 As StringOn Error GoTo veryendstring1 = select any cellSet cell = Application.InputBox(prompt:=string1, Type:=8).cell.SelectIf IsEmpty(ActiveCell) ThenMsgBox helloActiveCell.Offset(1, 0).Se
显示全部
相似文档