Learning Python, from a VBA programmer's perspective

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
It has often been said that if one were to learn a new programming language, Python would be the choice of many.

I have dabbled with it and know some basics but do not feel comfortable.

If I can solve the equivalent problem in Excel, then it'll make more sense.

Suppose I have a column of numbers in column A of Excel and simply want to add 10 to every cell. I know how to do that in VBA but how would one go about in Python?

The thing holding me back is that in Excel VBA, you have the luxury of properties such as Range, Column, Cells, etc, I can write a For Each Loop of the range or alternatively I can read the entire column into an array, manipulate, then use Resize to paste it back onto the worksheet.

I know you have tuples, lists, etc. in Python.

Thanks
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I use both Excel and Python at my work regularly, their combination is a very powerful tool set.

The good thing about Python is that amazing people have built powerful open source libraries that allow you do almost everything you can imagine.

To answer your question, you have some numbers in column A, and you want to add 10 to every cell. Assuming the column A has as column head "Col A"

Code:
import pandas as pd
df = pd.read_excel('your_file.xls')
df['Col B'] = df['Col A'] + 10

df here refers to a "dataframe", what does it look like? It's basically a tabular data, exactly the same as an Excel sheet!
1585585645222.png
 
Upvote 0
Th
I use both Excel and Python at my work regularly, their combination is a very powerful tool set.

The good thing about Python is that amazing people have built powerful open source libraries that allow you do almost everything you can imagine.

To answer your question, you have some numbers in column A, and you want to add 10 to every cell. Assuming the column A has as column head "Col A"

Code:
import pandas as pd
df = pd.read_excel('your_file.xls')
df['Col B'] = df['Col A'] + 10

df here refers to a "dataframe", what does it look like? It's basically a tabular data, exactly the same as an Excel sheet!
View attachment 10171

Thanks for this, greatly appreciated.
 
Upvote 0

Forum statistics

Threads
1,224,043
Messages
6,176,046
Members
452,700
Latest member
iamkmon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top