Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Copy Non-Consecutive Rows and Paste in a Seperate Worksheet

I have two worksheets I'm working on.
The first has a long list of items characterized in by one of four
identifiers (Adam, Bob, Charlie, David). So it would be something like this:

Adam Task 1 Due date X
Bob Task 34 Due date Y
Adam Task 2 Due date Z
Charlie Task 34 Due date Y
David Task 34 Due date Y
....

I want to make it so all of the rows for Adam (an none of the other rows)
are copied and pasted on to a separate worksheet. Then I will do the same for
Bob, Charlie, and David.

To make it even more complex, I do not want to past the entire contents of
each row, just certain columns. Here, let's say columns 1 and 2. So, the
resulting page for Adam would look like:
Adam Task 1
Adam Task 2

I've been wrestling with this for hours and can't get anywhere. Help! Thank
you very much!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,420
Default Copy Non-Consecutive Rows and Paste in a Seperate Worksheet

Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<=== change to suit
Dim i As Long
Dim LastRow As Long
Dim NextRow As Long
Dim sh As String

With ActiveSheet

LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = 1 To LastRow

If .Cells(i, "A").Value < sh Then

sh = .Cells(i, "A").Value
On Error Resume Next
If Not Worksheets(sh).Name = sh Then Worksheets.Add.Name =
sh
On Error GoTo 0
If Worksheets(sh).Range("A1").Value = "" Then

NextRow = 1
Else

NextRow = Worksheets(sh).Range("A" &
..Rows.Count).End(xlUp).Row + 1
End If

.Cells(i, "A").Resize(, 2).Copy
Worksheets(sh).Cells(NextRow, "A")
End If
Next i
End With
End Sub

--
__________________________________
HTH

Bob

"elf27" wrote in message
...
I have two worksheets I'm working on.
The first has a long list of items characterized in by one of four
identifiers (Adam, Bob, Charlie, David). So it would be something like
this:

Adam Task 1 Due date X
Bob Task 34 Due date Y
Adam Task 2 Due date Z
Charlie Task 34 Due date Y
David Task 34 Due date Y
...

I want to make it so all of the rows for Adam (an none of the other rows)
are copied and pasted on to a separate worksheet. Then I will do the same
for
Bob, Charlie, and David.

To make it even more complex, I do not want to past the entire contents of
each row, just certain columns. Here, let's say columns 1 and 2. So, the
resulting page for Adam would look like:
Adam Task 1
Adam Task 2

I've been wrestling with this for hours and can't get anywhere. Help!
Thank
you very much!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Copy Non-Consecutive Rows and Paste in a Seperate Worksheet

Bob,
Thanks for your reply.
I'm getting a compiling error though because of an Else without an If. In
this portion of the code:

If Worksheets(sh).Range("A1").Value = "" Then NextRow = 1
Else NextRow = Worksheets(sh).Range("A" &
..Rows.Count).End(xlUp).Row + 1


I realize there's an if there, but Excel doesn't.


Thanks again.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,420
Default Copy Non-Consecutive Rows and Paste in a Seperate Worksheet

You seem to have rolled individual lines in my code into 1. The NG also
wrapped lines, see if this is any better

Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<=== change to suit
Dim i As Long
Dim LastRow As Long
Dim NextRow As Long
Dim sh As String

With ActiveSheet

LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = 1 To LastRow

If .Cells(i, "A").Value < sh Then

sh = .Cells(i, "A").Value
On Error Resume Next
If Not Worksheets(sh).Name = sh Then _
Worksheets.Add.Name = sh
On Error GoTo 0
If Worksheets(sh).Range("A1").Value = "" Then

NextRow = 1
Else

NextRow = Worksheets(sh).Range("A" & _
.Rows.Count).End(xlUp).Row + 1
End If

.Cells(i, "A").Resize(, 2).Copy _
Worksheets(sh).Cells(NextRow, "A")
End If
Next i
End With
End Sub


--
__________________________________
HTH

Bob

"elf27" wrote in message
...
Bob,
Thanks for your reply.
I'm getting a compiling error though because of an Else without an If. In
this portion of the code:

If Worksheets(sh).Range("A1").Value = "" Then NextRow = 1
Else NextRow = Worksheets(sh).Range("A" &
.Rows.Count).End(xlUp).Row + 1


I realize there's an if there, but Excel doesn't.


Thanks again.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Copy Non-Consecutive Rows and Paste in a Seperate Worksheet

