Search code examples
pythonpywin32

Store a function variable's value to use it again next time function is called


I have a function that determines which Excel row to highlight. But I only want one row to be highlighted at a time. So, when the function is called again, I want it to remove the previous row's highlighting (i.e., highlight it white), and highlight the new row.

I could just have the function highlight the whole sheet white each time it's called. But I don't really want to do that just in case a user has other rows they already highlighted.

Here's the function I have that's being called:

def highlightSol(self, id):        
    for row, cell in enumerate(self.xl.ActiveSheet.Range('A:A')):
        if id == cell:
            row = self.row + 1
            self.xl.ActiveSheet.Cells(row,1).EntireRow.Interior.Color = 1

This works to highlight the row. But I'd like the value of row to remain somehow so that when this function is called again, I can unhighlight that row (or, technically, highlight it another color).

This is probably pretty easy but for some reason I'm stuck thinking of a solution. Any help would be greatly appreciated. Many thanks!


Solution

  • Set an attribute for the last highlighted element: self.highlightRow = row

    You could set the attribute to None in your class's __init__. It is then just a matter of reading that attribute and un-highlighting that row.