全民一起VBA提高篇 第二十一回 事件函数伺机而动,时机成熟一触即发

 

相关知识点

事件处理程序 模块里写的宏,标准宏;事件处理程序必须写在程序发生地;工作簿事件写在Thisworkbook

workbook_open() 工作簿打开运行程序

workbook_beforeclose(cancel as boolean) 括号内参数是执行完本程序,是否取消关闭工作簿的操作,cancel=Ture,就无法关闭

workbook_newsheet 新建工作表即可执行

worksheet_selectionchange 选中单元格事件引发操作

Range.EntireRow: 该单元格所在的行

Range.Entirecolumn: 该单元格所在的列

Private: 作用域,只能被本模块内部的代码调用,变量不指明默认为Private

Public:作用域,可以被其他模块的代码调用,子过程和函数默认为Public

例1 判断谁过生日,工作簿关联,程序写在Thisworkbook,通过名字来判断下面的名字应该改为workbook_open()

Sub checkbrithhay()

     Dim today as date,d as data,i as long

     Today=date

     i=6

     Do while Trim(cells(i ,2))<>””

            d=cells(i,6)

            If month(d)=month(today) and day(d)=day(today) Then

                   Msgbox”今天是”&cells(i,3)&cells(i,5)&”的生日”

            End If

            i=i+1

     Loop

End Sub

 

例2 新建表格自动按照模板新格式创建

Private Sub workbook_newssheet(ByVal sh as object)

     Sh.Range(“B2”)=”工号”

Sh.Range(“D2”)=”姓名”

Sh.Range(“F2”)=”年龄”

With sh

       .range(“B4”)=”参与项目”

       .range(“c4”)=”主要职责”

       .range(“d4”)=”有效工时”

       .range(“e4”)=”业绩评价”

       .range(“f4”)=”进入日期”

       .range(“g4”)=”推出日期”

End With

Sh.range(“b2,d2,f2,b4:g4”).font.bold=True

With sh.range(“B4:G30”)

       .Borders(xlEdgeLeft).LineStyle=xlContinuous

       .Borders(xlEdgeTop).LineStyle=xlContinuous

       .Borders(xlEdgeBottom).LineStyle=xlContinuous

       .Borders(xlEdgeRight).LineStyle=xlContinuous

       .Borders(xlInsideVertion).LineStyle=xlContinuous

       .Borders(xlInsideHorizontal).LineStyle=xlContinuous

End With

     End Sub

     例3 选中单元格背景色变色

     Private Sub worksheet_selectionchange(ByVal Target as Range)

             

       Cells.Interior.Color=xlNone ‘先让execl背景色透明

Target.Interior.Color=vbred ‘选中单元格即可变色,画画效果

     End Sub

     例4 单独工作表选中的单元格十字高亮

     Private Sub worksheet_selectionchange(ByVal Target as Range)

              Cells. Interior.Color=xlNone ‘先让execl背景色透明

Target. EntireRow.Interior.Color=vbred ‘选中单元格行变色

Target. EntireColumn.Interior.Color=vbred ‘选中单元格列变色

     End Sub

例5 单独工作表选中的单元格十字高亮

模块1

Sub highlight(r as Range)

               Cells. Interior.Color=xlNone ‘先让execl背景色透明

r. EntireRow.Interior.Color=vbCyan‘选中单元格行变色

r. EntireColumn.Interior.Color=vbCyan‘选中单元格列变色

 End Sub

工作表调用

Private Sub worksheet_selectionchange(ByVal Target as Range)

     Call 模块1.highlight(Target)

End Sub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值