Python or vba for excel

We hope you love the products we recommend! Just so you know, when you buy through links on our site, we may earn an affiliate commission. This adds no cost to our readers, for more information read our earnings disclosure.

Visual Basic for Applications (VBA) is an Excel programming language built by Microsoft, whereas Python is a high-level, general-purpose, and open-source programming language that is frequently used to create websites and applications, automate processes, and, of course, perform data analysis. Python was created by Guido van Rossum.

VBA and Python are both excellent programming languages and wonderful resources for data analysis, but they serve distinct reasons.

  1. Python vs VBA: Which is Better for Data Analysis?
  2. Is Python Better Than VBA?
  3. Python Vs VBA Speed
  4. Is VBA Easier Than Python?
  5. Python Vs VBA Differences
    • Automation Capabilities
    • Open Source Differences
  6. Benefits of VBA
  7. Drawbacks of VBA
  8. Benefits of Python
  9. Drawbacks of Python
  10. Can Python Replace VBA?
  11. Should I Learn VBA Or Python First?

Python vs VBA: Which is Better for Data Analysis?

The majority of businesses are switching from VBA to Python.

Furthermore, because it is open-source, numerous people may change and tweak the Python code in more creative and useful ways.

Python is preferable to VBA for data analysis because it is cleaner and allows for better version control. VBA is only useful for simple Excel automation because that is what it was designed for. If you need to accomplish something more complicated, Python is the way to go. 

Most people are still puzzled as to why so many professionals still use VBA, and what advantages it has over Python, which is more current and capable of handling larger data sets.

Join us on a journey through the preferences between VBA and Python. 


What you want to do with it determines which is preferable.

VBA and Python are both worthwhile learning.

VBA, on the other hand, will be better if you need anything done within Excel. When you need anything done with an Excel export, Python is more beneficial.

Because of the features it provides, many data analysts and software developers now believe Python to be a suitable replacement tool for VBA, however, the issue of “Is Python quicker than VBA?” is frequently raised.

This is because, when it comes to carrying out activities and responsibilities, quicker is always preferable.


Python Vs VBA Speed

Python is the most effective. If you wish to concentrate on a larger environment, however, it is equally useful for a variety of tasks, but not for Office automation with the same simplicity or efficiency as VBA.

As a result, we can say that both Python and VBA are fast when utilized for their intended purposes in terms of speed.


Is VBA Easier Than Python?

Despite the handful of free tools available to make Python simple to learn, Excel is far easier to get started with and is much more user pleasant (some say).

It is reasonably easy to become an advanced user with VBA without much expertise owing to its GUI (Graphical User Interface)

Additionally, although Python is known for its clean syntax, it is more difficult to learn than VBA, and also requires installation of several packages and configuration of an environment. 

However, Python is simpler to master than Excel, which has its own customized language, VBA, which is difficult to grasp and execute.

The majority of people compare Python to English and believe it is easier to learn than other computer languages. Everything else becomes as simple as communication once they learn the basic concept of constructing and manipulating with the basics.


Python Vs VBA Differences

Automation Capabilities

When it comes to automating, Python and VBA are both capable of automation, although Python can handle significantly bigger datasets than VBA.

Plus, Python allows you to extend your projects, scale up and build complex tasks. When compared to Excel’s VBA, Python allows for speedier computations and the handling of more complicated formulas.

VBA is restricted to Microsoft Office automation, but Python may run on any operating system and be used for a wide range of tasks.

Open Source Differences

Python has an advantage over VBA since it is an open-source language.

As a result, numerous libraries have been written to help with complicated and innovative jobs that would be impossible or constrained in VBA.

Because anybody can design and develop a Python library, the possibilities are virtually limitless.


Benefits of VBA

  • It can remove the need to repeat tasks and replace them with macros and visual controls. VBA allows you to perform tasks in exactly the same way again and over again without making any mistakes.
  • The VBA code’s user-friendliness makes it simple to utilize. Even persons with no prior programming knowledge may quickly learn to comprehend and create VBA code.
  • It’s also Microsoft’s introduction language, so it’s got a big fan base.

Drawbacks of VBA

  • The VBA editor is likewise out of date and unusable.
  • VBA is not a stand-alone language. It can only operate as part of a host application, not as a separate program, which restricts VBA’s capabilities because it can only be used within the Excel domain.

Benefits of Python

  • Python is a beginner-friendly language, and getting started with it is simple.
  • Python offers a large library that includes code for web browsers, image processing, databases, email, unit testing, and other things. These fantastic libraries make doing difficult data analysis and machine learning jobs a pleasure.
  • Python is a free programming language available for anyone. Not only that, but it’s also free and open-source software. This implies you may obtain Python’s source code, modify it, and share the resulting Python version.

Drawbacks of Python

  • Python uses more memory than other languages because its data types are more versatile.
  • Python is a slow language because it is an interpreted language, which means it executes code directly rather than turning it into machine-language instructions. This action frequently leads to a longer execution time.

Can Python Replace VBA?

Nowadays, everyone has a lot of data, and Excel VBA is becoming obsolete when it comes to handling enormous amounts of data. Python is quickly becoming the new standard since it can accomplish everything that VBA could do in Excel.

VBA is really different from Excel, and the two interact via a COM interface. As a result, the win32com library may be used to access Excel from Python.


Should I Learn VBA Or Python First?

Learn Python if you wish to create independent (standalone) apps.

If you just want to automate time-consuming and repetitive tasks in Office apps, then VBA is the way to go.

Not because Python is difficult, but because learning the fundamentals of VBA is straightforward.

Users that switch from Excel to Python get access to a number of advantages, including an open-source development environment, a large number of volunteer contributors, and free libraries.

In summary, Excel is an excellent entry-level tool that allows you to perform basic analysis quickly and easily.

Python, on the other hand, provides the tools, methodologies, and processing capacity that Excel lacks in the present day, with enormous data sets and more advanced analytics and automation. 

Python, after all, is more powerful, quicker, and capable of superior data processing, as well as having a more inclusive and collaborative support structure.

So you’ve heard about the potential Python, and you’ve been using Visual Basic for Applications (VBA) for a while. But you’re probably thinking—which is better? And is it worth switching over?

Not to worry, in this blog post, I’ll be comparing Python vs VBA and discuss some of the pros and cons of each language, plus some examples.

Read on to understand which language is better for you!

Python vs VBA: Which Works Better for Data Analysis?

Python works better than VBA for data analysis. Python provides more flexibility, speed, and scalability. It’s designed with data analysis and manipulation in mind, while VBA is designed to automate activities within Excel like formatting, manipulating cells, and other tasks.

Additionally, Python has a wide range of libraries for data analysis and visualization that makes it easier to do complex tasks than with VBA.

My Experience in Switching From VBA to Python

When I was working as a biological researcher in college, I had to collect most of my data and input them into large spreadsheets within Microsoft Excel. I had to format and clean them all within Excel either manually or through simple Macros using VBA code.

I also tried using Google Apps Script (on Google Sheets) to transform my data. The first problem I encountered with this approach was that the processing was far too slow!

I then did some research and stumbled upon the Python programming language. Based on what I heard, the Python language has many data transformation libraries that can speed up my data processing time.

Upon trying out writing some Python code, I quickly realized that the syntax was much easier to understand than Excel VBA.

Once I picked up a basic understanding of this language, I could extract data from Excel spreadsheets without even opening them in Excel!

