按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
三行,而在第一列时
ElseIf cell。Row 》 3 And celllumn = 1 Then
cell。Value = cell。Offset(…1; 6)。Value + 1
' Stop when the last day of the month has been entered。 当遇到当月的最后一天
时,停止
If cell。Value 》 (FinalDay … StartDay) Then
cell。Value = 〃〃
Next ' Exit loop when calendar has correct number of days
' shown。 当日历显示了正确的数字时,退出循环
Exit For
End If
End If
278
… 页面 295…
' Create Entry cells; format them centered; wrap text; and border
' around days。 创建输入单元格,居中,自动换行,并在每日周围设置边框
For x = 0 To 5
Range(〃A4〃)。Offset(x * 2; 0)。EntireRow。Insert
With Range(〃A4:G4〃)。Offset(x * 2; 0)
。RowHeight = 65
。HorizontalAlignment = xlCenter
。VerticalAlignment = xlTop
。WrapText = True
。Font。Size = 10
。Font。Bold = False
' Unlock these cells to be able to enter text later after
' sheet is protected。 解开这些区域的锁定,以供将来输入文本
。Locked = False
End With
' Put border around the block of dates。 在每日周围设置边框
With Range(〃A3〃)。Offset(x * 2; 0)。Resize(2; _
7)。Borders(xlLeft)
。Weight = xlThick
lorIndex = xlAutomatic
End With
With Range(〃A3〃)。Offset(x * 2; 0)。Resize(2; _
7)。Borders(xlRight)
。Weight = xlThick
lorIndex = xlAutomatic
End With
Next
Range(〃A3〃)。Offset(x * 2; 0)。Resize(2; 7)。BorderAround _
Weight:=xlThick; ColorIndex:=xlAutomatic
If Range(〃A13〃)。Value = 〃〃 Then Range(〃A13〃)。Offset(0; 0) _
。Resize(2; 8)。EntireRow。Delete
' Turn off gridlines。 关闭网格线
ActiveWindow。DisplayGridlines = False
' Protect sheet to prevent overwriting the dates。 保护工作表,防止覆盖日期
ActiveSheet。Protect DrawingObjects:=True; Contents:=True; _
Scenarios:=True
' Resize window to show all of calendar (may have to be adjusted
' for video configuration)。 设置窗口大小以显示完整日历
ActiveWindow。WindowState = xlMaximized
ActiveWindow。ScrollRow = 1
' Allow screen to redraw with calendar showing。 允许屏幕重绘日历显示
Application。ScreenUpdating = True
' Prevent going to error trap unless error found by exiting Sub
' here。 放置没有错误时也允许错误陷阱
Exit Sub
' Error causes msgbox to indicate the problem; provides new input box; 错误导致信
息框指明问题,提供新的输入框
' and resumes at the line that caused the error。 并恢复至导致错误的代码行
MyErrorTrap:
MsgBox 〃You may not have entered your Month and Year correctly。〃 _
& Chr(13) & 〃Spell the Month correctly〃 _
& 〃 (or use 3 letter abbreviation)〃 _
& Chr(13) & 〃and 4 digits for the Year〃
MyInput = InputBox(〃Type in Month and year for Calendar〃)
If MyInput = 〃〃 Then Exit Sub
Resume
End Sub
279
… 页面 296…
事件名称 新建工作表
事件描述 示例18当用户新建一个工作表后,引发 该事件
Private Sub Workbook_NewSheet(ByVal Sh As Object)
If MsgBox(〃Do you want to place 〃 & vbCrLf _
& 〃the new sheet at the beginning 〃 & vbCrLf _
& 〃of the workbook?〃; vbYesNo) = vbYes Then
Sh。Move before:=ThisWorkbook。Sheets(1)
Else
Sh。Move After:=ThisWorkbook。Sheets( _
ThisWorkbook。Sheetsunt)
MsgBox Sh。Name & _
〃 is now the last sheet in the workbook。〃
End If
End Sub
当用户在弹出的信息框上点击是的话,示例程序就会将新建的工作表置于工作簿的开始,否则在结
尾
示例18 – 试验:打开一个新工作簿,在VB编辑器窗口,激活工程浏览器窗口并且打开Excel对象
文件夹。双击ThisWorkbook并 且输入示例程序于ThisWorkbook代码窗口。切换到Excel窗口,并且
在工作表标签的任意地方单击右键,从快捷菜单上选择插入, 选择你想要插入的工作表类型,并
点击确定。Excel将会问你将新建的工作表放置到什么地方。下面是一个Excel工作表可以响应的
事件列表。
事件名称 描述
SheetActivate 当用户激活当前工作簿里的任何工作表发
生该事件。SheetActivate事件 同样也可
以发生在应用软件级别,当你激活任意打
开的工作簿里的任意 工作表时,都会激活
该事件
SheetDeactivate 当用户激活工作簿里一个不同的工作表时,引发该事件
SheetSelectionChange 当用户选择不同单元格时,引发该事件。
该事件发生在该工作簿里的每 个工作表
上
SheetChange 本事件发生在用户更改单元格内容时
SheetCalculate 本事件发生在用户重新计算工作表的时候
SheetBeforeDoubleClick 当用户双击工作表上的单元格时,引发该事件
SheetBeforeRightClick 当用户在工作表单元格上单击右键时,引发该事件
事件名称 激活窗口
事件描述 示例19
当用户激活任何显示该工作簿的
窗口时,引发该事件 Private Sub Workbook_WindowActivate(ByVal _
Wn As Window)
Wn。GridlineColor = vbYellow
End Sub
当用户激活包含Workbook_WindowActivate程序的工作簿时,示例程序将更改工作表的网格线颜色
为黄色
示例19 – 试验:在VB编辑器窗口,激活工程浏览器窗口并打开Excel对象文件夹。双击
ThisWorkbook,并输入示例程序。切换 到Excel窗口,打开一个全新的工作簿。使用窗口菜单将
Excel工作簿设置为竖直并排排列。当你激活含有示例程序的工作簿时, 网格线应该会变为黄色
了。
事件名称 窗口失活
事件描述 示例20
280
… 页面 297…
当用户将焦点从该工作簿窗口移 走时,引发该事件
Private Sub Workbook_WindowDeactivate(ByVal _
Wn As Window)
Wn。GridlineColor = vbRed
End Sub
当用户从包含Workbook_WindowActivate过程代码的工作簿切换到另外一个
工作簿时,示例程序将其网格 线设置为红色
示例20 – 试验:在VB编辑器窗口,激活工程浏览器窗口并打开Excel文件夹,双击ThisWorkbook
并输入示例程序。切换到Excel 窗口,打开一个全新的工作簿。使用窗口菜单将所有Excel工作簿
设置为竖直排列。当你失活包含Workbook_WindowDeactivate 事件代码的工作簿并切换到空工作
簿时,被失活地工作表里的网格线就会被变为红色。
事件名称 窗口调整大小
事件描述 示例21
当用户打开窗口,调整窗口大小, 最大化或者最小化窗口时,引发 该事件
Private Sub Workbook_WindowResize(ByVal Wn As Window)
If Wn。WindowState xlMaximized Then
Wn。Left = 0
Wn。Top = 0
End If
End Sub
当用户调整窗口大小时,示例程序将工作簿窗口移至屏幕的左上角
示例21 – 试验:在VB编辑器窗口,激活工程浏览器窗口并且打开Excel对