5.1鸟瞰Application对象

5.2 必须了解的面向显示特性

5.2.1 使用ScreenUpdating改进和完善执行性能

代码清单5.1:实现屏幕更新的性能

'代码清单5.1: 实现屏幕更新的特性
Sub TimeScreenUpdating()
Dim dResult As Double 'test with screen updating turned on
dResult = TestScreenUpdating(True)
MsgBox Format(dResult, "0.00") & " seconds.", vbOKOnly 'test with screen updating turned off
dResult = TestScreenUpdating(False)
MsgBox Format(dResult, "0.00") & " seconds.", vbOKOnly End Sub Function TestScreenUpdating(bUpdatingOn As Boolean) As Double 'record the start time
Dim dStart As Double
dStart = Timer 'turn screen updating on or off
Application.ScreenUpdating = bUpdatingOn 'loop through each worksheet
'in the workbook 250 times
Dim nRepetition As Integer
Dim ws As Worksheet
For nRepetition = To
For Each ws In ThisWorkbook.Worksheets
ws.Activate
Next
Next 'turn screen updating on
Application.ScreenUpdating = True 'return elapsed time since procedure started
TestScreenUpdating = Timer - dStart 'clean up
Set ws = Nothing End Function

5.2.2 使用状态栏为最终用户提供信息

代码清单5.2:使用StatusBar属性显示信息

'代码清单5.2: 使用StatusBar属性显示信息
'this subroutine tests the impact of
'using statusbar to display lots of frequent messages.
Sub TimeStatusBar()
Dim dStart As Double
Dim dResult As Double
Dim bDisplayStatusBar As Boolean 'remember original status bar setting
bDisplayStatusBar = Application.DisplayStatusBar
'turn on the status bar
Application.DisplayScrollBars = True 'baseline test - no status bar, every row
'to isolate how long it takes to
'perform mod statement on all rows
dStart = Timer
TestStatusBar , False
dResult = Timer - dStart
MsgBox Format(dResult, "0.00") & " Seconds.", vbOKOnly 'time using statusbar -every row
dStart = Timer
TestStatusBar , True
dResult = Timer - dStart
MsgBox Format(dResult, "0.00") & " Seconds.", vbOKOnly 'time using statusbar -every fifth row
dStart = Timer
TestStatusBar , True
dResult = Timer - dStart
MsgBox Format(dResult, "0.00") & " Seconds.", vbOKOnly 'restore the status bar to its original setting
Application.DisplayScrollBars = bDisplayStatusBar End Sub 'this subroutine displays a message to the status bar
'(if desired) for each row in a worksheet using the
'interval specified.
Private Sub TestStatusBar(nInterval As Integer, bUseStatusBar As Boolean)
Dim lRow As Long
Dim lLastRow As Long
Dim ws As Worksheet 'using the first worksheet in this workbook
'no changes will be made to the worksheet.
Set ws = ThisWorkbook.Worksheets() 'every version since excel 97 has had
'65,536 rows. excel 5 had 16,384 rows.
lLastRow = ws.Rows.Count For lRow = To lLastRow 'test to see if the current row
'is the interval specified.
If lRow Mod nInterval = Then
If bUseStatusBar Then
Application.StatusBar = "processing row: " & lRow & _
" of " & lLastRow & " rows."
End If
End If
Next Application.StatusBar = False
Set ws = Nothing
End Sub

5.3 需要了解的面向显示特性

代码清单5.3:带有Cursor属性的可用光标

'代码清单5.3: 带有Cursor属性的可用光标
Sub ViewCursors()
Application.Cursor = xlNorthwestArrow
MsgBox "Do you like the xlNorthwestArrow? Hover over the worksheet to see it." Application.Cursor = xlIBeam
MsgBox "How about xlIBeam? Hover over the worksheet to see it." Application.Cursor = xlWait
MsgBox "How about xlWait? Hover over the worksheet to see it." Application.Cursor = xlDefault
MsgBox "How about xlDefault? Hover over the worksheet to see it." End Sub