Before I knew it, I was writing analyzed data to new sheets, and all the analysis could be done time-efficiently.

Now that you’ve seen the potential of Python let me share more!

Python vs VBA: Syntax Comparison

VBA is the main successor of the original Visual Basic event-driven language. VBA’s syntax is much like Visual Basic but has evolved. It uses an object-oriented approach where everything is identified as an object and must have methods, properties, and events associated with it.

Python’s syntax is much easier to read than VBA’s because it takes advantage of indentation and whitespace to show structure.

Python also uses fewer lines of code to accomplish the same tasks as VBA.

Additionally, Python can be extended using modules and packages which makes it a much more powerful and flexible language.

Overall, Python offers a more modern approach to programming with its syntax optimized for readability.

Here’s an example of VBA vs. Python syntax:

VBA code:

Sub My_Procedure()

Dim x As Integer

For x = 1 To 5 Step 2

MsgBox (x)

Next x

End Sub

Python code:

for x in range(1, 6, 2):

print(x)

As you can see, the Python code is much shorter and easier to read. This is just a simple example, but it demonstrates how Python’s syntax can save you time and make your code more readable.

Python vs VBA: Difficulty Comparison

VBA is a simpler language and easier to learn than Python. It’s also not as powerful, so it can’t do as much. Python requires more knowledge of programming fundamentals such as data types, indentation, classes, functions, etc.

This makes it a bit harder to learn than VBA, but still very manageable if you are willing to dedicate some time and effort.

Python is also one of the easiest computer programming languages to learn, so it should not be too tough for beginners.

The payoff of mastering Python is immense, however, since it can be used for many purposes, such as web development, automating tasks, and data analysis.

What is Python?

Python is a high-level, general-purpose programming language. It is one of the most popular interpreted languages used across multiple use cases. It is known for its flexibility in varied applications, especially in data science and analysis.

Python can also be used for web development, software development, data analysis, artificial intelligence (AI), and machine learning (ML)!

Python is also great for automation tasks as it offers many libraries designed to help you complete complex tasks faster than before.

Similar to the experience I had, automating data transformation tasks and performing complex calculations along the way is one of the perfect uses for Python in most businesses.

The Pros of Using Python

1. Easy to Learn and Understand

Python is a simple language that’s easy to learn. It has basic syntax rules and straightforward commands, which makes it easier for beginners to understand.

This means that most of the functions and words used in Python use natural language that are

2. Flexible

One of the defining features of Python is its flexibility. It can be used in many different areas, such as web development, software development, data analysis, AI, and ML.

3. Open Source

Python is open source, so it’s free to use and modify for any purpose. This means that you can freely download the code and customize it to fit your specific needs.

You also have access to a wide variety of libraries and frameworks, which makes programming easier.

4. Speed

Being more optimized for handling data analysis and transformation tasks, Python can perform tasks at a faster speed than VBA.

This makes Python ideal for crunching large amounts of data quickly and efficiently.

5. Better Version Control

Python also has better version control capabilities than VBA. This means that you can easily keep track of modifications made to code and ensure that all changes made are recorded.

This is excellent for debugging and for rolling back versions when a mistake has been made in the code.

The Cons of Using Python

1. Interpreter Errors

Python is an interpreted language, which means that the code is executed in real-time. This can lead to potential interpreter errors, which may take longer to debug than with a compiled language such as VBA.

2. Requires More Setup

Python requires more setup and installation than VBA, which can be a turn-off for beginners.

3. Not Integrated with Microsoft Excel

Python is not as tightly integrated with Microsoft Excel as VBA.

This means that you may have to use separate IDE tools such as Jupyter Notebooks or PyCharm to execute your code.

These programs can be difficult for non-programmers to understand, so it may take some time and effort to become proficient.

Examples Where Python is Better Than VBA

1. Data Analysis

Python is well-suited for data analysis tasks due to its highly optimized libraries, such as Pandas and NumPy. These libraries can crunch large amounts of data compared to VBA.

2. Automation

Python is great for automating repetitive tasks and handling data pipelines, such as generating reports or transforming data using a single script.

What is VBA?

VBA is a language used mainly in Microsoft Office applications, such as Word and Excel. It stands for Visual Basic for Applications and is an implementation of Visual Basic from Microsoft.

VBA is designed to simplify complex tasks by allowing users to create simple macros or scripts that can be used repeatedly. The VBA language is typically used to interact with the Excel Object Model, which can be used for automating repetitive tasks or streamlining complicated processes within Excel.

The Pros of Using VBA

1. Integrations With Microsoft Excel

VBA is tightly integrated with Microsoft Excel and can manipulate worksheet data or control other Office applications. This makes it easier for users to perform tasks from within the familiar Excel environment.

2. Quicker Setup

Since VBA is already available in Microsoft Excel, there’s no need to install additional software or libraries. This makes it easier to get started with VBA than Python.

3. Easier Syntax Rules

VBA has fewer syntax rules compared to Python, which makes it simpler for beginners to understand and use.

The Cons of Using VBA

1. Limited Functionality

VBA is limited in terms of what it can do compared to Python. It’s mainly used for automating tasks within Excel, so it won’t be as useful for web development or data analysis tasks.

VBA is also only available through the Visual Basic Editor on Microsoft Office Programs. This means that VBA cannot be used to extract and manipulate data from external data sources like databases.

2. Poor Error Handling

VBA also has a poor error-handling system, which can cause bugs or unexpected behavior. Debugging VBA code is difficult due to its limited error-handling capabilities.

3. Slow Performance

VBA is not as optimized for handling large amounts of data, so it can be slow compared to Python when processing large datasets.

Examples Where VBA Is Better Than Python

1. Quick Automation Tasks

VBA is great for automating quick and simple tasks within Microsoft Excel. This includes creating reports, generating graphs, or manipulating data with a few lines of code.

Since VBA does not require much setup, it would be perfect for quickly putting together a simple Excel automation.

2. Accessing Restricted Offline Files

For businesses that use the Microsoft suite, there might be situations where only Excel can be used to access data for data security purposes.

This is where VBA will be more useful in accessing such restricted files compared to Python.

3. Non-Technical Users

VBA is great for non-technical users as it has a lower learning curve and simpler syntax rules compared to Python. VBA is easier to pick up and use without having to learn complex programming concepts.

VBA vs Python for Automation

VBA is better suited for quickly automating simple tasks within Excel, while Python is more powerful and versatile for data analysis or web development tasks. However, both VBA and Python have their advantages when it comes to automation tasks.

For users who are new to programming or want something that’s easy to pick up and use without having to learn complex programming concepts, VBA would be the better choice.

Related Questions

Can Python do everything VBA can?

Python can do most of the things VBA can do. However, it is not as tightly integrated with Microsoft Office applications.

Python has libraries to control Excel and manipulate worksheet data, but it requires more setup and is not as straightforward to use compared to VBA.

Can Python replace VBA?

Python cannot replace VBA. VBA has good integrations with Microsoft applications that Python does not have. However, most of the essential functions of data analysis work can be replaced with Python.

Should I learn Python or VBA?

You should learn Python if you want to be able to do data analysis and web development tasks. Python has more powerful functionalities compared to VBA, but it requires more setup and has a steeper learning curve. However, you should learn VBA if you require more custom automation to manipulate objects in Excel.

Both Python and VBA are important languages to learn if you’re planning to enter a career as a data analyst, as these tools will be useful for handling data from a wider range of data sources.

