Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Help? Anyone see problem with this macro? Way to condense it?

I have written the following macro (some portions borrowed) and the
first two (2) subs run fine and move all of their data to their
respective sheets, but the the third (3rd) only does half the lines,
then if you run it again, half of what is left, and a third time
through completes. I have beat my head against the wall in an attempt
to figure out why 2/3's of it works wonderful and the last 1/3 is not
working? Any ideas? (Thanks in advance)
================================================== ================

Option Explicit
Sub CopyRows1()
Dim rng As Range
Dim cl As Range
Dim str As String

Set rng = ActiveSheet.Range("a2:a6500") 'Range to search (used
range)
str = "X" 'What to look for
For Each cl In rng 'Check each cell

If cl.Text = str Then
'If cell contains the correct value copy it to next empty
row on sheet 2 & delete the row sheet 1
cl.EntireRow.Copy Destination:=Sheet2.Cells(Rows.Count,
1).End(xlUp).Offset(1, 0)
cl.EntireRow.Delete
End If
Next cl
'Run ["Sheet1.CopyRows2"]
End Sub
-----------------------------------------------------------------------------------------------------------------
Sub CopyRows2()
Dim rng As Range
Dim cl As Range
Dim str As String

Set rng = ActiveSheet.Range("a2:a6500") 'Range/Column to search
str = "Y" 'What to look for
For Each cl In rng 'Check each cell

If cl.Text = str Then
'If cell contains the correct value copy it to next empty
row on sheet 3 & delete the row sheet 1
cl.EntireRow.Copy Destination:=Sheet3.Cells(Rows.Count,
1).End(xlUp).Offset(1, 0)
cl.EntireRow.Delete
End If
Next cl
'Run ["Sheet1.CopyRows3"]
End Sub
-----------------------------------------------------------------------------------------------------------
Sub CopyRows3()
Dim rng As Range
Dim cl As Range
Dim str As String

Set rng = ActiveSheet.Range("a2:a50") 'Range/Column to search
str = "A" 'What to look for
For Each cl In rng 'Check each cell

If cl.Text = str Then
'If cell contains the correct value copy it to next empty
row on sheet 4 & delete the row sheet 1
cl.EntireRow.Copy Destination:=Sheet4.Cells(Rows.Count,
1).End(xlUp).Offset(1, 0)
cl.EntireRow.Delete
End If
Next cl
'Run ["Sheet1.CopyRows4"]
End Sub
================================================== =============
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Help? Anyone see problem with this macro? Way to condense it?

Deleting rows in a range that you are traversing can be problematic. Things
sometimes get missed. What you wnat to do is to craverse the entier range
creating a single large range to be copied at the end... (this code will also
be a bit faster as it only does a single copy and a single delete)

Sub CopyRows3()
Dim rng As Range
Dim cl As Range
Dim rngAll as Range
Dim str As String

Set rng = ActiveSheet.Range("a2:a50") 'Range/Column to search
str = "A" 'What to look for
For Each cl In rng 'Check each cell
If cl.Text = str Then
if rngAll is nothing then
set rngall = cl
else
set rngall = union(cl, rngAll)
end if
End If
Next cl
if not rngall is nothing then
rngall.copy Destination:=Sheet4.Cells(Rows.Count,
1).End(xlUp).Offset(1, 0)
rngall.entirerow.delete
end if
'Run ["Sheet1.CopyRows4"]
End Sub

--
HTH...

Jim Thomlinson


"RONZANDER" wrote:

I have written the following macro (some portions borrowed) and the
first two (2) subs run fine and move all of their data to their
respective sheets, but the the third (3rd) only does half the lines,
then if you run it again, half of what is left, and a third time
through completes. I have beat my head against the wall in an attempt
to figure out why 2/3's of it works wonderful and the last 1/3 is not
working? Any ideas? (Thanks in advance)
================================================== ================

Option Explicit
Sub CopyRows1()
Dim rng As Range
Dim cl As Range
Dim str As String

Set rng = ActiveSheet.Range("a2:a6500") 'Range to search (used
range)
str = "X" 'What to look for
For Each cl In rng 'Check each cell

If cl.Text = str Then
'If cell contains the correct value copy it to next empty
row on sheet 2 & delete the row sheet 1
cl.EntireRow.Copy Destination:=Sheet2.Cells(Rows.Count,
1).End(xlUp).Offset(1, 0)
cl.EntireRow.Delete
End If
Next cl
'Run ["Sheet1.CopyRows2"]
End Sub
-----------------------------------------------------------------------------------------------------------------
Sub CopyRows2()
Dim rng As Range
Dim cl As Range
Dim str As String

Set rng = ActiveSheet.Range("a2:a6500") 'Range/Column to search
str = "Y" 'What to look for
For Each cl In rng 'Check each cell

If cl.Text = str Then
'If cell contains the correct value copy it to next empty
row on sheet 3 & delete the row sheet 1
cl.EntireRow.Copy Destination:=Sheet3.Cells(Rows.Count,
1).End(xlUp).Offset(1, 0)
cl.EntireRow.Delete
End If
Next cl
'Run ["Sheet1.CopyRows3"]
End Sub
-----------------------------------------------------------------------------------------------------------
Sub CopyRows3()
Dim rng As Range
Dim cl As Range
Dim str As String

Set rng = ActiveSheet.Range("a2:a50") 'Range/Column to search
str = "A" 'What to look for
For Each cl In rng 'Check each cell

