How To Use Excel VBA In Python

Поделиться
HTML-код
  • Опубликовано: 5 окт 2024
  • What if we could control the same Excel object model we use in VBA, in Python? Well lucky for Windows users, there is a way we can leverage the Excel object model in Python with the Win32 library. In this video, we introduce our series and explore how to do basic tasks in Excel using this library.
    Video Resources:
    --------------------------------------------------
    Resource: GitHub File
    Link: github.com/are...
    Resource: Sigma Coding Pythoncom Folder
    Link: github.com/are...
    Resource: Sigma Coding Excel VBA Folder
    Link: github.com/are...
    Resource: Sigma Coding PowerPoint VBA Folder
    Link: github.com/are...
    Resource: Sigma Coding Word VBA Folder
    Link: github.com/are...
    Resources:
    --------------------------------------------------
    Facebook Page: / codingsigma
    Facebook Group: / sigmacoding
    GitHub Sigma Coding: github.com/are...
    Support Sigma Coding:
    --------------------------------------------------
    Patreon: / sigmacoding
    Amazon Associates: amzn.to/3bsTI5P **
    Related Topics:
    --------------------------------------------------
    Title: How To Copy Between Excel And PowerPoint In Python
    Link: • How To Copy Between Ex...
    Title: How To Make A Python API Request Using The Excel VBA Library
    Link: • How To Make A Python A...
    Title: How To Run Python Code In An Open Excel Workbook
    Link: • How To Run Python Code...
    Title: How To Create Python Libraries For VBA
    Link: • How To Create Python L...
    Title: Web Scraping With Excel Using Python
    Link: • Web Scraping With Exce...
    Title: Using Excel Constants In The Python Win32 Library
    Link: • Using Excel Constants ...
    Title: How To Create Python Formulas For Excel
    Link: • How To Create Python F...
    Title: How To Use PowerPoint VBA In Python
    Link: • How To Use PowerPoint ...
    Title: How To Use Python's Pandas With The VBA Library
    Link: • How To Use Python's Pa...
    Title: Early Vs Late Binding In Win32Com
    Link: • Early Vs Late Binding ...
    Title: How To Use Excel VBA In Python
    Link: • How To Use Excel VBA I...
    Title: How to Use the PyITypeInfo Object in Pythoncom
    Link: • How to Use the PyIType...
    Title: How To Get All Running COM Objects in Pythoncom
    Link: • How To Get All Running...
    Title: How to Create Python Array Formulas for Excel
    Link: • How to Create Python A...
    Title: How to Use the PyIDispatch Object in Pythoncom
    Link: • How to Use the PyIDisp...
    Title: Using Events in Python Win32 | Part 1
    Link: • Using Events in Python...
    Title: Using Events in Python Win32 | Part 2
    Link: • Using Events in Python...
    Title: Using SQLite & Excel With Win32 In Python
    Link: • Using SQLite & Excel W...
    Title: How to Use Word VBA in Python
    Link: • How to Use Word VBA in...
    **Amazon Associates Disclosure:
    --------------------------------------------------
    I am a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. One of the ways I support the channel is by using Amazon Associates to earn fees on purchases you make. That means when you use the link above, it will track any purchases made from that link and give a small portion of it to the Sigma Coding. I love this approach because it allows you to do what you're already doing (shopping) but also helps support the channels you care about. Also, it makes it where I can invest that revenue to help improve and grow the channel.
    Tags:
    --------------------------------------------------
    #Win32COM #VBA #Excel

