View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Auric__ Auric__ is offline
external usenet poster
 
Posts: 538
Default Copying specific rows to another sheet

Ravichandra Reddy wrote:

I need to copy rows which have same information in column C into another
sheet.

For ex:

[snip]
Now I need a macro so that in sheet 2 all rows which contains P should
be there, sheet 3 all rows which contain NP and so on.


Try this:

Sub foo()
Dim dataSheet As Worksheet
Dim avg As Worksheet, nm As Worksheet, np As Worksheet, p As Worksheet
Dim L0 As Long
'adjust names as appropriate
Set dataSheet = Worksheets("Sheet1")
Set avg = Worksheets("AVG")
Set nm = Worksheets("NM")
Set np = Worksheets("NP")
Set p = Worksheets("P")
For L0 = 1 To dataSheet.Cells.SpecialCells(xlCellTypeLastCell).R ow
dataSheet.Rows(L0).Copy
'for next line, change 2 to the appropriate column
Select Case dataSheet.Cells(L0, 2).Value
Case "AVG"
avg.Activate
avg.Cells(avg.Cells.SpecialCells(xlCellTypeLastCel l).Row + 1, _
1).Select
avg.Paste
Case "NM"
nm.Activate
nm.Cells(nm.Cells.SpecialCells(xlCellTypeLastCell) .Row + 1, _
1).Select
nm.Paste
Case "NP"
np.Activate
np.Cells(np.Cells.SpecialCells(xlCellTypeLastCell) .Row + 1, _
1).Select
np.Paste
Case "P"
p.Activate
p.Cells(p.Cells.SpecialCells(xlCellTypeLastCell).R ow + 1, _
1).Select
p.Paste
Case Else
'no match found; any necessary code here
End Select
Next
Application.CutCopyMode = False
End Sub

This assumes that there is already existing data on the other sheets, or a
header row or something. If not, add this to the end of the sub:

avg.Rows(1).Delete Shift:=xlUp
nm.Rows(1).Delete Shift:=xlUp
np.Rows(1).Delete Shift:=xlUp
p.Rows(1).Delete Shift:=xlUp

--
- Is it safe?
- It's safe. It's very safe.