+ Reply to Thread
Results 1 to 12 of 12

KeyError with Python in excel

  1. #1
    Registered User
    Join Date
    09-30-2024
    Location
    Netherlands
    MS-Off Ver
    Version 2408 (Build 17928.20156 Click-to-Run)
    Posts
    7

    Exclamation KeyError with Python in excel

    Hello everyone,

    This is my first post on the forum, so please excuse my mistakes if it's been mislabeled.

    I face this KeyError in python in excel when i attpempt to obtain the value of cell AF12 (see the inserted image):
    Portlantis.png
    In this situation I use a variable that contains the string AF12 as input for my xl() function.

    When i try to directly call the value of the same cell using the following code: target = xl('AF12'), I don't face any errors.

    Can anyone tell me what's wrong with it?

    All help is appreciated!

  2. #2
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: KeyError with Python in excel

    How about something like

    PHP Code: 
    import pandas as pd
    data 
    xl("A1:R15")
    col 5
    row 
    12
    data 
    data.iloc[row-1,col-1

  3. #3
    Registered User
    Join Date
    09-30-2024
    Location
    Netherlands
    MS-Off Ver
    Version 2408 (Build 17928.20156 Click-to-Run)
    Posts
    7

    Re: KeyError with Python in excel

    Thanks for the reply!

    I think what you're doing is trying to obtain the data in the cell using pandas. This doesn't work for me, because what I eventually want is to be able to live edit the cells. That's why I am going for python in excel instead of openpyxl or pandas. I actually already made a prototype that does exactly what I want using openpyxl, but it gives me a corrupt file or one that has no links and macros. So my goal atm is to live edit instead using python in excel, but then i got this weird error.

  4. #4
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: KeyError with Python in excel

    I actually don't understand what you are trying to do exactly. Maybe with an example file it will be clearer.
    For example, do you really need Python here?

  5. #5
    Registered User
    Join Date
    09-30-2024
    Location
    Netherlands
    MS-Off Ver
    Version 2408 (Build 17928.20156 Click-to-Run)
    Posts
    7

    Re: KeyError with Python in excel

    I can share an example file when i am at home, but i can't share the file itself as it's confidential. The idea is simple however, I have a large project (one big contract) that's made up out of smaller projects (small contracts). The small contracts aren't decided at the start of the timeline, so their number vary heavily. The goal is that excel will read the delivery dates from a column, say C, and calculate the number of cells to the right (in the planning) it has to go to reach the cell the corresponding date. Then it should color it red. I am not sure if excel can do this. I already did it with openpyxl, but it gives a corrupted file back due to how many things are going on at the same time in the document. Not to mention it's a coauthoring document which already makes openpyxl unwanted since it can't handle that.

  6. #6
    Forum Contributor
    Join Date
    10-02-2012
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    293

    Re: KeyError with Python in excel

    You can use Pandas inside Excel. Python for Excel supports 1000s of packages.

  7. #7
    Registered User
    Join Date
    09-30-2024
    Location
    Netherlands
    MS-Off Ver
    Version 2408 (Build 17928.20156 Click-to-Run)
    Posts
    7

    Re: KeyError with Python in excel

    I will try and see if it can live edit the cell and let you know, thanks!

  8. #8
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: KeyError with Python in excel

    I would recommend checking out xlwings to anyone familiar with VBA who wants to use Python to interface with Excel.
    xlwings.org

  9. #9
    Registered User
    Join Date
    09-30-2024
    Location
    Netherlands
    MS-Off Ver
    Version 2408 (Build 17928.20156 Click-to-Run)
    Posts
    7

    Re: KeyError with Python in excel

    I actually followed through with this. I made my code in xlwings and it runs as I want it to, thanks a lot! Any ideas on how I should approach turning it into something similar to an excel function? The goal is to have it run on it's own when a new entry is made in excel. I am not familiar with VBA btw,i am just starting to code x), but I would be happy to look into it if that's what gets the job done. Thanks for the suggestion!

  10. #10
    Registered User
    Join Date
    09-30-2024
    Location
    Netherlands
    MS-Off Ver
    Version 2408 (Build 17928.20156 Click-to-Run)
    Posts
    7

    Re: KeyError with Python in excel

    I tried doing that but it still saves to a new file, but I managed to get it done with xlwings. Thanks for the suggestion though!

  11. #11
    Registered User
    Join Date
    09-30-2024
    Location
    Netherlands
    MS-Off Ver
    Version 2408 (Build 17928.20156 Click-to-Run)
    Posts
    7

    Re: KeyError with Python in excel

    I tried importing xlwings instead, but I don't think it's supported

  12. #12
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: KeyError with Python in excel

    It looks like you need a change event in VBA...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Python In Excel Still Not Received
    By Dal123 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-31-2023, 06:26 AM
  2. Python vs VBA with Excel
    By andrewc in forum Excel General
    Replies: 2
    Last Post: 07-18-2021, 06:58 AM
  3. How to use python language with excel
    By jorel in forum Excel General
    Replies: 4
    Last Post: 05-14-2018, 03:12 AM
  4. Python or VBA in excel
    By songhaegyo in forum The Water Cooler
    Replies: 49
    Last Post: 01-11-2018, 12:40 PM
  5. python in excel
    By idandush in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-05-2015, 04:26 PM
  6. Python commands in Excel
    By ljoseph in forum Excel General
    Replies: 0
    Last Post: 09-18-2009, 07:02 AM
  7. Excel and Python
    By davidlawrence in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-24-2008, 08:42 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1