Still not working.
Getting an "error 9. Subscript out of range" on the line:
If Worksheets(sh).Range("A1").Value = "" Then

For some reason, it's creating a new sheet but not naming it (it remains
'Sheet 1') while sh has the file name as the value. That's why when it tries
to go to the worksheet called sh it's out of range.



"Bob Phillips" wrote:

You seem to have rolled individual lines in my code into 1. The NG also
wrapped lines, see if this is any better

Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<=== change to suit
Dim i As Long
Dim LastRow As Long
Dim NextRow As Long
Dim sh As String

With ActiveSheet

LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = 1 To LastRow

If .Cells(i, "A").Value < sh Then

sh = .Cells(i, "A").Value
On Error Resume Next
If Not Worksheets(sh).Name = sh Then _
Worksheets.Add.Name = sh
On Error GoTo 0
If Worksheets(sh).Range("A1").Value = "" Then

NextRow = 1
Else

NextRow = Worksheets(sh).Range("A" & _
.Rows.Count).End(xlUp).Row + 1
End If

.Cells(i, "A").Resize(, 2).Copy _
Worksheets(sh).Cells(NextRow, "A")
End If
Next i
End With
End Sub


--
__________________________________
HTH

Bob

"elf27" wrote in message
...
Bob,
Thanks for your reply.
I'm getting a compiling error though because of an Else without an If. In
this portion of the code:

If Worksheets(sh).Range("A1").Value = "" Then NextRow = 1
Else NextRow = Worksheets(sh).Range("A" &
.Rows.Count).End(xlUp).Row + 1


I realize there's an if there, but Excel doesn't.


Thanks again.






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Copy Non-Consecutive Rows and Paste in a Seperate Worksheet

Actually, previous post was wrong. I got it to work, but the problem is that
it created 130 sheets and then errored out.
Each sheet only had one line of information

"Bob Phillips" wrote:

You seem to have rolled individual lines in my code into 1. The NG also
wrapped lines, see if this is any better

Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<=== change to suit
Dim i As Long
Dim LastRow As Long
Dim NextRow As Long
Dim sh As String

With ActiveSheet

LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = 1 To LastRow

If .Cells(i, "A").Value < sh Then

sh = .Cells(i, "A").Value
On Error Resume Next
If Not Worksheets(sh).Name = sh Then _
Worksheets.Add.Name = sh
On Error GoTo 0
If Worksheets(sh).Range("A1").Value = "" Then

NextRow = 1
Else

NextRow = Worksheets(sh).Range("A" & _
.Rows.Count).End(xlUp).Row + 1
End If

.Cells(i, "A").Resize(, 2).Copy _
Worksheets(sh).Cells(NextRow, "A")
End If
Next i
End With
End Sub


--
__________________________________
HTH

Bob

"elf27" wrote in message
...
Bob,
Thanks for your reply.
I'm getting a compiling error though because of an Else without an If. In
this portion of the code:

If Worksheets(sh).Range("A1").Value = "" Then NextRow = 1
Else NextRow = Worksheets(sh).Range("A" &
.Rows.Count).End(xlUp).Row + 1


I realize there's an if there, but Excel doesn't.


Thanks again.




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Copy Non-Consecutive Rows and Paste in a Seperate Worksheet

Do you have empty cells in column A?

How about strings that contain invalid characters in those cells (maybe dates
using the slash character)?

Add a line to help debug the program:

If .Cells(i, "A").Value < sh Then
msgbox i & vblf & .cells(i,"A").value
sh = .Cells(i, "A").Value

I bet Bob's routine would work fine with the test data you shared. But the fix
may depend on what's in those cells that's causing the error.

elf27 wrote:

Still not working.
Getting an "error 9. Subscript out of range" on the line:
If Worksheets(sh).Range("A1").Value = "" Then

For some reason, it's creating a new sheet but not naming it (it remains
'Sheet 1') while sh has the file name as the value. That's why when it tries
to go to the worksheet called sh it's out of range.

"Bob Phillips" wrote:

You seem to have rolled individual lines in my code into 1. The NG also
wrapped lines, see if this is any better

Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<=== change to suit
Dim i As Long
Dim LastRow As Long
Dim NextRow As Long
Dim sh As String

With ActiveSheet

LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = 1 To LastRow

If .Cells(i, "A").Value < sh Then

sh = .Cells(i, "A").Value
On Error Resume Next
If Not Worksheets(sh).Name = sh Then _
Worksheets.Add.Name = sh
On Error GoTo 0
If Worksheets(sh).Range("A1").Value = "" Then