Is VBA still relevant in 2023?

VBA is not relevant in 2023 as there are better and more powerful options available for automating tasks. Python is a popular and versatile language that can be used for many different purposes, so it has become the go-to choice for automation tasks.

VBA developers are also becoming less common as compared to Python developers since Python can cover most of the functions VBA can.

But if you need to quickly automate tasks in Excel without having to learn complex programming concepts, then VBA will still be relevant.

Is Python faster than VBA?

Yes, Python is generally faster than VBA when it comes to processing large datasets. This is because Python has libraries optimized for handling data-intensive tasks.

What is a good Python-to-VBA converter?

The best Python-to-VBA converters are pyxll and ExcelPython. Both of these converters let you convert VBA scripts into Python code. These converters provide a good bridging gap between these VBA and Python.

Python vs Excel: What are the differences?

Excel is a spreadsheet application that can be used to organize and manipulate data. Python is a general-purpose programming language that provides more advanced features like web development and machine learning.

1. Ability to Handle Complex Tasks

Python is better suited for complex tasks, while Excel is better suited for simpler tasks like sorting, filtering, and creating reports.

2. Versatility

Python is also more versatile as it can be used to automate tasks in a variety of applications, while Excel is limited to working with data in spreadsheets.

3. Speed

Python is faster than Excel for complex calculations and data manipulation tasks, but Excel is easier to learn and use for those just getting started with programming or data analysis.

Can Python automate Excel?

Yes, Python can be used to automate tasks in Excel. There are a few different libraries that you can use to control Excel and manipulate worksheet data, such as xlwings or pywin32.

These libraries allow you to programmatically interact with Excel using Python code.

Final Thoughts

In conclusion, Python is a much better solution for data analysis and automation of Excel tasks than VBA. Python has more powerful functions, and libraries are faster for complex calculations and are more versatile for different applications.

However, if you’re just getting started with programming or need something that’s quick to learn and use without having to learn complex concepts, then VBA might be a better choice.

Ultimately, the decision between VBA and Python depends on the requirements of your task and business.

I hope this article has been helpful to you!

I’m a tech nerd, data analyst, and data scientist hungry to learn new skills, tools, and software. I love sharing content with my years of experience in data science, marketing, and tech startups.

Introduction

When beginning your coding career, you will certainly ask yourself questions like, “What exactly is a programming language?”, “Which programming language should I learn”, “What is the difference between language A and language B”, etc.

The answer to these questions and many others can be very challenging because there are an impressive number of programming languages on the market.

The world of computer programming is more or less like soccer with each programming language having supporters that are very vocal about the merits of the language that they are using. In this article, I will talk about programming languages in general before focusing on Python and VBA.

What is a Computer Program and a Programming Language?

A computer program is a detailed plan or procedure for solving a problem with a computer. More specifically, it is an unambiguous, ordered sequence of instructions necessary to achieve a solution.

The distinction between computer programs and equipment is often made by referring to the former as software and the latter as hardware.

A programming language is a set of commands, instructions, and other syntax use to create software. There are many programing languages on the market and they differ from each other by their syntax and purpose.

The History of Programming Languages

Programming languages have a long history, a product of the fact that the types of problems to be solved kept changing and became more and more complex with time.

The genesis of the computer revolution was in a machine. The first programming languages were by then machine-oriented. Thus, machine language was born. As you would imagine, machine language consisted of binaries – ‘0s’ and ‘1s’ representing the switches of the CPU.

Computer programming was reserved only to few experts who understood the structure of the CPU. Then came Assembly languages in 1947 that was easier for humans to understand and read. Almost no one these days writes programs in machine language, but assembly language is still used.

Programming languages such as FORTRAN, BASIC, PASCAL, COBOL, SIMULA, Smalltalk and C were derived from assembly language. These languages are big improvements over assembly language, but they still required you to think in terms of the structure of the computer rather than the structure of the problem you were trying to solve.

The famous C language for example, was designed primarily for the UNIX operating system. Many programming languages that followed such as C++, Java, C#, Visual Basic, PHP, Perl, Python, Ruby, etc., were influenced in one way or another by C, or any of the first ‘imperative’ languages mentioned earlier.

More importantly, programming languages evolve to solve real world problems. Therefore, the evolution of programming languages and the birth of new ones are either driven by industry, commercial or academic needs.

Unfortunately, programming languages have historically sparked almost bitter religious wars. The hatred of one language over another, is an unfortunate reality. Behind the development of programming languages, are large and powerful companies with various vested interests coupled with high rivalry. This rivalry tends to spread to the people working in those companies and to the industry at large.

Types of Programming Languages

As you might have learnt from the history narrated above, there are two types of programming languages: high-level and low-level languages

High-level languages

High-level languages are close to human language and are designed to be easy to be read and understood by humans. They are used by most programmers to write source code in a natural fashion, using logical words and symbols.

In a high-level language, you will have reserved words like If, Else, Do, While etc. or Symbols like <, >, ==, and != which are common operators.

The good news is that many high-level languages are similar enough making it possible for a programmer to easily switch from one language to another. Examples of high-level languages include C++, Java, Perl, and PHP.

High level languages can be grouped in 2 categories which are “compiled languages” and “interpreted languages”. The difference between the two categories is that with a compiled language, the source code must be compiled – converted to a machine language – in other to run, while an interpreted language can be run through an interpreter without being compiled.

Some examples of compiled languages include C++ and Java while Perl and PHP are interpreted languages. Generally, compiled languages are used to create software applications, while interpreted languages are used for running scripts, such as those used to generate content for dynamic or interactive websites.

Low-level languages

Low-level languages are more difficult to read than high-level languages. They are not designed to be human readable but are machine readable. They include assembly and machine languages.

An assembly language contains a list of basic instructions. An assembler can be used to translate the assembly code into machine code. The machine code, or machine language, contains a series of binary codes that are understood directly by a computer’s CPU.

The figure below gives a summary of the type of programming languages:

Levels of lanugages

Visual Basic for Applications (VBA) Vs Python

Which programing language is better between VBA and Python? It is not unusual to read such question on the web. It is more appropriate to ask: which language is more suitable for what I want to achieve rather than knowing which language “is better.”

Computer programming languages are like tools in a toolbox. Different tools are designed to solve different problems. For example, suppose you want to drive a nail through a piece of wood. The ideal tool for this purpose would be a hammer. The same hammer would not be useful if you want to tighten a nut to a bolt, that will be the job a spanner.

Your preference for the hammer over the spanner will not in any way influence the specific use of each tool. Do you get the picture? This is just to say that the choice of a programming language is contextual.