代码清单5.4:示范各种面向窗口的属性

'代码清单5.4: 示范各种面向窗口的属性
Sub GetWindowInfo()
Dim lState As Long
Dim sInfo As String
Dim lResponse As Long 'Determine window state
lState = Application.WindowState
Select Case lState
Case xlMaximized
sInfo = "Window is maximized." & vbCrLf
Case xlMinimized
sInfo = "Window is maximized." & vbCrLf
Case xlNormal
sInfo = "window is normal." & vbCrLf
End Select 'prepare message to be displayed
sInfo = sInfo & "Usable Height = " & Application.UsableHeight & vbCrLf
sInfo = sInfo & "Usable Width = " & Application.UsableWidth & vbCrLf
sInfo = sInfo & "Height = " & Application.Height & vbCrLf
sInfo = sInfo & "Width = " & Application.Width & vbCrLf & vbCrLf sInfo = sInfo & "Would you like to minimize it? " & vbCrLf 'Display message
lResponse = MsgBox(sInfo, vbYesNo, "") 'Minimize window if user clicked yes
If lResponse = vbYes Then
Application.WindowState = xlMinimized
End If End Sub

5.4 便捷的Excel对象属性

属性 返回 描述
ActiveCell Range  
ActiveChart Chart  
ActivePrinter String  
ActiveSheet Sheet  
ActiveWindow Window  
ActiveWorkbook Workbook  
Selection Range/Chart/Control 取决于用户的选择
ThisCell Range 调用一个用户定义的函数单元格
ThisWorkbook Workbook  
Caller Range 返回使用此函数的单元格

5.5 常用的简化文件操作

5.5.1从用户那里获得文件名

代码清单5.5:从用户那里获取单个工作薄

'代码清单5.5: 从用户那里获取单个工作薄
Sub TestGetFile()
Dim nIndex As Integer
Dim sFile As String 'Get a batch of Excel files
sFile = GetExcelFile("Testing GetExcelFile Function") 'make sure dialog wasn't cancelled - in which case
'sFile would equal False
If sFile = "False" Then
Debug.Print "No file selected."
Exit Sub
End If 'OK - we have a valid file
Debug.Print sFile End Sub 'Presents user with a GetOpenFileName dialog which allows
'single file selection.
'return a single of filename
Function GetExcelFile(sTitle As String) As String Dim sFilter As String
Dim bMultiSelect As Boolean sFilter = "Workbooks (*.xls),*.xls"
bMultiSelect = False GetExcelFile = Application.GetOpenFilename _
(FileFilter:=sFilter, Title:=sTitle, MultiSelect:=bMultiSelect) End Function

代码清单5.6:从用户那里获取一批工作薄

'代码清单5.6: 从用户那里获取一批工作薄
Sub TestGetFiles()
Dim nIndex As Integer
Dim vFiles As Variant 'Get a batch of Excel files
vFiles = GetExcelFiles("Testing GetExcelFiles Function") 'make sure dialog wasn't cancelled - in which case
'vFiles would equal False
If Not IsArray(vFiles) Then
Debug.Print "No files selected."
Exit Sub
End If 'OK - loop through the fileNames
For nIndex = To UBound(vFiles)
Debug.Print vFiles(nIndex)
Next nIndex End Sub 'Presents user with a GetOpenFileName dialog that allows
'Multiple file selection.
'Returns an array of filenames.
Function GetExcelFiles(sTitle As String) As Variant
Dim sFilter As String
Dim bMultiSelect As Boolean sFilter = "Workbooks (*.xls), *.xls "
bMultiSelect = True GetExcelFiles = Application.GetOpenFilename _
(filefilter:=sFilter, Title:=sTitle, MultiSelect:=bMultiSelect) End Function

默认情况下,VBA数组是基于0的。但是,GetOpenFilename多选模式返回的数组是基于1的。

5.5.2 使用GetSaveAsFilename选取合适的位置

