How To Reference Cells with Variables Using xlwings?

Method

#xlwings

i=3    #Create a variable `i`

sht.range(“C”+ str(i)).select()    #Reference a cell using the variable `i`

#sht.cells(i,i).select()

 

#xlwings API

#i=3

#sht.api.Range(“C”+ str(i)).Select()

#sht.api.Cells(i,i).Select()

Sample Code

#Referencing Cells Using Variables

import xlwings as xw    #Import the xlwings package

app=xw.App()
bk=app.books.active    #Get the active workbook
sht=bk.sheets.active    #Get the active worksheet

#xlwings
i=3    #Create a variable `i`
sht.range("C"+ str(i)).select()    #Reference a cell using the variable `i`
#sht.cells(i,i).select()

#xlwings API
#i=3
#sht.api.Range("C"+ str(i)).Select()
#sht.api.Cells(i,i).Select()

#bk.close()
#app.kill()
Reference Cells with Variables Using xlwings
January 27, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *