Introduction-本书简介

来源:互联网 发布:php获取当前时区 编辑:程序博客网 时间:2024/04/30 02:00

 

电子表格软件的巨集()编程在过去的12年中发生的巨大的变化,那时还是基于文本的编程方式OFFICE 97的一个主要特性是巨集()出现在工作表模型中,它们可以通过一个VB编译器独立存取巨集()在过去非常基础,代码放置在单独设计的工作表中虽然它的功能还算强大,但它仍然是非结构化的语言,当然更不会是基于面向对象而描述的了大量的指令只为特定的任务而精心设计,这样的优点在于这些指令在学习和理解上比较容易,许多程序员开始通过编写电子表格的巨集()指令开始学习。
    但是,撰写文挡与让其他程序员理解你写的程序相当苦难,因为代码可以被放置在工作表的任何地方,而且每一个代码段只是在一个区域内定义这就意味这代码会在工作表中漫天飞舞,让程序看起来非常的复杂当然,专业的程序员会正确的为它们的代码加上注解,而且会将它们很好的组织起来.但是.这仍然是一个复杂而且没有结构的过程如果其他人希望来检查这些代码,这会会费它们额外的时间去理解它们是如何工作的商业公司不得不经常重写复杂的巨集()代码,一旦它们的原作者离开公司,因为这和评估它需要的时间相比不值一提。
    自从Excel 5.0,微软开始引入一种被称为VBA的编程语言,它是一种面向对象的更加直观更加简章的语言,而且它和VB非常相似,如果你学会了VBA,你将很快能理解VB的运作方式。
    VBA与古老的巨集()语言有很大的差别,如果你对古老的巨集()语言非常熟悉,那麽使用VBA将花费你很多的时间去重新思考与组织你的代码面向对象的编程概念使得两者之间的区别就好像奶酪与粉笔之间的区别一样,这些差异会让你能在电子表格上做更多的事情。使用面向对象的编程方式你会接触到对象的概念。擧个例子,你载入一个工作薄,EXCEl中的一种对象(物件),其中你输入的数据的工作又是另外一种对象(物件),它们都有自己的属性,触发事件,与方法,这些都会在本书的后面进行与大家进行讨论你将会在本书的第12章"EXCEL对象模型"接触到面向对象编程得更多细节。
VBA能很好的住址代码并运行它面向对象的特性,如果你是第一次接触到EXCEL巨集()编程,这些概念很容易理解,即使你没有关于他们过去是如何在EXCEL中运作的任何细节。基于文本的编程方式非常的特别,VBA是一种无法与之相对应的编程方式。非常不幸的是,良好的基于文本的编程方式对学习VBA没有任何帮助,反而会拉长你的学习曲綫。
 
因爲VBA被应用在所有的OFFICE组件中,本书包含的一些资讯对Word,Access甚至VB本身都会有帮助VBA是比它的前身-基于文本的编程方式-强大很多,它允许你随心所欲的扩展EXCEL的功能,它可以提供EXCEL菜单提供不了的功能,它甚至提供了操作其他OFFICE组件的能力。例如,你可以使用VBAOutlook中复制你的通讯录到电子表格中,只要安装有Outlook即使它并没有运行;以同样的处理方式,可以很轻松的将ACEESS导出为EXCEL文件,Access用户会很快学会如何应用巨基()将一个表格导出到Excel,但如果你希望从多个表格导出数据以及演示如何格式化这些数据,它们的导出过程,阐述它们的计算方式,毫无疑问,VBA可以让你做到这些。
VBA是一种完全面向对象的编程语言,需要以与以往完全不同的视角去看待。所有的对象都有自己的触发事件,方法,属性,它们能协助你更好的处理程序。这些对象有严格的继承方式(程序对象|工作薄对象|工作表对象),这些概念会在12章中详细解释。
不管你是一个巨集()编程的新手还是简单的只是想提高自己的技能,本书会向你展示EXCEL VBA面向对象编程的细节,能更好的协助你学习巨集().
--------------------------------------------------------------------------------------------------------------------