Комментарии • 69

  • @tomaszwisniewski319
    @tomaszwisniewski319 4 года назад +2

    I am beginner with python programming and this is for me super helpful, great tutorials thank you

  • @ahawk1220
    @ahawk1220 5 лет назад +10

    00:00 Introduction
    1:35 Library introduction
    2:20 Creating instance of Excel
    3:45 Create a new workbook
    4:55 Create new sheet in workbook
    5:50 Set reference to a range of cells
    6:20 Set values into cells
    6:50 Set a reference to a range of cells
    8:05 Count the cells in our range
    8:50 Loop through the range of cells
    12:50 Conclusion

  • @deldridg
    @deldridg 5 лет назад +1

    Once again - many thanks. I have previously put some effort into integrating C# with Excel (Excel DNA) but now after switching attention over to Python for its incredible speed and libraries directed to large scale analytics, I'm excited to explore the possibilities you are sharing. For example, I have one client who is adamant on using Excel but will soon require some heavier lifting, especially in the area of machine learning and this could be the perfect opportunity for me to get into this exciting combo of technologies. Thanks again from Sydney - Dave

    • @SigmaCoding
      @SigmaCoding  5 лет назад

      That's definitely one of the benefits of using Python with Excel is now we get access to so many libraries that are built for these data heavy task.

  • @Brupie1
    @Brupie1 5 лет назад +5

    Thanks for this video. By the way, early in the video you mention that using Python gives Excel access to Dictionaries. Dictionaries can also be accessed using the Microsoft Scripting Runtime library.

  • @pstefan86
    @pstefan86 5 лет назад +3

    Your channel is great - I'd love to see more of a 'deep dive' into win32. You can even control mouse movements and clicks; it's really powerful!

    • @SigmaCoding
      @SigmaCoding  5 лет назад +1

      You can expect more on Win32 in the near future, it's been a very popular topic so I've been putting together some code so I can make some more videos.

  • @bli240
    @bli240 5 лет назад +3

    Great informative video and thanks for taking the time to post it! I keep hearing how awesome Python is at automating tasks in Excel however I still struggle at understanding its value proposition especially for those comfortable w/ VBA like myself. While Python and Excel integration seems great especially given how old VBA is, features such as intellisense aren’t included which impacts development time.

  • @hassanullahshah4256
    @hassanullahshah4256 3 года назад +1

    It is pertinent to mention that first you have to install Win32com Lib using command on command prompt "python -m pip install pywin32". Then u can import win32com.client

  • @JHatLpool
    @JHatLpool 2 года назад

    A really nice demonstration. Thanks for posting.

  • @lasercho4338
    @lasercho4338 5 лет назад +2

    good video. I always use and praise the OPENPYXL, but this way also look useful.

    • @SigmaCoding
      @SigmaCoding  5 лет назад

      Yeah for Excel we have a ton of options, it’s really nice. I’ve been loving this Library for Access and PowerPoint

  • @FabricioBiazzotto
    @FabricioBiazzotto 4 года назад

    That "for i in range" loop was useless on that video... to make it work, use the previous "for CellItem in ExcRng2" loop and change "CellItem.Address" with "CellItem.Value".
    By the way, nice video. Thank you by sharing your knowledge.

  • @nirajbaraili7128
    @nirajbaraili7128 5 лет назад +1

    Excellent video...Hope to see more on this.

    • @SigmaCoding
      @SigmaCoding  5 лет назад +1

      I'm glad you like it, It's been a very interesting topic to cover! I hope to have a video up soon where we can create a VBE like environment where we can run code in an open workbook using Jupyter notebooks. I think that will be a big help for people who are used to running code in open worksbooks!

  • @neosinan1
    @neosinan1 5 лет назад +2

    I was wondering If I can use my SAP/vba scripts in Python, it looks like I can. After polishing my python skills I will come back to this, So I can intergrate both of these. Thanks man.

    • @SigmaCoding
      @SigmaCoding  5 лет назад

      My guess would be yes, as I've seen VBA be integrated with SAP in the past.

    • @neosinan1
      @neosinan1 5 лет назад

      @@SigmaCoding I was already doing VBA part, I was wondering if using python was posibble with VBA in the manner you showed, which you showed in this video. Thanks for the tutorial and the answer.

    • @FabricioBiazzotto
      @FabricioBiazzotto 4 года назад +1

      Yes, you can. I am working with Python and SAP on my daily job tasks. Just look for "SAP GUI Scripting" and you will know how to do that.

  • @romanzdk
    @romanzdk 5 лет назад +2

    This is absolutely amazing.

  • @尤諠荏
    @尤諠荏 Год назад

    Cool Man, Thx for sharing this really cool stuff

  • @IcanCwhatUsay
    @IcanCwhatUsay 5 лет назад +1

    I'm sorry, I only have one thumbs up to give you. Great tutorial!

  • @gdorman619
    @gdorman619 3 года назад

    Love your videos!

  • @carlosmejia8111
    @carlosmejia8111 5 лет назад +1

    Thank you for your videos. It would be very nice if you post or give us a copy of what you type because is very difficult to follow you.

    • @SigmaCoding
      @SigmaCoding  5 лет назад +1

      Already in the process, I'm planning on making the code available on GitHub. I'll make an announcement when the code has been uploaded to GitHub.

  • @dharmatejasingampalli6480
    @dharmatejasingampalli6480 2 года назад

    Is it possible to load any external dll... like how we load a dll using ...Declare Function

  • @giuseppepresti2859
    @giuseppepresti2859 4 года назад +1

    Hello, great video. Do you know how I can activate a macro from my python code?

    • @SigmaCoding
      @SigmaCoding  4 года назад

      I added a file on my GitHub page that will walk through a simple example. Here is the link:
      github.com/areed1192/sigma_coding_youtube/blob/master/python/python-vba/Lesson%2011%20-%20Calling%20Macros%20From%20Win32COM.py

  • @amitmanolkar
    @amitmanolkar 3 года назад

    Can we call python script/ exe file from excel using VBA an then using python store value in excel VBA variable?

  • @luisangel15
    @luisangel15 4 года назад +1

    Hi! Is there a way to manipulate Pivot Tables using Python?

    • @SigmaCoding
      @SigmaCoding  4 года назад +1

      Yes you can, here is a simple example using the Win32COM library.
      import win32com.client as win32
      # Define the Excel App
      xlApp = win32.GetActiveObject('Excel.Application')
      # Grab the Pivot Table
      xlPvt = xlApp.Workbooks("Create Pivot Table.xlsm").Worksheets("Pivot_Table").PivotTables("MyNewPivotTable")
      # Print the name.
      print(xlPvt.Name)
      # Count the number of fields in the Pivot Table.
      print(xlPvt.PivotFields().Count)

  • @DequanHarrison
    @DequanHarrison 5 лет назад +1

    I'm new to coding and I've been confused whether to learn VBA or Python. I hear VBA is not supported anymore by Microsoft and they're thinking of migrating over to Javascript. So, do you think I should learn VBA, Python, or Javascript. I just want to be able to manipulate excel spreadsheets, combine data, shape, read, write, etc. as VBA allows, but Python/Javascript are more marketable I guess in terms of job outlook. Thoughts?

    • @SigmaCoding
      @SigmaCoding  5 лет назад +2

      If you learn JavaScript, it sounds like it'll be primarily for the Office API. Keep in mind that API is still under development and I haven't seen a time line as to when it will be completely finished. I would focus on Python at this point only because you can still access the VBA model from it and it's just a good language overall.
      You'll hear different opinions from a whole bunch of people but I personally find Python the most intuitive and user friendly. Regarding VBA, I think it'll be a long time before it's truly gone because too many companies have too much invested in it. Microsoft has already tried pulling the plug on VBA but it had mediocre results as people still kept using it.

    • @DequanHarrison
      @DequanHarrison 5 лет назад

      @@SigmaCoding Thanks for answering my question! Last question, seeing that I can in fact access the VBA model with Python ... Do you recommend going the Python Win32com VBA route or Python Pandas, Openpyxl, etc route in regards to accessing the excel model?

    • @SigmaCoding
      @SigmaCoding  5 лет назад +1

      If your goal is simply to load and transform data, stick with Pandas. Win32COM is an excellent library if you need to do more complex with the Excel application itself, but if you're just loading and analyzing data, there are better libraries.
      If you're having to do things like work with data between Excel & PowerPoint or access shapes inside an Excel document, then Win32COM is for you.

    • @DequanHarrison
      @DequanHarrison 5 лет назад

      @@SigmaCoding Thank you for taking the time to answer my question. Love your channel! A wealth of information and knowledge!

  • @umakanthsahu8122
    @umakanthsahu8122 3 года назад

    sir, it's opening in background, is there any way that launches it in foreground

  • @monishnarendra1747
    @monishnarendra1747 5 лет назад +1

    Is there a way of selecting a populated activex combo box using python???

    • @SigmaCoding
      @SigmaCoding  5 лет назад +1

      I'm going to have to explore this some more. I'm not too familiar with this topic, but my gut is telling me yes.

  • @xBaphometHx
    @xBaphometHx 3 года назад

    I tried to merge several Workbooks as separate Worksheets in a new Excel file with Pandas but it didn't respect the formatting. How can I do that with Win32com?

  • @seungbumyang
    @seungbumyang 5 лет назад

    Great!!! Would you tell me how to open the existing excel file for updating the data?

    • @SigmaCoding
      @SigmaCoding  5 лет назад

      Hi Steve, if you write:
      import win32com.client as win32
      ExcelApp = win32.Dispatch("Excel.Application")
      workbook = ExcelApp.Workbooks.Open("")
      that will open an existing Excel Workbook and then you would just make the updates as necessary.

    • @bli240
      @bli240 5 лет назад

      @@SigmaCoding Just a note to use "\\" for the file path as opposed to just "\". Just learned that the hard way. Not sure if it was specific to my PC though.

  • @anirudhpal3505
    @anirudhpal3505 4 года назад

    How to process the value of excel cell having a formula using python?

  • @josepmirimorales687
    @josepmirimorales687 5 лет назад +1

    Very interesting, but is not easy to see what you are tiping , its blur.

    • @SigmaCoding
      @SigmaCoding  5 лет назад

      I’ll try to zoom in on the next video. Hopefully that will fix it. Also the video is uploaded as HD, I don’t know if you have it at a lower quality I’ve noticed that causes it sometimes.

    • @ignacioa3698
      @ignacioa3698 5 лет назад +1

      Go to the Settings... then change the Quality to 720pHD. That will give you crystal clear videos with no issues at all. That should do the trick.

  • @sujimonthankappan6832
    @sujimonthankappan6832 5 лет назад +1

    can you share the Jupyter notebook

    • @SigmaCoding
      @SigmaCoding  5 лет назад

      It's already on GitHub, but I just realized the link isn't in the description. Here is the link: bit.ly/2VXmtAr
      Also, the description should be updated.

  • @jakeg9711
    @jakeg9711 4 года назад

    Window doesnt open automatically when executing the code? Any ideas? Using VS Code. thanks !

  • @solomonadrian1365
    @solomonadrian1365 4 года назад

    This is the first video in Python VBA Model but where is the video to show how to set this up. I see this runs on localhost, do I need apache? I only used openpyxl os this is new for me.

    • @SigmaCoding
      @SigmaCoding  4 года назад +2

      Hey Solomon, when I made this video, I was running it in something called a Jupyter Notebook. That, in short, is really just an open-source web application we can use to share code in a "document-format". It sounds like you don't have it installed on your system. I would recommend you do the following:
      1. Download Anaconda, which is a distribution of Python that comes we a lot of popular packages, including the package for Jupyter Notebooks. Here is the download link: www.anaconda.com/distribution/
      2. If you want, you can just run this code in an application called Visual Studio Code. It's a lightweight code editor I also use in my Videos. Here is the download link: code.visualstudio.com/
      Let me know if that gets you in a good spot.

    • @solomonadrian1365
      @solomonadrian1365 4 года назад

      ​@@SigmaCoding Will do and will watch all the videos. Thanks for the reply and hard work.

  • @chiyto
    @chiyto 4 года назад

    Is there specific documentation for this library? Can't find it in the interwebs

    • @SigmaCoding
      @SigmaCoding  4 года назад

      Well it depends on what type of documentation you're looking for. If you're looking for VBA specific documentation, there isn't really one for Win32COM expect for little examples here and there. If you want documentation related to pythoncom/win32COM then here is that link: timgolden.me.uk/pywin32-docs/pythoncom.html

  • @sameersetia1859
    @sameersetia1859 5 лет назад +1

    How can i call vba macros using this feature

    • @SigmaCoding
      @SigmaCoding  5 лет назад

      Is this going to be a macro in your Personal Macro Workbook or is this going to be in the workbook that has your data?

    • @bli240
      @bli240 5 лет назад +1

      The 2nd answer to this question on Stack Overflow would help: stackoverflow.com/questions/19616205/running-an-excel-macro-via-python

    • @sameersetia1859
      @sameersetia1859 5 лет назад +1

      Hey thanks i found out

  • @amit.mohite
    @amit.mohite 4 года назад

    Title says VBA but you are showing example which just use Excel Application without even touching VBA

    • @SigmaCoding
      @SigmaCoding  4 года назад

      Yeah this video was supposed to serve more as an introduction to the topic, it's not intended to go over every object in the model. Sorry, if I gave the wrong impression from the title.
      However, If you look in the description you'll see links to other videos in this series. Hopefully, you'll find the content you're looking for there. :)

  • @mak32
    @mak32 2 года назад

    when i start code, python say me: "aborted (disconnected)"

  • @unhott1893
    @unhott1893 4 года назад +1

    Very cool. I think you made an error in your for i in ... loop. The only reason it worked was coincidence-- the celItem is referring to the cellItem from the last loop, D7, and printing out D7.value over and over again. Does ExcelRng2[i].value work?

    • @SigmaCoding
      @SigmaCoding  4 года назад +1

      Good catch, I'm looking back at it and I'm trying to understand why I wrote it like that because it doesn't make sense. Technically I'm just printing the last value assigned CellItem. The proper way is pretty much what you're doing up above.
      ExcelRng.Cells(i, 4).Value

    • @paulmeesters
      @paulmeesters 2 года назад

      @@SigmaCoding That isn't correct either. i will start from 0 (zero) and thus causing an error. You could use something like this: print(excelRng2.Cells(4,i+1).Value)

  • @coomerlad3293
    @coomerlad3293 3 года назад +1

    420 likes, lets fucking go