If cl.Text = str Then
'If cell contains the correct value copy it to next empty
row on sheet 4 & delete the row sheet 1
cl.EntireRow.Copy Destination:=Sheet4.Cells(Rows.Count,
1).End(xlUp).Offset(1, 0)
cl.EntireRow.Delete
End If
Next cl
'Run ["Sheet1.CopyRows4"]
End Sub
================================================== =============

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Help? Anyone see problem with this macro? Way to condense it?

It works beautifully and thank you for the assistance. I would like to
understand the differences better however, and the "union" was
something new to me. Could you take a minute and perhaps add some
notes to the code to explain how it funtions and where each
calculation occurs? If not, I do understand and still very much
appreciate the new code.

Ron
================================================
===============================================

On Sep 29, 12:06*pm, Jim Thomlinson <James_Thomlin...@owfg-Re-Move-
This-.com wrote:
Deleting rows in a range that you are traversing can be problematic. Things
sometimes get missed. What you wnat to do is to craverse the entier range
creating a single large range to be copied at the end... (this code will also
be a bit faster as it only does a single copy and a single delete)

Sub CopyRows3()
* * Dim rng * * * *As Range
* * Dim cl * * * * As Range
* * Dim rngAll as Range
* * Dim str * * * *As String

* * Set rng = ActiveSheet.Range("a2:a50") 'Range/Column to search
* * str = "A" 'What to look for
* * For Each cl In rng 'Check each cell
* * * * If cl.Text = str Then
* * * * * * if rngAll is nothing then
* * * * * * * *set rngall = cl
* * * * * * else
* * * * * * * *set rngall = union(cl, rngAll)
* * * * * * end if
* * * * End If
* * Next cl
* * if not rngall is nothing then
* * * rngall.copy Destination:=Sheet4.Cells(Rows.Count,
1).End(xlUp).Offset(1, 0)
* * * rngall.entirerow.delete
* * end if
* * 'Run ["Sheet1.CopyRows4"]
End Sub

--
HTH...

Jim Thomlinson



"RONZANDER" wrote:
I have written the following macro (some portions borrowed) and the
first two (2) subs run fine and move all of their data to their
respective sheets, but the the third (3rd) only does half the lines,
then if you run it again, half of what is left, and a third time
through completes. I have beat my head against the wall in an attempt
to figure out why 2/3's of it works wonderful and the last 1/3 is not
working? Any ideas? (Thanks in advance)
================================================== ================


Option Explicit
Sub CopyRows1()
* * Dim rng * * * *As Range
* * Dim cl * * * * As Range
* * Dim str * * * *As String


* * Set rng = ActiveSheet.Range("a2:a6500") 'Range to search (used
range)
* * str = "X" 'What to look for
* * For Each cl In rng 'Check each cell


* * * * If cl.Text = str Then
* * * * * * *'If cell contains the correct value copy it to next empty
row on sheet 2 & *delete the row sheet 1
* * * * * * cl.EntireRow.Copy Destination:=Sheet2.Cells(Rows.Count,
1).End(xlUp).Offset(1, 0)
* * * * * * cl.EntireRow.Delete
* * * * End If
* * Next cl
* * 'Run ["Sheet1.CopyRows2"]
End Sub
---------------------------------------------------------------------------*--------------------------------------
Sub CopyRows2()
* * Dim rng * * * *As Range
* * Dim cl * * * * As Range
* * Dim str * * * *As String


* * Set rng = ActiveSheet.Range("a2:a6500") 'Range/Column to search
* * str = "Y" 'What to look for
* * For Each cl In rng 'Check each cell


* * * * If cl.Text = str Then
* * * * * * *'If cell contains the correct value copy it to next empty
row on sheet 3 & *delete the row sheet 1
* * * * * * cl.EntireRow.Copy Destination:=Sheet3.Cells(Rows.Count,
1).End(xlUp).Offset(1, 0)
* * * * * * cl.EntireRow.Delete
* * * * End If
* * Next cl
* * 'Run ["Sheet1.CopyRows3"]
End Sub
---------------------------------------------------------------------------*--------------------------------
Sub CopyRows3()
* * Dim rng * * * *As Range
* * Dim cl * * * * As Range
* * Dim str * * * *As String


* * Set rng = ActiveSheet.Range("a2:a50") 'Range/Column to search
* * str = "A" 'What to look for
* * For Each cl In rng 'Check each cell


* * * * If cl.Text = str Then
* * * * * * *'If cell contains the correct value copy it to next empty
row on sheet 4 & *delete the row sheet 1
* * * * * * cl.EntireRow.Copy Destination:=Sheet4.Cells(Rows.Count,
1).End(xlUp).Offset(1, 0)
* * * * * * cl.EntireRow.Delete
* * * * End If
* * Next cl
* * 'Run ["Sheet1.CopyRows4"]
End Sub
================================================== =============- Hide quoted text -


- Show quoted text -


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
Condense a formula gotroots Excel Worksheet Functions 1 December 16th 09 03:25 PM
condense List Lita Excel Worksheet Functions 5 November 21st 08 06:00 AM
Condense Code Nigel Excel Programming 2 December 22nd 05 11:59 PM
Condense formula Derek Y via OfficeKB.com Excel Worksheet Functions 9 November 18th 05 03:00 AM
Help to condense a formula GerryK Excel Worksheet Functions 1 June 29th 05 08:19 PM


All times are GMT +1. The time now is 04:41 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"