By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

Pywin32 Excel question

P: n/a
I posted the following message to the Pywin32 list but if anybody here
can help, it would be appreciated very much.

============================
Hi list,

I have a need to copy 3 rows of data from the top of my Excel
spreadsheet to another location. I would have throught that this
should be very straightforward since I've done a fair amount of
Excel/Python programming. Unforturnately, I am stuck on this one.

The VB Macro says I need to:

Range("1:1,2:2,3:3").Select
Range("A3").Activate
Selection.Copy
Rows("20:20").Select
ActiveSheet.Paste

So, I figure the Python code would be something like:

<xlApp determined already>
1) xlSheet=xlApp.ActiveWorkbook.ActiveSheet
2) xlSel=xlSheet.Range("1:1,2:2,3:3").Select()
3) #xlSel=xlSheet.Range("A3").Activate()
4) xlSel.Copy()
5) xlSheet.Rows("20:20").Select()
6) xlSheet.Paste()

Unfortunately, this doesn't work. After line 2, xlSel becomes "True" -
not a "Selection" and so the code fails at line 4). I am not sure why
I have to do the "Activate" on line 3 but it didn't matter, the code
still fails at line 4.
What am I doing wrong?

Any help is greatly appreciated.

Regards,

Aug 5 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

John Henry wrote:
I posted the following message to the Pywin32 list but if anybody here
can help, it would be appreciated very much.

============================
Hi list,

I have a need to copy 3 rows of data from the top of my Excel
spreadsheet to another location. I would have throught that this
should be very straightforward since I've done a fair amount of
Excel/Python programming. Unforturnately, I am stuck on this one.

The VB Macro says I need to:

Range("1:1,2:2,3:3").Select
Range("A3").Activate
Selection.Copy
Rows("20:20").Select
ActiveSheet.Paste

So, I figure the Python code would be something like:

<xlApp determined already>
1) xlSheet=xlApp.ActiveWorkbook.ActiveSheet
2) xlSel=xlSheet.Range("1:1,2:2,3:3").Select()
3) #xlSel=xlSheet.Range("A3").Activate()
4) xlSel.Copy()
5) xlSheet.Rows("20:20").Select()
6) xlSheet.Paste()

Unfortunately, this doesn't work. After line 2, xlSel becomes "True" -
not a "Selection" and so the code fails at line 4). I am not sure why
I have to do the "Activate" on line 3 but it didn't matter, the code
still fails at line 4.
My first guess is that the True returned in step 2 merely tells you
the requested selection succeeded (the cells on the worksheet
became highlighted). What would happen if you requested 500
columns? Would you get False?

My second guess is that step 4 should be xlSheet.Copy() based
on your using xlSheet.Paste().

For that matter, step 5 doesn't assign the result of the select to a
variable. Perhaps that indicates that the assignment isn't necessary?
Or maybe you should test that the selection succeeded before
attempting to paste?
>

What am I doing wrong?

Any help is greatly appreciated.

Regards,
Aug 5 '06 #2

P: n/a
Somebody on the Pywin32 list helped. The problem is that:

xlSel=xlSheet.Range("1:1,2:2,3:3").Select()

is wrong.

It should be:

xlSel=xlSheet.Range("1:1,2:2,3:3")
xlSel.Select()

Then I can do the rest.

And no, you don't want to do the xlSheet.Copy(). That copies the
entire workbook and you end up with a new workbook.

But you have to do xlSheet.Paste(). Go figure!


me********@aol.com wrote:
John Henry wrote:
I posted the following message to the Pywin32 list but if anybody here
can help, it would be appreciated very much.

============================
Hi list,

I have a need to copy 3 rows of data from the top of my Excel
spreadsheet to another location. I would have throught that this
should be very straightforward since I've done a fair amount of
Excel/Python programming. Unforturnately, I am stuck on this one.

The VB Macro says I need to:

Range("1:1,2:2,3:3").Select
Range("A3").Activate
Selection.Copy
Rows("20:20").Select
ActiveSheet.Paste

So, I figure the Python code would be something like:

<xlApp determined already>
1) xlSheet=xlApp.ActiveWorkbook.ActiveSheet
2) xlSel=xlSheet.Range("1:1,2:2,3:3").Select()
3) #xlSel=xlSheet.Range("A3").Activate()
4) xlSel.Copy()
5) xlSheet.Rows("20:20").Select()
6) xlSheet.Paste()

Unfortunately, this doesn't work. After line 2, xlSel becomes "True" -
not a "Selection" and so the code fails at line 4). I am not sure why
I have to do the "Activate" on line 3 but it didn't matter, the code
still fails at line 4.

My first guess is that the True returned in step 2 merely tells you
the requested selection succeeded (the cells on the worksheet
became highlighted). What would happen if you requested 500
columns? Would you get False?

My second guess is that step 4 should be xlSheet.Copy() based
on your using xlSheet.Paste().

For that matter, step 5 doesn't assign the result of the select to a
variable. Perhaps that indicates that the assignment isn't necessary?
Or maybe you should test that the selection succeeded before
attempting to paste?


What am I doing wrong?

Any help is greatly appreciated.

Regards,
Aug 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.