vba Python 程序员资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/76882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 09:55:11  来源:igfitidea点击:

Resources for Python Programmer

pythonvbaporting

提问by tzot

I have written a lot of code in Python, and I am very used to the syntax, object structure, and so forth of Python because of it.

我用 Python 写了很多代码,也因此非常习惯 Python 的语法、对象结构等。

What is the best online guide or resource site to provide me with the basics, as well as a comparison or lookup guide with equivalent functions/features in VBA versus Python.

什么是最好的在线指南或资源站点,可以为我提供基础知识,以及在 VBA 和 Python 中具有等效功能/特性的比较或查找指南。

For example, I am having trouble equating a simple List in Python to VBA code. I am also have issues with data structures, such as dictionaries, and so forth.

例如,我无法将 Python 中的简单列表等同于 VBA 代码。我也有数据结构的问题,例如字典等。

What resources or tutorials are available that will provide me with a guide to porting python functionality to VBA, or just adapting to the VBA syntax from a strong OOP language background?

有哪些资源或教程可以为我提供将 Python 功能移植到 VBA 的指南,或者只是从强大的 OOP 语言背景中适应 VBA 语法?

回答by tzot

VBA is quite different from Python, so you should read at least the "Microsoft Visual Basic Help" as provided by the application you are going to use (Excel, Access…).

VBA 与 Python 完全不同,因此您至少应该阅读您将要使用的应用程序(Excel、Access...)提供的“Microsoft Visual Basic 帮助”。

Generally speaking, VBA has the equivalent of Python modules; they're called "Libraries", and they are not as easy to create as Python modules. I mention them because Libraries will provide you with higher-level types that you can use.

一般来说,VBA 具有等效于 Python 的模块;它们被称为“库”,它们不像 Python 模块那样容易创建。我提到它们是因为库将为您提供您可以使用的更高级别的类型。

As a start-up nudge, there are two types that can be substituted for listand dict.

作为启动助推器,有两种类型可以代替listdict

list

list

VBA has the type Collection. It's available by default (it's in the library VBA). So you just do a
dim alist as New Collection
and from then on, you can use its methods/properties:

VBA 的类型为Collection. 默认情况下它是可用的(它在库中VBA)。所以你只需做一个
dim alist as New Collection
,从那时起,你就可以使用它的方法/属性:

  • .Add(item)( list.append(item) ),
  • .Count( len(list) ),
  • .Item(i)( list[i] ) and
  • .Remove(i)( del list[i] ). Very primitive, but it's there.
  • .Add(item)( list.append(item) ),
  • .Count( len(list) ),
  • .Item(i)( list[i] ) 和
  • .Remove(i)( del list[i] )。非常原始,但它就在那里。