NextRow = 1
Else

NextRow = Worksheets(sh).Range("A" & _
.Rows.Count).End(xlUp).Row + 1
End If

.Cells(i, "A").Resize(, 2).Copy _
Worksheets(sh).Cells(NextRow, "A")
End If
Next i
End With
End Sub


--
__________________________________
HTH

Bob

"elf27" wrote in message
...
Bob,
Thanks for your reply.
I'm getting a compiling error though because of an Else without an If. In
this portion of the code:

If Worksheets(sh).Range("A1").Value = "" Then NextRow = 1
Else NextRow = Worksheets(sh).Range("A" &
.Rows.Count).End(xlUp).Row + 1


I realize there's an if there, but Excel doesn't.


Thanks again.





--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 199
Default Copy Non-Consecutive Rows and Paste in a Seperate Worksheet

How about adding some line and change codes a little like below.

keiji

elf27 wrote:
Actually, previous post was wrong. I got it to work, but the problem is that
it created 130 sheets and then errored out.
Each sheet only had one line of information

"Bob Phillips" wrote:

You seem to have rolled individual lines in my code into 1. The NG also
wrapped lines, see if this is any better

Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<=== change to suit
Dim i As Long
Dim LastRow As Long
Dim NextRow As Long
Dim sh As String


add the code below

Dim Acsh as Worksheet

set Acsh=ActiveSheet


Change the line below

With ActiveSheet


to

With Acsh


LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = 1 To LastRow

If .Cells(i, "A").Value < sh Then

sh = .Cells(i, "A").Value
On Error Resume Next
If Not Worksheets(sh).Name = sh Then _
Worksheets.Add.Name = sh
On Error GoTo 0
If Worksheets(sh).Range("A1").Value = "" Then

NextRow = 1
Else

NextRow = Worksheets(sh).Range("A" & _
.Rows.Count).End(xlUp).Row + 1
End If

.Cells(i, "A").Resize(, 2).Copy _
Worksheets(sh).Cells(NextRow, "A")
End If
Next i
End With
End Sub


--
__________________________________
HTH

Bob

"elf27" wrote in message
...
Bob,
Thanks for your reply.
I'm getting a compiling error though because of an Else without an If. In
this portion of the code:

If Worksheets(sh).Range("A1").Value = "" Then NextRow = 1
Else NextRow = Worksheets(sh).Range("A" &
.Rows.Count).End(xlUp).Row + 1


I realize there's an if there, but Excel doesn't.


Thanks again.



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Copy Non-Consecutive Rows and Paste in a Seperate Worksheet

First, when I referred to a previous post being wrong, I meant mine, not
Dave's (see above). Dave was right that the problem was an empty value in a
cell. Sorry, Dave.

The below code works with the data I'm using. Unfortunately, I've got some
formulas in some of the cells and a slew of conditional formatting that I
need to keep. When it pastes in to the new worksheet, it pastes the formulas.
Any tips on pasting values? The commented-out pastevalues lines cause errors.

Public Sub ProcessData()
Const AEName As String = "B" '<=== change to suit
Dim i As Long
Dim T As Long
Dim LastRow As Long
Dim NextRow As Long
Dim sh As String

With Sheet1
T = .Cells(.Rows.Count, "A").End(xlUp).Row
i = 8
Do Until LastRow < 0
If .Cells(i + 2, "A").Value = "" Then
LastRow = i + 1
Else: LastRow = 0
i = i + 1
End If
Loop
End With

With Sheet1
For i = 8 To LastRow

If .Cells(i, AEName).Value < sh Then
sh = .Cells(i, AEName).Value
On Error Resume Next
If Not Worksheets(sh).Name = sh Then _
Worksheets.Add.Name = sh
On Error GoTo 0
If Worksheets(sh).Range("A1").Value = "" Then
NextRow = 1
Else
NextRow = Worksheets(sh).Range("A" & _
.Rows.Count).End(xlUp).Row + 1
End If

.Cells(i, "A").Resize(, 2).Copy _
Worksheets(sh).Cells(NextRow, "A")
' .PasteSpecial xlValues
' .PasteSpecial xlFormats

End If


Next i
End With
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 199
Default Copy Non-Consecutive Rows and Paste in a Seperate Worksheet

I don't know why you use Do loop for looking for the first non blank cell.
I might be wrong, but just ignoring blank cells is enough.

About pasting values, i think it's just a matter of using wrong syntax.
I've tried to revise your code. but not sure this would work in your case.

