[python-win32] Re: Excel -- Range() not working

Roger Upole rwupole at msn.com
Thu Apr 2 14:59:51 CEST 2009


Gerald Reeder wrote:
> Hello,
>
> I'm trying to get values from a non-contiguous range, specifically
> "E5:E10,E35"
>
> Range("E5:E10,E35").Value -- only returns the values for "E5:E10", 
> ignoring
> "E35"
> Range("E5:E10,E35").Address -- however returns "E5:E10,E35"
> Range("E5:E10,E35").Count -- returns 7, which is correct
>
> I'm early-binding and using Python 2.6 and Pywin2.6.1
>
> from win32com.client import Dispatch
>
>    xl = Dispatch('Excel.Application')
>    xl.Visible = 0
>    xl.DisplayAlerts = 0
>    xl.Workbooks.Open(xl_file)
>    wrkbk = xl.ActiveWorkbook
>    sht = wrkbk.Worksheets(1)
>    print sht.Range("E5:E10,E35").Value
>    print sht.Range("E5:E10,E35").Address
>    wrkbk.Close()
>    xl.Quit()
>
> Any ideas? I've searched the complete archive and I've read Mark and 
> Andy's
> (excellent) book.
> A VBA Macro works with this Range syntax.
>
> Thank you,
> JR
>

Using some VBScript, I also get 6 items for Range.Value.

Dim xl, r, s
set xl = GetObject( "z:\\tempxp\\tmp.xls")
set r = xl.Worksheets(1).Range ("E5:E10,E35")
Wscript.Echo (LBound(r.Value))
Wscript.Echo (UBound(r.Value))

Looks like you can still access the extra item
using

sht.Range("E5:E10,E35").Cells[6].Value

      Roger



More information about the python-win32 mailing list