Coming back to our topic of interest, I’ll make the following observations concerning VBA and Python:

  • Phyton and VBA are both high-level languages.
  • Phyton and VBA are both interpreted languages.
  • Python is a General-Purpose Language (GPL) while VBA is a Domain Specific Language (DSL) made just for the Windows environment.
  • As its name suggests, VBA is closely related to Visual Basic and uses the Visual Basic Runtime Library. However, VBA code normally can only run within a host application, rather than as a standalone program. VBA can, however, control one application from another using OLE Automation. For example, VBA can automatically create a Microsoft Word report from Microsoft Excel data that Excel collects automatically from polled sensors. It is worth mentioning that VBA is also implemented, at least partially, in applications published by companies other than Microsoft, including ArcGIS, AutoCAD, CorelDraw, LibreOffice, Reflection, SolidWorks, WordPerfect, and UNICOM System Architect (which supports VBA 7.1).
  • Visual Basic for Applications enables building user-defined functions (UDFs), automating processes and accessing Windows API and other low-level functionality through dynamic-link libraries (DLLs). It’s actually a versatile language with many applications.
  • VBA is limited to automation in Microsoft Office products, while Python can run under all sorts of operating systems, for a wide variety of purposes. Dropbox, for example is written in Python, as are large parts of Google’s software for instance.
  • Python is an Open Source language: Unlike VBA, Python allows users to modify and alter the code in creative ways. What this means is that many libraries have been developed to aid in complex and creative tasks that would be impossible or limited in VBA. Anyone can create and develop a library to support Python, which means the possibilities are unlimited.
  • Python and VBA can handle similar functions when it comes to automating, but Python is capable of dealing with much larger volumes of data than VBA. With Python, calculations are faster and more complex formulas can be handled as compared to Excel’s VBA.
  • Python’s power comes from its libraries. Many of the basic standard libraries, or extensions allow it to perform highly complex tasks with just a few lines of code where Excel-VBA may take much more time and lines of code to perform the same work. Some libraries like plotly and D3 can visualize that data into interactive charts and graphs that are more creative and visually appealing than those of Excel. There are even libraries that incorporate Artificial Intelligence and machine learning, allowing Python users to create predictive and forecasting models that VBA-Excel cannot.
  • Python has a vast community. Python is one of the most popular and growing programming languages. Its community is very active and continually contributes so that the knowledge base and depth of creativity is mined like no other. Stack Overflow is one of the largest online communities where questions are answered and solutions are delivered.

It is obvious from the observations above that Python is by far more powerful than VBA but like I said earlier, what matters is what you intend to achieve. There is no need killing a mosquito with an atomic bomb when a simple spray can do the job.

At times I find VBA-Excel more appropriate for a certain task and much more efficient than Python. If you want to build independent (standalone) applications, then learn Python. On the other hand, if you simply want to automate laborious and repetitive task in Office applications, then go with VBA.

Finally, if you have limited time, then you should definitely learn VBA. Not because Python is hard, but because picking up the basics of VBA is very simple.

Conclusion

When picking a programming language, it is unwise to love or hate one over another. By so doing, you are not only reducing your effectiveness as a programmer but also minimizing your earnings potential. Most of my earnings as a freelancer come from my skills in Excel and VBA. A word to the wise is sufficient.

Tagged with: assembly, high level language, Language, Library, low level language, machine language, python, scripts, VBA, vba vs python, windows

Unlike VBA, using Python for data analysis is cleaner and provides better version control. As a result, most firms are transitioning from VBA to Python. More so, being an open-source enables many users to modify and alter the Python code in creative and more meaningful ways.

Several data analysts and software developers today consider Python a worthy replacement tool for VBA due to the benefits it offers, but the question that is often asked is, “Is Python faster than VBA?”

This is because when it comes to executing actions and performing tasks, faster is always better.

Click this affiliate link to register for Python Certification Training.

VBA Vs. Python: Speed

In most operations, it’s undeniable that VBA is faster than Python. From repeated observations from using both plain VBA and plain Python, it’s obvious that Python is about two to three times slower than VBA. However, this is because both are not called directly from their native environment.

Both VBA and Python are very good languages but have different purposes. VBA is great for Office automation and is typically used for developing Access and Excel apps that not only save time but also enable keeping up with and searching through data very simple for the users. 

Python, on the other hand, is also great for many purposes but not for Office automation with the same ease or performance as using VBA. Hence, in terms of speed, both Python and VBA are fast when they are used for their intended purposes.

More so, for non-intensive mathematical operations where speed is very important, VBA is faster as you would spend significant extra coding time to use Python. However, for intensive mathematical operations, Python offers a fairly easy-to-use and efficient compiled code that is faster than VBA.

Click this affiliate link to register for Microsoft Excel VBA Training.

How Is Speed Measured?

Now that it has been ascertained that both VBA and Python can be very fast when used for their intended purpose but VBA being faster in most applications, let’s examine how the application speed is measured for both programming languages.

Whether you have prior experience under your belt or are new to programming, there will always be a time when you need to measure the amount of time it takes for a procedure or task to complete. This also applies to procedures executed on VBA and Python.

How to Measure Speed and Performance for VBA?

In VBA, this can be done through two major ways using:

∎ The Intrinsic Timer Function

∎ timeGetTime API function (Winmm.dll)

The latter method was used to create a Stopwatch class in VBA. It can measure code for longer periods or performance more accurately than the VBA Timer function.

Intrinsic Timer Functions

VBA has many built-in time functions (members of VBA.DateTime) as shown in the figure below:

Irrespective of how much you want VBA date or time values to track elapsed time, they are not created to do so. Hence, the VBA date and time values store a particular point in time, not a span of time, and they cannot be store later than 24hrs in a given date or time variable.

The Stopwatch Class

The stopwatch class utilizes the “timeGetTime” API Function rather than the intrinsic VBA Timer function. This makes the stopwatch class more accurate and can be used to measure longer periods.

How to Measure Speed and Performance of Python?

You can use either time.time() or time.clock() functions to measure time off a program’s execution. The python docs state that these functions should be used for benchmarking purposes.

Example:

Output:

This will give the output −

The timeit module can be used to get proper statistical analysis of the execution time of a code snippet. This module runs the snippet numerous times and then informs the time taken for the shortest run. This module can be used as follows:

Example:

def f(x):

return x * x

import timeit

timeit.repeat(“for x in range(100): f(x)”, “from __main__ import f”, number=100000)

Click this affiliate link to register for Python Certification Training.

Output

This will give the output –

Factors That Make Excel Quick

This section looks at factors that speed up VBA and make macros run faster.

1. Turn Off Automatic Spreadsheet Calculation

If a new value is entered into an Excel worksheet cell, Excel will recalculate all the other cells that refer to this worksheet cell. So, if the VBA macro is writing values into the worksheet, VBA will have to wait until the worksheet is finished recalculating each entry before it can resume. This takes lots of unnecessary time, but you can turn off this time-wasting automatic calculation by entering this command at the beginning of the macro:

Application.Calculation = xlCalculationManual

If you need to recalculate spreadsheet values while the macro is running, use any of the following commands: 

“Calculate”…. this recalculates the entire workbook. 

“Worksheets(“sheet1”).Calculate”…. this recalculates only a specific sheet. 

“Range(“A1:C5”).Calculate”…. this recalculates only a specific Range.

When you need to turn back on the automatic calculation, use the following command:

Application.Calculation = xlCalculationAutomatic.

2. Turn Off Screen Updates

Each time VBA macros write data to the Excel sheet, it also causes the screen image to refresh. Refreshing the screen image caused the system performance to drag. Therefore, use this command to turn off screen updates.

Application.ScreenUpdating = FALSE

After executing the VBA macro, you should use this command to turn on screen updates.

Application.ScreenUpdating = TRUE

3. Reduce the Traffic Between VBA and The Excel Worksheet

Immediately the VBA macros begin to run; it’s important to stop all unnecessary references to the Excel worksheet. Making reference or grabbing data from the Excel worksheet will slow down performance. More so, if possible, do not read or write worksheet data within loops. It’s faster and better to read data just once and save it in memory than re-reading it every time.

4. Avoid Using Variants in Formulas

