Conditional copying a row and inserting a new row


Posted by Forever Lost on February 14, 2002 10:57 AM

Hello,
I have 12 worksheets. I want 1 worksheet to look at the other worksheets and find the rows that have "New" in the A column. If it has "New" then I want it to copy the data in columns B-P in that row to the worksheet that displays only the "New" items. There could be many "New" items so the next new would have to go in the next row on the worksheet that is looking for them. And so on and so on. Sorry if this is confusing, I am confused myself. Please help!

Posted by Barrie Davidson on February 14, 2002 1:05 PM

Hello "Forever Lost". The code below will copy columns B-P (from all the worksheets after your "New Listing" worksheet) in to a worksheet named "New Listing" (in columns B-P). NOTE - this code assumes your "New Listing" worksheet is the first worksheet in your workbook.

Sub CopyNew()
' Written by Barrie Davidson

Dim WorkingSheet As String
Dim i As Long
Dim Rowi As Long

On Error Resume Next
Sheets("New Listing").Select
Range("B65536").End(xlUp).Offset(1, 0).Select
If Selection.Row = 2 And Range("A1").Value = "" Then Selection.Offset(-1, 0).Select
Rowi = 0
For i = 1 To ActiveWorkbook.Sheets.Count - 1
ActiveSheet.Next.Select
Range("A1").Select
Cells.Find(What:="new", _
After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False) _
.Activate
If UCase(Selection.Value) = "NEW" Then
Do Until Selection.Row < Rowi
Rowi = Selection.Row
WorkingSheet = ActiveSheet.Name
Selection.Offset(0, 1).Range("A1:O1").Copy
Sheets("New Listing").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.Offset(1, 0).Activate
Sheets(WorkingSheet).Select
Cells.Find(What:="new", _
After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False) _
.Activate
Loop
Rowi = 0
End If
Next i

End Sub


Hope this helps you out.

Regards,
BarrieBarrie Davidson

Posted by Forever Lost on February 14, 2002 1:20 PM

Thanks Barrie! One question though. Is there a way for the "New Listing" Worksheet to be the last in the workbook? This new listing sheet is more like a report done at the end of the inventory. If it is up front then people would get confused. Even though they wouldn't need to use it because it is more for the inventory manager. I'm trying to make it as easy as possible for the people doing the counting.

Thanks again!
Eric

Posted by Barrie Davidson on February 14, 2002 1:24 PM

Eric, try this code instead.

Sub CopyNew()
' Written by Barrie Davidson

Dim WorkingSheet As String
Dim i As Long
Dim Rowi As Long

On Error Resume Next
Sheets("New Listing").Select
Range("B65536").End(xlUp).Offset(1, 0).Select
If Selection.Row = 2 And Range("A1").Value = "" Then Selection.Offset(-1, 0).Select
Rowi = 0
For i = 1 To ActiveWorkbook.Sheets.Count - 1
Sheets(i).Select
Range("A1").Select
Cells.Find(What:="new", _
After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False) _
.Activate
If UCase(Selection.Value) = "NEW" Then
Do Until Selection.Row < Rowi
Rowi = Selection.Row
WorkingSheet = ActiveSheet.Name
Selection.Offset(0, 1).Range("A1:O1").Copy
Sheets("New Listing").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.Offset(1, 0).Activate
Sheets(WorkingSheet).Select
Cells.Find(What:="new", _
After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False) _
.Activate
Loop
Rowi = 0
End If
Next i
Sheets("New Listing").Select

End Sub

Regards,
Barrie Thanks Barrie! One question though. Is there a way for the "New Listing" Worksheet to be the last in the workbook? This new listing sheet is more like a report done at the end of the inventory. If it is up front then people would get confused. Even though they wouldn't need to use it because it is more for the inventory manager. I'm trying to make it as easy as possible for the people doing the counting. Thanks again!

Barrie Davidson

Posted by Forever Lost on February 14, 2002 2:01 PM

Doesn't seem to work. It keeps pasting the same couple of cells over and over. The cells don't contain the word "new" in them. It keeps pasting Manager and his name. The rows that could contain new are about 7 rows down. I tried it with both versions you sent me and they both do they same thing.

Thanks again for your help.
Eric Eric, try this code instead.



Posted by Barrie Davidson on February 14, 2002 2:23 PM

Eric, can you e-mail a copy of the file (with any confidential info taken out) and I'll have a look at it when I get home tonight from work? FYI - it's 4:30 pm here right now.

Barrie

Barrie Davidson