You can also use the VBA Array type, which like python arrays are lists of same-type items, and unlike python arrays, you need to do ReDimto change their size (i.e. you can't just append and remove items)

你也可以使用 VBA Array 类型,它就像 python 数组是相同类型项的列表,与 python 数组不同,你需要ReDim改变它们的大小(即你不能只是追加和删除项目)

dict

dict

To have a dictionary-like object, you should add the Scripting library to your VBA project1. Afterwards, you can
Dim adict As New Dictionary
and then use its properties/methods:

要获得类似字典的对象,您应该将脚本库添加到您的 VBA 项目 1。之后,您可以
Dim adict As New Dictionary
然后使用其属性/方法:

  • .Add(key, item)( dict[key] = item ),
  • .Exists(key)( dict.has_key[key] ),
  • .Items()( dict.values() ),
  • .Keys()( dict.keys() ),
    and others which you will find in the Object Browser2.
  • .Add(key, item)( dict[key] = item ),
  • .Exists(key)( dict.has_key[key] ),
  • .Items()( dict.values() ),
  • .Keys()( dict.keys() ),
    以及您将在 Object Browser2 中找到的其他内容。

1 Open VBA editor (Alt+F11). Go to Tools→References, and check the "Microsoft Scripting Runtime" in the list.

1 打开 VBA 编辑器 (Alt+F11)。转到“工具”→“参考”,然后选中列表中的“Microsoft Scripting Runtime”。

2 To see the Object Browser, in VBA editor press F2 (or View→Object Browser).

2 要查看对象浏览器,请在 VBA 编辑器中按 F2(或查看→对象浏览器)。

回答by Jiaaro

This tutorial isn't 'for python programmers' but I thinkit's a pretty good vba resource:

本教程不是“针对 Python 程序员”,但我认为这是一个非常好的 vba 资源:

http://www.vbtutor.net/VBA/vba_tutorial.html

http://www.vbtutor.net/VBA/vba_tutorial.html

This site goes over a real-world example using lists:

该站点使用列表查看了一个真实示例:

http://www.ozgrid.com/VBA/count-of-list.htm

http://www.ozgrid.com/VBA/count-of-list.htm

回答by Jiaaro

Probably not exactly what you are looking for but this is a decent VBA site if you have some programming background. It's not a list of this = that but more of a problem/solution

可能不是您正在寻找的,但如果您有一些编程背景,这是一个不错的 VBA 站点。这不是这个=那个的列表,而是更多的问题/解决方案

http://www.mvps.org/access/toc.htm

http://www.mvps.org/access/toc.htm

回答by Sam Corder

VBA as in what was implemented as part of Office 2000, 2003 and VB6 have been deprecated in favor of .Net technologies. Unless you are maintaining old code stick to python or maybe even go with IronPython for .Net. If you go IronPython, you may have to write some C#/VB.Net helper classes here and there when working with various COM objects such as ones in Office but otherwise it is supposed to be pretty functional and nice. Just about all of the Python goodness is over in IronPython. If you are just doing some COM scripting take a look at what ActiveState puts out. I've used it in the past to do some COM work. Specifically using Python as an Active Scripting language (classic ASP).

作为 Office 2000、2003 和 VB6 的一部分实施的 VBA 已被弃用,以支持 .Net 技术。除非你维护旧代码坚持使用 python 或者甚至使用 IronPython for .Net。如果您使用 IronPython,则在使用各种 COM 对象(例如 Office 中的对象)时,您可能需要在这里和那里编写一些 C#/VB.Net 帮助程序类,否则它应该非常实用且美观。几乎所有 Python 的优点都在 IronPython 中结束了。如果您只是在编写一些 COM 脚本,请查看 ActiveState 输出的内容。我过去曾用它来做一些 COM 工作。专门使用 Python 作为活动脚本语言(经典 ASP)。

回答by molasses

I think the equivalent of lists would be arrays in terms of common usage. Where it is common to use a list in Python you would normally use an array in VB. However, VB arrays are very inflexible compared to Python lists and are more like arrays in C.

我认为就常见用法而言,列表的等价物将是数组。在 Python 中通常使用列表的地方,您通常会在 VB 中使用数组。然而,与 Python 列表相比,VB 数组非常不灵活,更像是 C 中的数组。

' An array with 3 elements
'' The number inside the brackets represents the upper bound index
'' ie. the last index you can access
'' So a(2) means you can access a(0), a(1), and a(2) '

Dim a(2) As String
a(0) = "a"
a(1) = "b"
a(2) = "c"

Dim i As Integer
For i = 0 To UBound(a)
    MsgBox a(i)
Next

Note that arrays in VB cannot be resized if you declare the initial number of elements.

请注意,如果您声明元素的初始数量,则无法调整 VB 中的数组大小。

' Declare a "dynamic" array '

Dim a() As Variant

' Set the array size to 3 elements '

ReDim a(2)
a(0) = 1
a(1) = 2

' Set the array size to 2 elements
'' If you dont use Preserve then you will lose
'' the existing data in the array '

ReDim Preserve a(1)

You will also come across various collections in VB. eg. http://devguru.com/technologies/vbscript/14045.asp

您还将在 VB 中遇到各种集合。例如。http://devguru.com/technologies/vbscript/14045.asp

Dictionaries in VB can be created like this:

VB 中的字典可以这样创建:

Set cars = CreateObject("Scripting.Dictionary")
cars.Add "a", "Alvis"
cars.Add "b", "Buick"
cars.Add "c", "Cadillac" 

http://devguru.com/technologies/vbscript/13992.asp

http://devguru.com/technologies/vbscript/13992.asp

回答by JonnyGold

While I'm not a Python programmer, you might be able to run VSTO with Iron Python and Visual Studio. At least that way, you won't have to learn VBA syntax.

虽然我不是 Python 程序员,但您或许可以使用 Iron Python 和 Visual Studio 运行 VSTO。至少这样,您将不必学习 VBA 语法。

回答by CtrlDot

This may sound weird, but since I learned data structures in C++, I had a really hard time figuring out how to create them without pointers. There's something about VB6/VBA that makes them feel unnatural to me. Anyway, I came across this section of MSDN that has several data structure examples written in VBA. I found it useful.

这听起来可能很奇怪,但自从我在 C++ 中学习了数据结构,我很难弄清楚如何在没有指针的情况下创建它们。有一些关于 VB6/VBA 的东西让我觉得它们不自然。无论如何,我遇到了 MSDN 的这一部分,其中有几个用 VBA 编写的数据结构示例。我发现它很有用。

Creating Dynamic Data Structures Using Class Modules

使用类模块创建动态数据结构

回答by S.Lott

"I'm having trouble equating a simple List in Python with something in VBA..."

“我无法将 Python 中的简单列表与 VBA 中的某些内容等同起来……”

This isn't the best way to learn the language. In a way, you're giving up large pieces of Python because there isn't something like it in VBA.

这不是学习语言的最佳方式。在某种程度上,您放弃了大量 Python,因为 VBA 中没有类似的东西。

If there's nothing like a Python list in VBA, then -- well -- it's something new. And new would be the significant value in parts of Python.

如果在 VBA 中没有像 Python 列表那样的东西,那么——嗯——它是新的东西。而 new 将是 Python 部分的重要价值。

The first parts of the Python Built-in Typesmay not map well to VBA. That makes learning appear daunting. But limiting yourself to just things that appear in VBA tends to prevent learning.

Python内置类型的第一部分可能无法很好地映射到 VBA。这使得学习看起来令人生畏。但是将自己限制在 VBA 中出现的东西往往会阻碍学习。