View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.misc
Peter[_16_] Peter[_16_] is offline
external usenet poster
 
Posts: 1
Default How to copy all rows that contain a certain criteria to a new sheet?


Simon Lloyd;454461 Wrote:
I haven't looked at your workbook in detail or your code at all,
however, if all you want to do is copy any row that has the word voucher
in column G then this would suffice:

Code:
--------------------
Sub Copy_Vouchers()

Dim MyCell As Range
Dim Sh As Worksheet
For Each Sh In Sheets(Array("Cash", "Bank")) 'add sheets as needed
For Each MyCell In Sheets(Sh.Name).Range("G1:G" & Sheets(Sh.Name).Range("G" & Rows.Count).End(xlUp).Row)
If Left(MyCell.Text, 7) = "Voucher" Then
MyCell.EntireRow.Copy Destination:=Sheets("Vouchers").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
End If
Next MyCell
Next Sh
End Sub

--------------------


Hello Simon

Your solution worked, thank you. I have now added a new worksheet
"Subs" and added the following code (based on yours):

Sub Subs()
Dim MyCell As Range
Dim Sh As Worksheet
For Each Sh In Sheets(Array("Cash", "Bank", "Stock")) 'add sheets as
needed
For Each MyCell In Sheets(Sh.Name).Range("G1:G" &
Sheets(Sh.Name).Range("D" & Rows.Count).End(xlUp).Row)
If Left(MyCell.Text, 4) = "Annual subscriptions" Then
MyCell.EntireRow.Copy Destination:=Sheets("Subs").Range("A" &
Rows.Count).End(xlUp).Offset(1, 0)
End If
Next MyCell
Next Sh
End Sub

It doesn't work! Any ideas?!

From a very VBA beginner

Regards

Peter


--
Peter
------------------------------------------------------------------------
Peter's Profile: http://www.thecodecage.com/forumz/member.php?userid=687
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=125534