Avoid declaring a numerical variable as the Variant except where necessary. Based on repeated tests, the variable types from fastest to slowest in mathematical equations are – Constant, Single, Double, Long, Integer, Variant.

5. Avoid Evaluating Strings

Strings (text) are slow to evaluate. Try to avoid evaluating Strings in code in this manner:

Select Case Gender

Case “Male”

(insert code here)…

Case “Female”

(insert code here)…

Case “Unisex”

(insert code here)…

End Select

Enumeration assigns a constant numerical value to a variable. VBA is able to process enumerated values rapidly while maintaining readable code. Enumeration will assign default numerical values or specific values.

Public Enum enumGender

Male = 0

Female = 1

Unisex = 2

End Enum

Dim Gender as enumGender

Select Case Gender

Case Male

(insert code here)…

Case Female

(insert code here)…

Case Unisex

(insert code here)…

End Select

Boolean operators are simply TRUE or FALSE switches that process really quickly. In the example below, bMale, bFemale, and bUnisex are Boolean variables. The Boolean code is roughly ten times faster than using Strings.

If bMale Then

(insert code here)…

ElseIf bFemale Then

(insert code here)…

ElseIf bUnisex Then

(insert code here)…

End If

6. Don’t Select Specific Worksheets Unless Necessary

You generally do not need to use the Select command to read or write to a worksheet. It is about 30 times faster not to Select a worksheet.

Avoid this:

Worksheets(“sheet1”).Select

Amount1 = Cells(1, 1)

Do this instead:

Amount1 = Worksheets(“sheet1”).Cells(1,1)

7. Avoid Overuse of Statusbar Updates

VBA handled mathematical processes faster than it can display or update the StatusBar. Writing to the StatusBar is a further example of traffic between VBA and Excel. Writing one out of every 100 scenarios to the StatusBar is about 90 times faster than writing every scenario to the StatusBar.

8. Avoid Unnecessary Math

Each mathematical operation slows down the process. So, if your coding uses four numerical within the loop, your operation will be about four times slower. Also, bear in mind that exponentiation is slower than addition, subtraction, multiplication, or division.

Click this affiliate link to register for Microsoft Excel VBA Training.

9. Don’t Copy and Paste

Finally, avoid the Copy and Paste (or PasteSpecial) functions. They are slow. It is about 25 times faster to avoid them and instead use the following command to copy and paste required values.

Range(“A1:Z100”).value = Range(“A101:Z200”).value

Factors That Make Python Slow

Python is a scripting language higher than VBA. This means Python can abstract the details of the computer system from you, i.e., pointers, memory management, etc. It also enables you to write programs that are closer to how human beings reason. This adds to its slowness.

Truly, VBA macros run faster than Python code if only the execution time is the measure, but if the development time is included, then Python may perform better.

Also, another reason why Python code seems to execute more slowly is that it is interpreted at runtime rather than being compiled to native code at compile time. Hence, the reason why Python is slower is that it is an interpreted language.

Click this affiliate link to register for Python Certification Training.

VBA Vs. Python for Data Analysis

Excel is a common tool used for data analysis, and it is frequently used to carry out analytical operations in the financial industry. Nevertheless, Excel can be more complex to use since it requires a VBA application. VBA macros can be very complex to operate. As a result, they make Excel very hard to use when dealing with many operations during data analyses.

∎ Python Is More Powerful

Python and Excel are able to handle similar functions when it comes to data automating and analysis. However, Python is capable of handling much larger volumes of data than Excel. Also, calculations are faster, and formulas can be more complex and specific compared to Excel’s VBA. 

Many of the basic standard Python libraries or extensions, including NumPy and Pandas, can perform data automation and analysis with a few lines of code, whereas Excel may take ten times as many commands to perform the same work. 

Other Python libraries such as Plotly and D3 can visualize that data into interactive charts and graphs that are more creative and visually appealing than the few charts and graphs that are at Excel’s disposal. 

Python also comes with a wide range of preinstalled libraries, which saves time for developers and analysts who would otherwise be required to create projects from scratch.

Python’s power comes from its libraries. Python also has libraries that integrate AI and machine learning, enabling users to create forecasting and predictive with Python that Excel users can’t create.

∎ Functional Integrations are better in Python

A good data analysis software should integrate with other types of analytical and non-analytical software. Python fits well into this category since it integrates well with other programs. Users are able to import and export various kinds of file formats into Python.

To illustrate, Python is compatible with SQL syntax and can also run it within its framework so as to extract data and tables to its environment. The Python environment is also very efficient at automating tasks, like importing data and writing analyzed data to Excel or CSV functions for the purpose of data analysis.

Moving from using Excel to using Python is a justifiable act when considered from the point of view of functional integration. Firstly, Python is user-friendly, and both beginners and experienced analysts can use the language with ease. Excel uses VBA language, which is a personalized platform that makes use of macros to automate tasks for data analysis.

Using macros to automate tasks is more complex than the automating tasks in the Python environment. And the fact that Python can be easily integrated with other programs makes it more suitable for data analysis.

∎ Python has Improved Code Compatibility

Data analysis code in Python can be stored as scripts for further manipulation and reuse. Python code is compatible and reproducible, which makes it suitable for further manipulation by other contributors who are running independent projects. Unlike with the VBA macros used in MS Excel, Python generates a cleaner data analysis and also offers a more superior version control.

Even better is Python’s ability to be consistent and accurate in the execution of code. Other users can also replicate the original code and still maintain a smooth execution at the same level as per the original code. The ability to reproduce code makes Python a lot more efficient than Excel because users can bypass the initial coding process and start off with a framework that is already functioning.

∎ Python has Higher Scalability and Efficiency

Data scientists have a preference for Python over Excel due to its ability to handle large data sets, as well as incorporate modeling and machine learning. Though VBA seems faster, when a large amount of data is to be handled, it takes a longer time for Excel to complete calculations than Python. If data is loaded onto the two programs at the same time, Excel will lag behind Python since it’s not built to handle large amounts of data.

More so, it takes a longer time for Excel to import data created with other analytical tools. It is even slower still when the amount of data being imported into the spreadsheet is large. Python bridges the gap because it is a more efficient tool for importing and exporting data in different formats, which makes it ideal for data scraping. 

When compared to Excel, Python can handle complex calculations, automating tasks, and data pipelines better. It also comes with a wide pool of manipulation tools and libraries.

∎ Python Has a Vast Community

Python code supports numerous extensions and libraries, and while Excel also has some to a certain extent, Python is one of the most popular and developing programming languages with several libraries that exceed tho of Excel. 

But what happens if you’re stuck? What happens if you have problems with your code? Need to generate a chart? There’s Stack Overflow for that. The community is very active and continually makes contributions so that the knowledge base and depth of creativity are mined like no other. Stack Overflow is among the largest online communities where questions are answered, and solutions are delivered.

Nevertheless, there are times when Excel is more appropriate for a certain data analysis task and much more efficient than Python.

∎ Python Handles Larger Data

Although Python is considered a more efficient data analysis tool for complex calculations and large volumes of data, Excel still enjoys greater popularity than Python, and it is used by many people in financial analysis.

While Excel is not the ideal application for handling large volumes of data, it is a more suitable tool for those organizations that have small volumes of data that need simple calculations. 

On the other hand, Python is more efficient than Excel when the organization handles large volumes of data that require automation to produce results within a short period.

xlwings Vs. VBA

