473,587 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

excel find last column

Hi all,

I am trying to find the last used column in an excel sheet using win32com:

lastcol = sh.UsedRange.Co lumns.Count

works, but only if there is no formatting. I want to allow formatting in the columns.

I would rather use the code below because formatted cells are irrelevant.
using:

lastcol = sh.UsedRange.Fi nd("*",xlPrevio us,xlByColumns) .Column

I get the error that xlPrevious is not defined.

I am using Activestate PythonWin 2.3.5. makepy.py cannot be located.
Is makepy.py needed? If so, where do I find it and how do I install
it? Seems like I ran into this problem before but I cannot remember
how I solved it?
Lance
Feb 8 '07 #1
3 9956
>
I get the error that xlPrevious is not defined.
If you are using pythonwin, run the COM Makepy utility on Microsoft
Excel Object Library. Then you have access to the defined constants as
follows.
>>import win32com.client
win32com.clie nt.constants.xl Previous
2

Hope this helps.

Paul
Feb 9 '07 #2
I ran makepy.py and loaded Microsoft Excel Object Library 11.0
I have imported:

import win32com.client
from win32com.client import constants
import re
import codecs,win32com .client
import time
import datetime
import win32com.client .dynamic
using this expression

lastcol = sh.UsedRange.Fi nd("*", "A1", win32com.client .constant.Searc hOrder=xlByColu mns, win32com.client .constants.Sear chDirection=xlP revious).Column

I get error:

, line 245
lastcol = sh.UsedRange.Fi nd("*", "A1", win32com.client .constant.Searc hOrder=xlByColu mns, win32com.client .constants.Sear chDirection=xlP revious).Column
SyntaxError: keyword can't be an expression

Tool completed with exit code 1
Guess I am getting closer to the solution? LOL
Sorry for being ignorant but I am a newbie and I don't understand all this python/excel stuff.

Lance



Lu*****@gmail.c om wrote:
>I get the error that xlPrevious is not defined.

If you are using pythonwin, run the COM Makepy utility on Microsoft
Excel Object Library. Then you have access to the defined constants as
follows.
>>>import win32com.client
win32com.cli ent.constants.x lPrevious
2

Hope this helps.

Paul

Feb 9 '07 #3
Lance Hoffmeyer wrote:
I ran makepy.py and loaded Microsoft Excel Object Library 11.0
I have imported:

import win32com.client
from win32com.client import constants
import re
import codecs,win32com .client
import time
import datetime
import win32com.client .dynamic
using this expression

lastcol = sh.UsedRange.Fi nd("*", "A1", win32com.client .constant.Searc hOrder=xlByColu mns, win32com.client .constants.Sear chDirection=xlP revious).Column

I get error:

, line 245
lastcol = sh.UsedRange.Fi nd("*", "A1", win32com.client .constant.Searc hOrder=xlByColu mns, win32com.client .constants.Sear chDirection=xlP revious).Column
SyntaxError: keyword can't be an expression
I suspect you're getting unnecessarily (but understandably)
confused by the wrapping which the win32com does for you.

Basically, when you run the makepy stuff, a module is generated
(which you can go and look at if you feel so inclined) which
defines the operations this COM object allows. To handle the
constants, a sort of pseudo module is available to you called
win32com.client .constants which you use just like any other
Python module.

In the interpreter dump below, I import the win32com.client
stuff and force the module to be generated programatically
(essentially the same as calling makepy against the
Excel.Applicati on COM library).

Then the constants "module" contains, among other things,
the xlByColumns constant.

<dump>
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright" , "credits" or "license" for more information.
>>import win32com.client
xl = win32com.client .gencache.Ensur eDispatch ("Excel.Applica tion")
win32com.clie nt.constants.xl ByColumns
2
>>>
</dump>

If you want to you can use the usual shortcuts:

const = win32com.client .constants
print const.xlByColum ns
# or even
xlByColumns = const.xlByColum ns

and use that anywhere you need, so in your example:

sh.UsedRange.Fi nd (
"*",
"A1",
SearchOrder=con st.xlByColumns,
SearchDirectory =const.xlPrevio us
).Column

NB I haven't bothered to see whether what your code
is doing is correct, merely illustrating the use
of constants.

HTH a bit
TJG
Feb 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
35519
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet and extract information from specific worksheets and cells. I'm not really sure how to get started with this process. I ran the COM Makepy...
3
11207
by: Lynn A. | last post by:
I have to perform an import from an excel file to Access. The file comes originally as a csv file. I have to delete some rows and columns, then change the formatting of some columns. I am using Windows 2000, Office 2000 I have been able to do everything but fill a column with zeros Here is some of the code attached to a command button,...
0
4685
by: Peter | last post by:
I am having a problem reading an Excel file that is XML based. The directory I am reading contains Excel files that can be of two types. Either generic Microsoft based or XML based. I am reading the Microsoft based files with an OleDbDataAdapter. Then filling the contents of the first worksheet into a dataset. However when I try to add the...
0
1211
by: rhett | last post by:
howdy folks, first off, I'd like to not that I have browsed the forums and thanks to what I've found there I'm 80% through this problem, but the last part seems to be a profound obstacle and any help you could provide would be much appreciated. As my title indicates, I'm devloping a C# application which will need to opne up an excel...
22
6436
by: DThreadgill | last post by:
Hi all - having a bit of problem with this code. It works in one of the databases that I have but not the one I'm currently working with...I've compared references and they're both the same: When trying to get the last column of data from each worksheet in my Excel file, it's giving me the last column as "c" when it should be "AH" or "AI,'...
0
3192
by: Taxman | last post by:
Windows XP, MS Office Excel 2003 If the tasks, I’m trying accomplish have been addressed previously (separately or in combination). Please, provide the links or keyword search to find them. I’ve been searching for code for each part of the task separately and trying to piece together multiple macros, that do something similar, to what I’m...
0
6216
by: Tony Hine | last post by:
Problem for Excel Developers One of the problems facing Excel developers moving into MS Access is actually the apparent similarity between MS Access tables and Excel spreadsheets. MS Access is NOT Excel This similarity of the “look” in both programs, the layout of the data, leads to the Excel developer mistakenly thinking that a database...
8
1479
by: Question123 | last post by:
Hi Friends How to read excel worksheet on server in reverse direction??
7
12057
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.