Resources for Python Programmer [closed]

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.

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.

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.

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?


Asked by: Kimberly618 | Posted: 05-10-2021






Answer 1

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…).

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.

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

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:

  • .Add(item) ( list.append(item) ),
  • .Count ( len(list) ),
  • .Item(i) ( list[i] ) and
  • .Remove(i) ( del list[i] ). Very primitive, but it's there.

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 ReDim to change their size (i.e. you can't just append and remove items)

dict

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

  • .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 Browser².

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

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

Answered by: Roman841 | Posted: 06-11-2021



Answer 2

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

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

Answered by: Ada456 | Posted: 06-11-2021



Answer 3

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

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

Answered by: Carina232 | Posted: 06-11-2021



Answer 4

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).

Answered by: Chelsea562 | Posted: 06-11-2021



Answer 5

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.

Answered by: Dexter177 | Posted: 06-11-2021



Answer 6

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.

' 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.

' 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

Dictionaries in VB can be created like this:

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

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

Answered by: Charlie277 | Posted: 06-11-2021



Answer 7

"I'm having trouble equating a simple List in Python with something in 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.

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.

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

Answered by: Gianna496 | Posted: 06-11-2021



Answer 8

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.

Creating Dynamic Data Structures Using Class Modules

Answered by: William679 | Posted: 06-11-2021



Similar questions

Now that Python 2.6 is out, what modules currently in the language should every programmer know about?

A lot of useful features in Python are somewhat "hidden" inside modules. Named tuples (new in Python 2.6), for instance, are found in the collections module. The Library Documentat...


Advice for C# programmer writing Python


C++ or Python for C# programmer?

Closed. This question is opinion-based. It is not c...


Python for C++ or Java Programmer

I have a background in C++ and Java and Objective C programming, but i am finding it hard to learn python, basically where its "Main Function" or from where the program start executing. So is there any tutorial/book which can teach python to people who have background in C++ or Java. Basically something which can show if how you were doing this in C++ and how this is done in Python. OK i think i did not put the que...


java - asm / C / Python / Perl / Lisp / Scheme Programmer looking for something new to learn

Closed. This question does not meet Stack Overflow guid...


Python for a Perl programmer

I am an experienced Perl developer with some degree of experience and/or familiarity with other languages (working experience with C/C++, school experience with Java and Scheme, and passing familiarity with many others). I might need to get some web work done in Python (most immediately, related to Google App Engine). As such, I'd like to ask SO overmind for good references on how to best learn Python for someone w...


Perl for a Python programmer

I know Python (and a bunch of other languages) and I think it might be nice to learn Perl, even if it seems that most of the people is doing it the other way around. My main concern is not about the language itself (I think that part is always easy), but about learning the Perlish (as contrasted with Pythonic) w...


Newbie Python programmer tangling with Lists

Here's what I've got so far: # A. match_ends # Given a list of strings, return the count of the number of # strings where the string length is 2 or more and the first # and last chars of the string are the same. # Note: python does not have a ++ operator, but += works. def match_ends(words): counter = 0 for word in words: if len(word) >= 2 and word[0] == word[-1]: counter += counter retur...


php - Old desktop programmer wants to create S+S project

I have an idea for a product that I want to be web-based. But because I live in a part of the world where the internet is not always available, there needs to be a client desktop component that is available for when the internet is down. Also, I have been a SQL programmer, a desktop application programmer using dBase, VB and Pascal, and I have created simple websites using HTML and website creation tools, such as Frontpage...


Proper way to do session handling in Python + Pylons for a php programmer

I'm a php programmer who's just getting started with Python. I'm trying to get Python to handle login/logout via database-stored sessions. Things work, but seem inconsistent. For example, sometimes a user isn't logged out. Sometimes users "switch" logins. I'm guessing this has something to do with thread-safety, but I'm just not sure where to begin on how to fix this. Any help would be appreciated. Here's what I hav...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top