Are you a modern Excel developer but want to enjoy some of the efficiencies of Python in Excel, then xlwings is an innovative tool for you. With xlwings, you can use Python to make working in Excel faster. xlwings enables users to take advantage of the combined power of Python and Excel.

xlwings is a Python library that makes certain data analysis features of Python available in an Excel instance, which includes support for NumPy arrays and pandas Series and DataFrames. Like any other Python library, xlwings can be installed via common methods such as pip or conda; however, you can access the documentation for xlwings here if you require additional details.

Xlwings has four major object types, these are (in decreasing hierarchical order): App (in this case the MS Excel), Book, Sheet, and Range.

With xlwings, you can write Excel tools with Python instead of VBA and call your code directly from within Excel, for example, via a button on the sheet. This can also work well when prototyping web apps in macOS and Windows.

Here are what xlwings enables you to do in Excel:

∎ Automate Excel from Python, for example, to produce reports or to interact with Jupyter notebooks.

∎ Write macros in Python that can be run from buttons in Excel, for example, to load in data from an external API or database.

∎ Write UDFs (user-defined functions) and leverage the power from, e.g., NumPy, Pandas, and machine learning libraries.

∎ Remote control Excel, e.g., on an Eikon or Bloomberg terminal or Windows Server via the REST API.

Click this affiliate link to register for Microsoft Excel VBA Training.

Speed of User Versus Speed of Processor

Whether you’re using Python or VBA, the speed of the user is not really a determining factor in the speed of operation or efficiency of data automation and analysis. However, the speed of the processor is very important.

Normally, all Python programs run as a single process and use a single CPU. If your computer was manufactured during the last ten years, there’s a good chance it has four or more CPU cores. Therefore, about 75% of your computer power is left unused while you wait for your Python program to finish running on a single core.

The same thing goes for Excel VBA too. VBA macros cannot take advantage of multiple cores; they only run in a single thread. Execution and calculation time is generally dependent on the speed of your processor. Hence, the speed comes down to how good your processor’s single-threaded performance is.

If your single-thread processor speed is low, it will negatively affect macro performance and vice versa. Any computer with a recent i5-8xxx processor will be about 100% faster than the i3-2330M in macros and about 200% to 300% faster in general calculation performance.

More so, 4 GB RAM is low these days. Always aim for at least 8 GB RAM.

Click this affiliate link to register for Python Certification Training.

Conclusion

From the post above, you have seen that VBA is faster than Python in terms of execution time. But this doesn’t mean that VBA is better than Python. In fact, Python is more useful and valuable than VBA. Coupled with the power of xlwings, you can leverage the power of Python from and with Excel.

BOTi offers certification courses in Excel VBA and Python for those looking to take their careers to the next level in the data analysis and programming fields. Contact us today to keep learning and advancing your career.

Click this affiliate link to register for Microsoft Excel VBA Training.

The way Microsoft is moving with introduction of Office Scripts it does seem like the Redmond giant will not be planning to invest anymore in expanding VBA, however, will leave it as the designated offline scripting language. The challenge there is that while Office Scripts will become more mature we still need to wait a while for them to be fully supported for the desktop version of Excel. Meanwhile VBA is not getting any younger and is not evolving step by step with leading data science languages like Python. While I don’t think VBA will go away in the next 10 years, Microsoft will definitely be trying to get more users onto Office Scripts. But why limit yourself? I personally think today’s analysts should look towards languages and tools that are more versatile to processing data. While Office Scripts will integrate better with Excel and with Microsoft’s Power Suite of tools like Power Automate, I think today’s analysts should look towards languages used more broadly across fields of data science and data engineering.

Python has in the last few years become the language of choice for an overwhelming community of data professionals. If you are looking to migrate your VBA scripts to Python you don’t have to wait! You can start developing your scripts for Excel in Python right away, with only a few extra steps. Let me explain why would you want to consider doing so…

Why switch VBA to Python?

Don’t get me wrong I have created my fair share of VBA tools and automations. If you are creating Access based applications this might not be the alternative for you. However, if you use VBA mainly for creating automations, data cleaning, reports then I think you might appreciate the swap.

Python is the most popular programming language according to the TIOBE index. It is loved by data scientists and data engineers alike. Most machine learning algorithms on Kaggle are being created in Python. Similarly Python is the primary language used to extract, transform and load data used is managed cloud services like Amazon Glue or Azure Databricks. Invest in learning a language that can be more versatile than just for Excel.

Python supports many data formats today you might be processing Excel files. However, whether it is CSV, Excel, text, avro, json or any other type of data file – it shouldn’t really make a difference to you as a developer. Why limit yourself to processing Excel spreadsheets when instead you can learn a language that will help you process any data format instead.

Python is a more mature programming language. While VBA hasn’t seen updates since last 20 years, Python is in it’s 3rd major release, with it’s latest minor 3.10 version released just last autumn. With each release there are new capabilities and improvements to Python.

My journey of switching from VBA to Python

In this post I will try give VBA developers a quick comparison of some example scripts for common scenarios and how they can be recreated in Python. I personally started my programming journey back in the days from C/C++/C#. I started to do more Excel data wrangling and modeling to find I couldn’t do it without scripting. VBA was really the only alternative, as doing it in C# took much more time and languages like Python weren’t really then an alternative.

However, over the years as I moved more into data engineering I appreciated the ease with which you can process all kinds of data files using Databricks or Python Pandas. Over the past years Python has seen the Pandas library evolve and many other Excel dedicated libraries appear such as openpyxl. As of today I think the question of whether to use VBA or Python is not a clear choice anymore and requires considering each use case.

Anaconda for Python development

Albeit there are many IDEs (Integrated Development Environments) available for Python developers out there I suggest installing Anaconda. Anaconda is currently the most popular distribution platform for Python developers and comes with a Python distribution, the Spyder IDE used for data science and Jupyter Notebooks – the leading notebook platform for Python development.

Download the distribution package from the Anaconda website for your operating system (Windows, Apple, Linux) and start the installation.

Install Anaconda

Once installed you can decide if you want to continue coding in Spyder IDE or Jupyter Notebooks. For beginners I recommend Jupyter as it is more verbose and you can run bits and pieces of your code while seeing their outputs.

To get started open Jupyter Notebooks. After a moment you should be welcome with a browser window in which you will be welcome by Jupyter:Jupyter Screen
Now let us create our new Script file. Click on the New button and select Python 3 (ipykernel). That’s it you should see an browser tab open with an unnamed python file ready for action.
Jupyter Create New Python script
You should be all set now to play around with our scripts. If you want to get more familiar with Jupyter I recommend you read this tutorial.

Example 1: Hello World

Let’s start with a very simple example all the while getting used to using Jupyter Notebooks.

Hello World in VBA

With VBA you always need to declare a Sub procedure that runs your code. Hence, we will start by creating our Main Sub and inside we will include a simple Debug.Print statement to output our Hellos on to the Immediate console window

Example 1 - VBA - Hello World

Hello World in Python

In Python if you are running code from an IDE you can simply run the following print statement. Simply type your code into the first cell and hit SHIFT+ENTER to execute it:

Example 1 - Python - Hello World

In case you will be compiling your files you will need to specify the main procedure by defining it and running it like so:

def main():
  print("Hello World!")
  
main()

It is a good practice to always define a main procedure as your get used to creating more complex python executables and packages. However, in my examples below I want to keep the code simple and avoid wrapping it in main.