代码清单5.7:GetSaveAsFilename的基本使用

'代码清单5.7: GetSaveAsFilename 的基本使用
Sub SimpleGetSaveAsFilename()
Dim sFile As String
Dim lResponse As Long
Dim sMsg As String Do
sFile = Application.GetSaveAsFilename
sMsg = "you chose: " & sFile & " . Keep experimenting?"
lResponse = MsgBox(sMsg, vbYesNo) Loop While lResponse = vbYes End Sub

5.5.2.1 分解文件名

代码清单5.8:分解文件名为路径和文件名

'代码清单5.8: 分解文件名为路径和文件名
'A simple procedure for testing the
'BreakDownName procedure
Sub TestBreakdownName()
Dim sPath As String
Dim sName As String Dim sFileName As String
Dim sMsg As String sFileName = Application.GetSaveAsFilename
BreakdownName sFileName, sName, sPath
sMsg = "the file name is: " & sName & vbCrLf
sMsg = sMsg & "the path is: " & sPath & vbCrLf MsgBox sMsg, vbOKOnly End Sub Function GetShortName(sLongName As String) As String
Dim sPath As String
Dim sShortName As String BreakdownName sLongName, sShortName, sPath
GetShortName = sShortName End Function '当有2个返回值时,用byRef参数过程
Sub BreakdownName(sFullName As String, ByRef sName As String, ByRef sPath As String)
Dim nPos As Integer 'Find out where the filename begins
nPos = FileNamePosition(sFullName)
If nPos > Then
sName = Right(sFullName, Len(sFullName) - nPos)
sPath = Left(sFullName, nPos - )
Else
'invalid sFullName - don't change anything
End If End Sub 'Returns the position or index of the first
'character of the filename given a full name
'A full name consists of a path and a filename
'Ex. FileNamePosition("c: \Testing\Test.txt") = 11
Function FileNamePosition(sFullName As String) As Integer
Dim bFound As Boolean
Dim nPosition As Integer bFound = False
nPosition = Len(sFullName) Do While bFound = False
If nPosition = Then Exit Do If Mid(sFullName, nPosition, ) = "\" Then
bFound = True
Else
nPosition = nPosition -
End If
Loop If bFound = False Then
FileNamePosition =
Else
FileNamePosition = nPosition
End If End Function

5.6 检查操作环境

代码清单5.9:使用Application对象属性获取有效的系统信息

'代码清单5.9:使用Application 对象属性获取有效的系统信息
Sub InspectTheEnvironment()
Debug.Print Application.CalculationVersion
' Debug.Print Application.MemoryFree
' Debug.Print Application.MemoryUsed
Debug.Print Application.OperatingSystem
Debug.Print Application.OrganizationName
Debug.Print Application.UserName
Debug.Print Application.Version End Sub

5.7有用的两个额外成员

第一个是CutCopyMode属性,这个属性决定当剪切或复制时,是否在选中区域边界周围显示移动的破折号。

Application.CutCopyMode = False

第二个功能是InputBox方法:

'5.7 InputBox 函数用法的例子
Sub SimpleInputBox()
Dim vInput As Variant
vInput = InputBox("What is your name?", "introduction", Application.UserName)
MsgBox "Hello, " & vInput & ". Nice to meet you.", vbOKOnly, "Introduction" End Sub