Introduction

Overview

Spreadsheet macro programming has changed enormously within the last 12 years, when we went from text-based macros to VBA. There was also a major change in Office 97, when macros went to VBA worksheet modules in a separate environment accessed via the Visual Basic Editor. It used to be fairly basic: code was entered onto a specially designated worksheet. Although the language was powerful in its own right, it was not a structured language and could certainly not be described as Object-Oriented. The number of commands was limited and a fair amount of ingenuity was required to do certain tasks. The main advantage of it was that it was easy to learn and understand; many programmers cut their teeth initially by writing spreadsheet macros.

However, it was also quite difficult to document and for other programmers to understand, since code could be placed anywhere on the macro spreadsheet, and blocks of code were only defined by a range name. This meant that code could be all over the place and following the flow of the program around the macro spreadsheet could become very complicated. Of course, professional developers could comment their code by using a cell to the right and could organize their code and its placement on the worksheet. However, it was still a complicated and unstructured process. If anyone other than the original author examined the code, it could take days to find out exactly how it worked and what it was doing. Commercial companies frequently found that when the author of a complicated macro left the company, that macro had to be rewritten from scratch because of the time involved in assessing what it was doing.

Since the advent of Excel 5.0, Microsoft has introduced a new programming language called Visual Basic for Applications (VBA). VBA is a more intuitive and robust programming language using an object-oriented design. It has a great deal of similarity with its older and larger cousin, Visual Basic (VB). Once you learn VBA, you will have a fair understanding of how Visual Basic itself operates.

VBA is extremely different from the old macro language—if the older language is what you are used to, it will involve a total rethinking of how you write and structure your code. The concepts of Object Orientated Programming (OOP) are as different as chalk and cheese to the old macro language, but there is a huge advantage in terms of what you can achieve on a spreadsheet. With Object Orientated Programming you are dealing with the concept of objects. To use an example, the workbook you load is an object. The worksheet where you enter your data is another object. The command bar (menu) across the top is the printer and the screen. All have properties, events, and methods (discussed later in the book). You will start to see Object Oriented Programming in more detail when you reach Chapter 12 (“The Excel Object Model”).

VBA does allow a more structured and object-orientated approach to writing your macros. If this is your first foray into the world of Excel macro programming, you may well find the concepts easier to grasp, since you have no knowledge of the technologies used in the past within Excel. The text-based macros were a completely different language in terms of how and where you entered it. The concept of VBA is unique and cannot be compared to text-based macros. Unfortunately, knowledge of the old system of writing macros can add to confusion with the new method of VBA and extend the learning curve.

Since VBA is shared by all Office Applications, a great deal of the information contained in this book will help you with other Office Applications such as Word or Access and also with Visual Basic itself. VBA is more powerful then the previous text macro language and enables you to extend Excel in any way you choose. It allows you to write code to do things not within the Excel menu structure. It even provides the means to access and manipulate other Microsoft Office Applications under software control. For example, by using the Outlook object model you could copy address lists onto your spreadsheet merely by executing VBA code. This would happen even if Outlook were not running, since Outlook only needs to be installed for the object model to work.

By the same token, you can also manipulate the Excel object model from another Microsoft Application. For example, you could produce an Excel spreadsheet file from Microsoft Access without Excel ever appearing onscreen or being loaded. Access users will know that it is very easy to write a macro to export a table into Excel, but what if you want information from several tables and you want presentation formatting on the numbers and audit trails on the figures explaining how they were calculated? VBA allows you to do this.

VBA is a full object-oriented language that needs a totally different viewpoint from the old text-based macro programming. All objects can have Events, Methods, and Properties and these can be manipulated to assist your programs. The objects are arranged in a hierarchy (Application Object | Workbook | Worksheet). This concept is explained in more detail in Chapter 12.

Whether you are totally new to macro programming in Excel or simply want to update your skills, this book will show you how to use the Excel object model along with VBA to learn how to program macros effectively.

 
 
原创粉丝点击