Example 2: Write to a cell

In our next example we will need to create a blank Excel file and write a single cell and close the file.

Write to a cell in VBA

In VBA we will need to create a new Excel Application (or use the existing one), create a new workbook, open the first worksheet and write text to our first worksheet cell. Lastly we will save our file and close the Excel application.

Sub Main()
    Dim app As Excel.Application
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet
    Dim fileName As String
    
    fileName = "C:...Test.xlsx" 'Replace ... with your file location
    Set app = New Excel.Application
    Set wb = app.Workbooks.Add
    Set ws = wb.Sheets(1)
    
    ws.Cells(1, 1) = "Hello there!"
    
    wb.SaveAs fileName
    app.Quit
End Sub

This code is actually very heavy as you will notice it will take a moment to see the new Excel application being launched.

Write to a cell in Python

In Python we usually fallback on certain libraries that allow us to run certain operations. In the case of Excel, if we want to have a very similar experience to using VBA we can leverage the openpyxl library. It comes with all frequently needed capabilities you might expect from VBA. Additionally it is very lightweight as it operates on the XML structure of Excel files (yep – Excel files are actually packages XML files) instead of having to launch Excel to run the operations for us. What happens below is we create a new Workbook, get the first active worksheet, save the string and save our new file or overwrite the old one.

import openpyxl as op
wb = op.Workbook()
ws = wb.active
ws.cell(1,1).value = "Hello there!"
wb.save(r"C:... Test.xlsx") #Replace ... with your file location

Appreciate how much shorter the code is! Because there is not interoperability with Excel we don’t need to remember about closing the application itself – as it was never open in the first place. In Python and in VBA we don’t really have to declare our variables. However, in VBA we won’t be able to see any hints on the variables methods and attributes if we don’t declare them. In Python, however, if you define a variable, you can create a new row in Jupyter and hit TAB after the dot “.” to see the available methods/attributes or hit SHIFT+TAB to get the documentation of the method. Much more convenient!

Example 3: String manipulation

In many scripting scenarios we often use VBA to do operations on Strings that otherwise would be very cumbersome by using LEFT, RIGHT, FIND or other Excel Formulas. Let’s compare multiple string manipulation examples in VBA vs Python.

String Manipulation in VBA

Usually using Excel formulas it is easy to make changes using standard LEFT, RIGHT functions. What is harder in Excel Formulas is splitting text. Below a simple example in VBA:

Dim full_name As String
full_name = "tom_hardY@google.com"

If InStr(full_name, "@") Then full_name = Split(full_name, "@")(0)
    
first_name = Split(full_name, "_")(0)
last_name = Split(full_name, "_")(1)
    
first_name = StrConv(Trim(first_name), vbProperCase) 'Tom
last_name = StrConv(Trim(last_name), vbProperCase)   'Hardy

String Manipulation in Python

The code in python will look similar, but notice that instead of running functions on text our strings actually ready methods for splitting, capitalizing and trimming text.

full_name = "tom_hardY@google.com"
if full_name.find("@") > 0 : full_name = full_name.split(sep="@")[0]
first_name = full_name.split("_")[0]
last_name = full_name.split("_")[1]
first_name = first_name.strip().capitalize() #Tom
last_name = last_name.strip().capitalize()   #Hardy

Example 4: Iterating over rows

A common task you perform in VBA is iterating over a set of rows in order to run tasks on individuals rows that you would have a hard time processing using Excel Formulas. For this example we will use the following simple Excel table. What I want is to iterate through the rows and print all first and last names of ppl that have an income above 60k.
Iterating Rows - VBA

Iterating over rows in VBA

In VBA we often don’t iterate over entire rows but more often over rows of a specific column (like an id column) in order to filter for patterns and only then do we run tasks on other columns of that particular row.

Below I need to identify all income cells in the G column and then filter only for ones that have values. Next in the loop I am checking whether the value is above 60k and ignoring the header row. To print the first and last name I am offsetting the cell by -5 and -4 columns respectively to capture the first and last names.

Sub IterateRows()
   Dim cell As Range
   Dim ws As Worksheet
   Set ws = Sheets("Sheet1")

   For Each cell In ws.Range("G:G").SpecialCells(xlCellTypeConstants)
        If cell.Value > 60000 And cell.Row > 1 Then
            Debug.Print "First name: " & cell.Offset(, -5) & ", Last name: " & cell.Offset(, -4)
        End If
   Next cell    
End Sub

I would say this is where VBA is a little clumsy as there are a lot of rooms for errors here. You could argue that we could have applied a Table on top of the data set or applied a filter. However, all of these require additional steps.

Iterating over rows in Python

In Python we will fallback on one of the most popular data analysis libraries – Pandas. Now Pandas is loved by data scientists and very versatile due to the fact is can be applied across all kinds of file types and schemas. The key data structure in Pandas is a DataFrame – which basically represents a table with headers.