5 Application 对象的更多相关文章

  1. Application对象、ViewState对象、分页展示--2017年1月4日

    Application对象 存储 Application 变量  Application["application名称"] = "application的值"; ...

  2. JSP内置对象之application对象

    虽然常把Web应用称为B/S架构的应用,但其实Web应用一样是C/S结构的应用,只是这种应用的服务器是Web服务器,而客户端是浏览器. 现在抛开Web应用直接看Web服务器和浏览器. Web服务器负责 ...

  3. 什么是Cookie对象,Session对象,Application对象。

    Cookie是: 一个由网页服务器放在您硬盘上的非常小的文本文件. 它本质上就像您的身份证明一样,并且不能像代码那样被执行或被用来散布病毒.它只能被您使用并且只能由提供的服务器读取. 使用Cookie ...

  4. Application对象、Session对象、Cookie对象、Server对象初步认识

    Application对象:记录应用程序参数的对象 用于共享应用程序级信息,即多个用户共享一个Application对象.在第一个用户请求ASP.NET文件时,将启动应用程序并创建Applicatio ...

  5. Android Application 对象介绍

    What is Application Application和Actovotu,Service一样是android框架的一个系统组件,当android程序启动时系统会创建一个 application ...

  6. ASP.NET中application对象的用法

    一.Application对象的理解 Application对象在实际网络开发中的用途就是记录整个网络的信息,如上线人数.在线名单.意见调查和网上选举等.在给定的应用程序的多有用户之间共享信息,并在服 ...

  7. [原创]java WEB学习笔记47:Servlet 监听器简介, ServletContext(Application 对象), HttpSession (Session 对象), HttpServletRequest (request 对象) 监听器,利用listener理解 三个对象的生命周期

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  8. ASP.NET中application对象

    ASP.NET中application对象的使用. Application对象的应用  1.使用Application对象保存信息  (1).使用Application对象保存信息  Applicat ...

  9. 初识 Asp.Net内置对象之Application对象

    Application对象 Applocation对象用于共享应用程序级信息,即多个用户可以共享一个Applocation对象. 用户在请求Asp.Net文件时,将启动应用程序并且创建Applicat ...

  10. jsp 用application对象制作留言板

    <%@ page contentType="text/html; charset=gb2312"%> <html> <body> <for ...

随机推荐

  1. 微信小程序 video组件 不随页面滚动

    1.页面初始化(滚动前)时,video所在位置 2.页面滚动后,video视频组件所在位置 看了别人家的小程序并不会出现这种状况.最后检查发现,是页面包裹层设置了 height:100% 导致的 顺便 ...

  2. docker push

    一.确保docker hub上有账号 二.确认要提交的镜像的命名空间为自己账号名称 三.在本地登录docker: docker login 四.提交镜像: docker push zhengchuzh ...

  3. 24L01-2.4G无线传输模块调节记录

    在调试24L01的时候,虽然能用到别人的程序,但仅仅是程序的初始化,并没有告诉我们如何去后续的操作,如何去再次发送一组数.最近调试24L01接近尾声,将逐一的地方总结下来,以便以后查阅,也供其他人借鉴 ...

  4. xcap发包工具的简单使用3(报文描述)

    之前详细介绍了如何构造以及发送报文,现在简单对报文描述一下 1.Ethernet MAC:填写报文目的mac和源mac地址 Vlan:支持单vlan,QinQ,如果有更多的vlan,请填写在“More ...

  5. NYOJ-568/1012//UVA-12299RMQ with Shifts,线段树单点更新+区间查询

    RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 ->  Link1  <- -> Link2  <- 以上两题题意是一样 ...

  6. 【扫描线】HDU 5124 lines

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 [题意] 在数轴x上,每次操作都覆盖一个区间的所有点,问被覆盖次数最多的点是覆盖了多少次 [思路] 最简单 ...

  7. windows PHP配置随笔

    这几天配置本地windows wnmp(windows + nginx + mysql + php 5.3)遇到了不少问题.决定以后随笔记下解决的问题. #php.ini 配置含路径的值时,要注意把使 ...

  8. 上传文件表单file,限制上传文件类型的方法--参数accept

    我们使用<input type="file" />来上传文件,但是当你只想要上传某种格式的文件,比如说(jpg)文件时.可以通过accept来限制. <form& ...

  9. 洛谷P1186 玛丽卡

    题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城 ...

  10. SQLSERVER数据库管理员的专用连接DAC

    出处: http://www.cnblogs.com/lyhabc/archive/2012/09/23/2698702.html DAC:Dedicated Admin Connection 当SQ ...