Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Runtime Error


Hi, I have a program that will loop a folder, look for excel file and
process it. The following is part of the code I'm using.

Set xlBook = Workbooks.Open(filename)
For k = 1 To xlBook.Worksheets.Count Step 1
maxRow = xlBook.Sheets(k).UsedRange.Rows.Count
Debug.Print "maxrow is ", maxRow
For i = maxRow To 2 Step -1
'process the data
Next i

The problem I'm facing currently, when I run the macro, I received a
overflow runtime error on the maxRow =
xlBook.Sheets(k).UsedRange.Rows.Count. The strange thing is I only
receive this error when I run the program with certain files.
Can you tell me how to overcome this problem?

Thanks a lot..


------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~View and post usenet messages directly from http://www.ExcelForum.com/

~~Now Available: Financial Statements.xls, a step by step guide to creating financial statements
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 78
Default Runtime Error

Do you use Option Explicit?
If so, how is maxRow dimensioned? It needs to be:
Dim maxRow As Long

More than 32767 rows will break an integer.

Kevin Beckham

-----Original Message-----

Hi, I have a program that will loop a folder, look for

excel file and
process it. The following is part of the code I'm using.

Set xlBook = Workbooks.Open(filename)
For k = 1 To xlBook.Worksheets.Count Step 1
maxRow = xlBook.Sheets(k).UsedRange.Rows.Count
Debug.Print "maxrow is ", maxRow
For i = maxRow To 2 Step -1
'process the data
Next i

The problem I'm facing currently, when I run the macro, I

received a
overflow runtime error on the maxRow =
xlBook.Sheets(k).UsedRange.Rows.Count. The strange thing

is I only
receive this error when I run the program with certain

files.
Can you tell me how to overcome this problem?

Thanks a lot..


------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~View and post usenet messages directly from

http://www.ExcelForum.com/

~~Now Available: Financial Statements.xls, a step by step

guide to creating financial statements
.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default Runtime Error


maxRow = xlBook.Sheets(k).UsedRange.Rows.Count

this is incorrect. the count of rows in the used rows
range is not the max row where the used range does not
begin at row 1.

Either:
maxRow = xlBook.Worksheets(k).UsedRange.Rows.Count + _
xlBook.Worksheets(k).UsedRange.Row

OR:
maxRow = xlBook.Worksheets(k).Cells.SpecialCells
(xlCellTypeLastCell).Row

note I used the "Worksheets" property
NOT "Sheets" ....Sheets includes graphs & XL5 macro sheets

Patrick Molloy
Microsoft Excel MVP

-----Original Message-----

Hi, I have a program that will loop a folder, look for

excel file and
process it. The following is part of the code I'm using.

Set xlBook = Workbooks.Open(filename)
For k = 1 To xlBook.Worksheets.Count Step 1
maxRow = xlBook.Sheets(k).UsedRange.Rows.Count
Debug.Print "maxrow is ", maxRow
For i = maxRow To 2 Step -1
'process the data
Next i

The problem I'm facing currently, when I run the macro,

I received a
overflow runtime error on the maxRow =
xlBook.Sheets(k).UsedRange.Rows.Count. The strange

thing is I only
receive this error when I run the program with certain

files.
Can you tell me how to overcome this problem?

Thanks a lot..


------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~View and post usenet messages directly from

http://www.ExcelForum.com/

~~Now Available: Financial Statements.xls, a step by

step guide to creating financial statements
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Runtime Error

I think Patrick wanted to subtract 1 from his first formula.

maxRow = xlBook.Worksheets(k).UsedRange.Rows.Count + _
xlBook.Worksheets(k).UsedRange.Row -1

Another option:

With xlBook.Worksheets(k).UsedRange
maxRow = .Rows(.Rows.Count).Row
End With

A lot like Patrick's second formula.

Patrick Molloy wrote:

maxRow = xlBook.Sheets(k).UsedRange.Rows.Count

this is incorrect. the count of rows in the used rows
range is not the max row where the used range does not
begin at row 1.

Either:
maxRow = xlBook.Worksheets(k).UsedRange.Rows.Count + _
xlBook.Worksheets(k).UsedRange.Row

OR:
maxRow = xlBook.Worksheets(k).Cells.SpecialCells
(xlCellTypeLastCell).Row

note I used the "Worksheets" property
NOT "Sheets" ....Sheets includes graphs & XL5 macro sheets

Patrick Molloy
Microsoft Excel MVP

-----Original Message-----

Hi, I have a program that will loop a folder, look for

excel file and
process it. The following is part of the code I'm using.

Set xlBook = Workbooks.Open(filename)
For k = 1 To xlBook.Worksheets.Count Step 1
maxRow = xlBook.Sheets(k).UsedRange.Rows.Count
Debug.Print "maxrow is ", maxRow
For i = maxRow To 2 Step -1
'process the data
Next i

The problem I'm facing currently, when I run the macro,

I received a
overflow runtime error on the maxRow =
xlBook.Sheets(k).UsedRange.Rows.Count. The strange

thing is I only
receive this error when I run the program with certain

files.
Can you tell me how to overcome this problem?

Thanks a lot..


------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~View and post usenet messages directly from

http://www.ExcelForum.com/

~~Now Available: Financial Statements.xls, a step by

step guide to creating financial statements
.


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
xpath error? Runtime Error 13 type mismatch Steve M[_2_] Excel Discussion (Misc queries) 0 January 17th 08 01:16 AM
xpath error? Runtime Error 13 type mismatch SteveM Excel Discussion (Misc queries) 1 December 4th 07 09:16 AM
Runtime error '1004' General ODBC error star_lucas New Users to Excel 0 August 29th 05 04:09 PM
Excel 2003 Macro Error - Runtime error 1004 Cow Excel Discussion (Misc queries) 2 June 7th 05 01:40 PM
Unknown where is the problem on the Runtime error - Automation error wellie Excel Programming 1 July 10th 03 08:12 AM


All times are GMT +1. The time now is 11:20 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"