两者的区别是什么?我一直认为VBA是VB的“残缺”版本,但当一个朋友前几天问我,我不知道实际的区别是什么。
另外,当你使用Excel时,你用的是VB还是VBA ?
两者的区别是什么?我一直认为VBA是VB的“残缺”版本,但当一个朋友前几天问我,我不知道实际的区别是什么。
另外,当你使用Excel时,你用的是VB还是VBA ?
当前回答
下面是对一个老问题的更技术性和更彻底的回答:Visual Basic for Applications (VBA)和Visual Basic (pre-.NET)不仅仅是相似的语言,它们是同一种语言。具体地说:
它们具有相同的规范:对语言包含的内容及其含义的独立于实现的描述。你可以在这里阅读:[MS-VBAL]: VBA语言规范 它们具有相同的平台:它们都编译为Microsoft P-Code,后者依次由完全相同的虚拟机执行,该虚拟机在dll msvbvm[x.0].dll中实现。
在我去年读到的一本旧的VB参考书中,作者(Paul Lomax)甚至断言“VBA”一直是语言本身的名称,无论是在独立应用程序中还是在嵌入式环境中(如MS Office):
“在我们继续讨论之前,让我们先澄清一个基本问题。Visual Basic for Applications (VBA)是用于在Visual Basic (VB)中编程的语言。VB本身就是一个开发环境;该环境的语言元素是VBA。”
细微的差别
Hosted vs. stand-alone: In practical, terms, when most people say "VBA" they specifically mean "VBA when used in MS Office", and they say "VB6" to mean "VBA used in the last version of the standalone VBA compiler (i.e. Visual Studio 6)". The IDE and compiler bundled with MS Office is almost identical to Visual Studio 6, with the limitation that it does not allow compilation to stand-alone dll or exe files. This in turns means that classes defined in embedded VBA projects are not accessible from non-embedded COM consumers, because they cannot be registered.
Continued development: Microsoft stopped producing a stand-alone VBA compiler with Visual Studio 6, as they switched to the .NET runtime as the platform of choice. However, the MS Office team continues to maintain VBA, and even released a new version (VBA7) with a new VM (now just called VBA7.dll) starting with MS Office 2010. The only major difference is that VBA7 has both a 32- and 64-bit version and has a few enhancements to handle the differences between the two, specifically with regards to external API invocations.
其他回答
VB (Visual Basic only up to 6.0) is a superset of VBA (Visual Basic for Applications). I know that others have sort of eluded to this but my understanding is that the semantics (i.e. the vocabulary) of VBA is included in VB6 (except for objects specific to Office products), therefore, VBA is a subset of VB6. The syntax (i.e. the order in which the words are written) is exactly the same in VBA as it would be in VB6, but the difference is the objects available to VBA or VB6 are different because they have different purposes. Specifically VBA's purpose is to programatically automate tasks that can be done in MS Office, whereas VB6's purpose is to create standard EXE, ActiveX Controls, ActiveX DLLs and ActiveX EXEs which can either work stand alone or in other programs such as MS Office or Windows.
VB不是一种语言。VB是一个承载VBA的程序,就像Office承载VBA一样。VB是一组App对象,就像Word和Excel一样,还有一个表单包,就像Office一样。
所以你只能在VB中编写VBA代码。
PS这个信息在VB问题页面的info选项卡上。
来自VBA信息
VBA 6于1998年发布,包含大量授权主机,其中包括:Office 2000 - 2010、AutoCAD、PI Processbook和独立的Visual Basic 6.0
下面是对一个老问题的更技术性和更彻底的回答:Visual Basic for Applications (VBA)和Visual Basic (pre-.NET)不仅仅是相似的语言,它们是同一种语言。具体地说:
它们具有相同的规范:对语言包含的内容及其含义的独立于实现的描述。你可以在这里阅读:[MS-VBAL]: VBA语言规范 它们具有相同的平台:它们都编译为Microsoft P-Code,后者依次由完全相同的虚拟机执行,该虚拟机在dll msvbvm[x.0].dll中实现。
在我去年读到的一本旧的VB参考书中,作者(Paul Lomax)甚至断言“VBA”一直是语言本身的名称,无论是在独立应用程序中还是在嵌入式环境中(如MS Office):
“在我们继续讨论之前,让我们先澄清一个基本问题。Visual Basic for Applications (VBA)是用于在Visual Basic (VB)中编程的语言。VB本身就是一个开发环境;该环境的语言元素是VBA。”
细微的差别
Hosted vs. stand-alone: In practical, terms, when most people say "VBA" they specifically mean "VBA when used in MS Office", and they say "VB6" to mean "VBA used in the last version of the standalone VBA compiler (i.e. Visual Studio 6)". The IDE and compiler bundled with MS Office is almost identical to Visual Studio 6, with the limitation that it does not allow compilation to stand-alone dll or exe files. This in turns means that classes defined in embedded VBA projects are not accessible from non-embedded COM consumers, because they cannot be registered.
Continued development: Microsoft stopped producing a stand-alone VBA compiler with Visual Studio 6, as they switched to the .NET runtime as the platform of choice. However, the MS Office team continues to maintain VBA, and even released a new version (VBA7) with a new VM (now just called VBA7.dll) starting with MS Office 2010. The only major difference is that VBA7 has both a 32- and 64-bit version and has a few enhancements to handle the differences between the two, specifically with regards to external API invocations.
对于几乎所有的编程目的,VBA和VB 6.0是一样的东西。
VBA无法将程序编译成可执行二进制文件。你总是需要主机(例如,一个Word文件和MS Word)来包含和执行你的项目。你也不能用VBA创建COM dll。
除此之外,在IDE上还有一个不同之处——相比之下,VB 6.0 IDE更强大。另一方面,您可以在VBA中紧密集成主机应用程序。应用程序全局对象(如“ActiveDocument”)和事件无需声明即可使用,因此特定于应用程序的编程非常简单。
不过,没有什么能阻止你启动Word,加载VBA IDE并解决一个与Word无关的问题。我不确定VB 6.0能做什么(技术上),而VBA不能。不过,我正在寻找MSDN上的比较表。
你想比较VBA与VB-Classic (VB6..)或VB.NET?
VBA (Visual Basic for Applications)是嵌入在Microsoft Office应用程序中的基于vb-classic的脚本语言。我认为它的语言特性与VB5类似(它只是缺少一些内置函数),但是:
您可以访问您为其编写vba脚本的办公文档,因此您可以例如。
编写宏(=用于办公室工作中重复性小任务的自动化例程) 为excel-cell-formula定义新函数 处理办公数据
示例:设置excel-cell的值
ActiveSheet.Cells("A1").Value = "Foo"
VBC和-。NET不是脚本语言。你可以使用它们在独立的IDE中编写独立的应用程序,这是VBA无法做到的(VBA脚本只是“存在”在Office中)
VBA和VB没有任何关系。NET(它们只是有相似的语法)。