LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Loop more efficient ?

Hi,

I would like to use a For Next loop to go through (several) worksheets
to (among other operations) insert rows when some criteria are met.

The problem i ran into was that the "LastRow" increases when rows are
inserted. So the rows at the end of the worksheet stay the unaffected

To overcome this problem I made two loops. The first to determine how
many rows to add to the current lastRow, and the second loop tot do
the actual things I want the do.


Here is the Code:

Private Sub CommandButton2_Click()

Dim sh As Worksheet
Set sh = ActiveSheet

"Loop 1 to determine how many rows to add to the current lastRow

cnt = 1
For introw = 1 To LastRow(sh)

If Application.IsNumber(Cells(introw, 1)) Then
cnt = cnt + 1

End If

introw = introw + 1

Next introw


"Loop 2 is the actual macro to do the job

For introw = 1 To LastRow(sh) + cnt

If Application.IsNumber(Cells(introw, 1)) Then
Cells(introw, 1).EntireRow.Insert

End If

introw = introw + 1

Next introw

End Sub


I was wondering if the Code I use can be made more efficient.


Help appreciated,


Farmer


 
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
IF and VLOOKUP - how efficient? anthonyg Excel Worksheet Functions 6 April 7th 07 08:45 AM
What is more efficient Brad Excel Discussion (Misc queries) 2 November 20th 06 09:13 PM
Which is more efficient? Norm[_5_] Excel Programming 3 April 2nd 04 04:24 PM
Is there more efficient formula? Diana[_5_] Excel Programming 19 August 22nd 03 10:24 PM
More efficient code Rob Bovey Excel Programming 1 July 9th 03 04:46 AM


All times are GMT +1. The time now is 11:55 PM.

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

About Us

"It's about Microsoft Excel"