Public Sub ProcessData()
Const AEName As String = "B" '<=== change to suit
Dim i As Long
Dim T As Long
Dim LastRow As Long
Dim NextRow As Long
Dim sh As String

With Sheet1

LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

For i = 8 To LastRow
sh = .Cells(i, AEName).Value
If sh < "" Then
On Error Resume Next
If Not Worksheets(sh).Name = sh Then _
Worksheets.Add.Name = sh
On Error GoTo 0
If Worksheets(sh).Range("A1").Value = "" Then
NextRow = 1
Else
NextRow = Worksheets(sh).Range("A" & _
.Rows.Count).End(xlUp).Row + 1
End If

.Cells(i, "A").Resize(, 2).Copy
Worksheets(sh).Cells(NextRow, "A").PasteSpecial xlValues
Worksheets(sh).Cells(NextRow, "A").PasteSpecial xlFormats
End If
Next i
End With

End Sub

keiji

elf27 wrote:
First, when I referred to a previous post being wrong, I meant mine, not
Dave's (see above). Dave was right that the problem was an empty value in a
cell. Sorry, Dave.

The below code works with the data I'm using. Unfortunately, I've got some
formulas in some of the cells and a slew of conditional formatting that I
need to keep. When it pastes in to the new worksheet, it pastes the formulas.
Any tips on pasting values? The commented-out pastevalues lines cause errors.

Public Sub ProcessData()
Const AEName As String = "B" '<=== change to suit
Dim i As Long
Dim T As Long
Dim LastRow As Long
Dim NextRow As Long
Dim sh As String

With Sheet1
T = .Cells(.Rows.Count, "A").End(xlUp).Row
i = 8
Do Until LastRow < 0
If .Cells(i + 2, "A").Value = "" Then
LastRow = i + 1
Else: LastRow = 0
i = i + 1
End If
Loop
End With

With Sheet1
For i = 8 To LastRow

If .Cells(i, AEName).Value < sh Then
sh = .Cells(i, AEName).Value
On Error Resume Next
If Not Worksheets(sh).Name = sh Then _
Worksheets.Add.Name = sh
On Error GoTo 0
If Worksheets(sh).Range("A1").Value = "" Then
NextRow = 1
Else
NextRow = Worksheets(sh).Range("A" & _
.Rows.Count).End(xlUp).Row + 1
End If

.Cells(i, "A").Resize(, 2).Copy _
Worksheets(sh).Cells(NextRow, "A")
' .PasteSpecial xlValues
' .PasteSpecial xlFormats

End If


Next i
End With



  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Copy Non-Consecutive Rows and Paste in a Seperate Worksheet

Keiji

I use the loop because the last cell according to Excel is different than
the last one in the list I want to use.

The addition of:
..Cells(i, "A").Resize(, 2).Copy
Worksheets(sh).Cells(NextRow, "A").PasteSpecial xlValues
Worksheets(sh).Cells(NextRow, "A").PasteSpecial xlFormats

Slowed down the macro significantly. Not sure why.
I also think it only pasted values, the formats did not transfer for some
reason.
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 199
Default Copy Non-Consecutive Rows and Paste in a Seperate Worksheet

Hi elf27

Adding the line below before With Sheet1 might improve the speed.

Application.ScreenUpdating = False

In my environment, it transfers both values and formats, so I have no
idea about not working in your side.

if showing your tested code, someone else could give you the reason.

Keiji

elf27 wrote:
Keiji

I use the loop because the last cell according to Excel is different than
the last one in the list I want to use.

The addition of:
.Cells(i, "A").Resize(, 2).Copy
Worksheets(sh).Cells(NextRow, "A").PasteSpecial xlValues
Worksheets(sh).Cells(NextRow, "A").PasteSpecial xlFormats

Slowed down the macro significantly. Not sure why.
I also think it only pasted values, the formats did not transfer for some
reason.

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
copy selected rows to second worksheet (NOT Cut + Paste) gyrra New Users to Excel 2 June 4th 10 09:05 PM
Copy Row Paste Row (Seperate Sheet) Benz Excel Programming 11 November 20th 06 08:57 PM
copy nonblank rows from many worksheets and paste them onto one worksheet starman Excel Programming 2 June 21st 06 12:21 PM
Cut and Paste to A Seperate Worksheet Excel Nerd Excel Programming 1 August 17th 04 09:21 PM
Copy rows and paste to new worksheet Hammer_757 Excel Programming 2 May 6th 04 01:39 PM


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