In the code below, we start by importing Pandas and loading the file and worksheet into df which is a variable with our DataFrame. The DataFrame by default detects the header row (by default the first row). We can use iterrows to iterate through each of the rows of our DataFrame. We can build our filtering directly into the DataFrame by simply df[ df[“income”]>60000 which indicates that we want to filter on all income values above 60k.

import pandas as pd

file_name = r"C:...Test.xlsx" #Replace ... with your path
df = pd.read_excel(file_name, sheet_name="Sheet1")

for r in df[ df["income"]>60000 ].iterrows():
    first_name = r[1]["first_name"]
    last_name = r[1]["last_name"]
    print(f"First Name:{first_name} Last Name:{last_name}")

What is great about DataFrames is you can add them very easily to simplify this task even more by created a series containing only filtered first and last names like below. This is a very elegant solution and more simple that using VBA.

import pandas as pd

file_name = r"C:...Test.xlsx" #Replace ... with your path
df = pd.read_excel(file_name, sheet_name="Sheet1")
df = df[df["income"] > 60000]

names = "First name:" + df["first_name"]+", Last name: " + df["last_name"]
for n in names:
    print(n)

Example 5: Inserting and Deleting Rows

Typically you might use VBA to insert new data rows into existing worksheets or remove existing rows. The problem using VBA is that often this comes at a large computational expense when compared to Python.

Inserting and Deleting Rows in VBA

As aluided above inserts/deletes can be time consuming in VBA – depending on how many rows of data you will have beneath the inserted/deleted rows. This is often because Excel will recompile a lot of calculations or for other reasons.

Sub InsertDeleteRows()

    'Delete row 10
    Range("10:10").EntireRow.Delete
    
    'Insert a row above row 10
    Range("10:10").EntireRow.Insert xlUp
    
End Sub

Inserting and Deleting Rows in Python

As we might not be looking at a structured worksheet we will again fallback on the openpyxl library. As opposed to VBA we need to be working on a closed document. If we want to save our changes we need to finish with save.

import openpyxl as op
file_name = r"C:...Test.xlsx" #Replace ... with your path
wb = op.load_workbook(file_name)

ws = wb["Sheet1"]
ws.delete_rows(10)
ws.insert_rows(10)

wb.save(file_name)

Example 6: Adding and Removing Worksheets

Adding and Removing Worksheets in VBA

In VBA we will need to add a blank worksheet to our ActiveWorkbook (the one open and visible), only then can we name our worksheet. When deleting the worksheet we might run into an issue that we will receive a pop-up window asking us if we want to delete the worksheet. That can be turned off using the DisplayAlerts option but requires an extra step.

Sub AddRemoveWorksheets()

    Dim wb As Workbook
    Dim ws As Worksheet
    Set wb = ActiveWorkbook
    
    'Add worksheet
    Set ws = wb.Worksheets.Add
    ws.Name = "NewWorksheet"
    
    'Find and remove worksheet
    Set ws = wb.Sheets("NewWorksheet")
    ws.Delete
    
End Sub

Adding and Removing Worksheets in Python

In Python both operations are oneliners.

import openpyxl as op
file_name = r"C:...Test.xlsx" #Replace ... with your path
wb = op.load_workbook(file_name)

wb.create_sheet("NewWorksheet")
wb.remove_sheet("NewWorksheet")

wb.save(file_name)

Example 7: Formatting cells

Formatting cells in VBA

Sub FormatCell()

    Dim cell As Range
    Set a1= Range("A1")
    
    a1.Font.Bold = True
    a1.Font.Color = RGB(255, 0, 0)
    a1.Borders.LineStyle = xlDot
    a1.Interior.Color = RGB(0, 255, 0)

End Sub

Formatting cells in Python

In Python to do Excel cell formatting we will need a few additional modules – Font, Color, Border and Side. Once imported the process is pretty similar to how you would do it in VBA.

from openpyxl.styles import colors
from openpyxl.styles import Font, Color, Border, Side
from openpyxl import Workbook
file_name = r"C:...Test.xlsx" #Replace ... with your path
wb = op.load_workbook(file_name)

ws = wb["Sheet1"]
a1 = ws.cell(1,1)
a1.font = Font(bold = True, color="FF0000")
bd = Side(style='thick', color="000000")
a1.border = Border(left=bd, top=bd, right=bd, bottom=bd)

wb.save(file_name)

Example 8: Conditional Statements

Contiational Statements in VBA

Sub ConditionalStatements()

    Dim num As Long
    num = 2
   
    If num > 10 Then
        Debug.Print "Value greater than 10"
    
    ElseIf num > 5 Then
        Debug.Print "Value greater than 5"
    Else
        Debug.Print "Value less or equal to 5"
    End If

End Sub

Conditional Statements in Python

num = 2
if num > 10:
    print("Value greater than 10")
elif num > 5:
    print("Value greater than 5")
else: print("Value less or equal to 5")

Example 9: Message Boxes

Message boxes are an easy way to notify your users of certain events such as finishing running a macro or asking them simple Yes / No questions.

Message Boxes in VBA

In VBA we use the native MsgBox function. They issue is that for Yes/No boxes we need to remember that the function will return a VBA code that we need to compare using one of the built in variables vbYes or vbNo.

Sub MessageBox()

    'Information box
    MsgBox "Hello", vbInformation, "Information"
    
    
    'Yes / No question
    If MsgBox("Do you like this tutorial?", vbYesNo, "Question") = vbYes Then
        Debug.Print "They like it!"
    Else
        Debug.Print "They don't like it!"
    End If
    
End Sub

Excel MessageBox

Message Boxes in Python

Python does not come with a native Message Box function. Instead you need to import a library that has this feature. I recommend using tkinter as it very similar in usage to the MsgBox VBA function.

from tkinter import messagebox

#Information box
messagebox.showinfo("Hello","Information")

#Yes / No question
if messagebox.askyesno("Question", "Do you like this tutorial?"):
    print("They like it!")
else:
    print("They don't like it!")

Python MessageBox

Example 10: Functions

As my last example let us compare how you can create custom functions in VBA vs Python.

Functions in VBA

In VBA we always need to be clear whether we are defining a subroutine or a function. For Functions if you want to specify an undefined number of arguments you can use the ParamArray.

'A Function with 2 arguments
Function AddNumbers(num1 as Long, num2 as Long) as Long
  ret = num1+num2
  AddNumbers = ret
End Function

'A Function with undefined number of arguments
Function AddManyNumbers(ParamArray values() as Variant) as Long
   ret = 0
   For Each v in values
      ret = ret + v
   next v
   AddManyNumbers = ret
End Function

Sub Test()
   Debug.Print AddNumbers 10, 20
   'Result 30

   Debug.Print AddManyNumbers 10, 20, 30
   'Result 60
End Sub

Functions in Python

In Python we do not have to be explicit about subroutines vs functions. You main code will run only when it skips the functions defined from top to bottom as shown below. Additionally if you want to create functions with undefined amounts of arguments you can precede your argument with * (or ** if you want a dictionary instead of a list).

#Function with 2 arguments
def add_numbers(num1, num2):
    ret = num1 + num2
    return ret

#Function with many arguments
def add_many_numbers(*args):
    ret = 0
    for arg in args:
        ret = ret + arg
    return ret

#Test functions
print(add_numbers(10,20))
'Result 30

print(add_many_numbers(10,20,30))
'Result 60

Python vs VBA: Conclusions

While I am not arguing to stop using VBA entirely in you Excel scripts, I think it makes sense to start considering Python for more complex automations/data processing tasks. Conversely when I get asked about what language is a good starter for beginner analysts – I still recommend they start with VBA. With Excel you get an IDE built in, your VBA scripts can get easily shared and appreciated by other folk at your workplace. Let me expand a bit more why VBA might still be your default choice some times:

Why you might still prefer to use VBA?

You should still stick with VBA if you are beginner developer and primarily an Excel user. If you haven’t got a strong grasp of programming and on daily basis you create only simple, usually pre-recorded, scripts I don’t think the switch will provide you a lot of benefit.
What is Python missing?

  • Python isn’t integrated into Excel. You always need to send your scripts separately and assure your users have Python installed on their workstations
  • Python doesn’t run on open documents – in VBA you might be used to seeing your code live on your open documents. Python usually manipulates closed documents, although you might try using interop libraries, however, that defeats the purpose of using Python
  • There is no Excel Macro recorder for Python scripts – if you are a beginner I would still stick with VBA, until you get a good grasp of it

When is the right moment to go Python?

If you are data engineer/data scientist Python makes more sense from the start. You don’t really care if you need to process Excel, csv or json files, data is data. In such cases all the benefits of packaging VBA with Excel files etc. don’t really matter.

  • Python has many data libraries such as numpy and pandas for data processing and mathematics. This includes data science ML/AI libraries such as scikit-learn allowing you to build neural networks, decision trees etc.
  • Python libraries, such as pandas, usually provide similar support for all kinds of data formats. This means you don’t need to learn different approaches for processing csv vs Excel files, instead you just need to get familiar with DataFrames
  • Support for parallel processing – you can parallelize your loops in just a few lines an increase many times over the performance of your scripts. VBA whereas is be default single-threaded and running large scripts can sometimes take hours
  • Processing of large datasets. VBA wasn’t built for processing large datasets and often even storing data in arrays of thousands can cause memory issues or exceptions. Python can easily process millions of rows of data. If you need to process terabytes of data you can use frameworks such as Spark
  • Working with APIs and web scraping. You can run web calls using the XMLHttpObject, however, the experience won’t be as easy as using the Python requests library, Scrapy, BeautifulSoup etc. Especially when you need to use authentication tokens. For precisely this reason Microsoft recommends Office Scripts

Any thoughts?

Like this post? Please share to your friends:
  • Python openpyxl создать excel файл
  • Python openpyxl open excel file
  • Python excel date format
  • Python excel create sheet
  • Python excel cell value