PC Review


Reply
Thread Tools Rate Thread

copy specific rows to a new sheet

 
 
=?Utf-8?B?a2lt?=
Guest
Posts: n/a
 
      17th Apr 2007
Hi
I have several rows of information in a worksheet I need a macro or code to
select only the rows that do not have the word "keep" anywhere in them, copy
those rows and open a new workbook and paste them into the worksheet then
save the worksheet in my documents with month as the filename.
I manage to do this with a macro selecting specific rows by drag and select
but the layout changes so this no good.
Help appreciated
Thanks
 
Reply With Quote
 
 
 
 
Norman Jones
Guest
Posts: n/a
 
      17th Apr 2007
Hi Kim,

'-------------------
I have several rows of information in a worksheet I need a macro or code to
select only the rows that do not have the word "keep" anywhere in them,
copy
those rows and open a new workbook and paste them into the worksheet then
save the worksheet in my documents with month as the filename.
I manage to do this with a macro selecting specific rows by drag and select
but the layout changes so this no good.
'-------------------

Try something like;

'================>>
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim destSH As Worksheet
Dim rng As Range
Dim rCell As Range
Dim Rng2 As Range
Dim iRow As Long
Dim CalcMode As Long
Const sStr As String = "keep" '<<===== CHANGE

Set WB = Workbooks("MyBook.xls") '<<===== CHANGE
Set SH = WB.Sheets("Sheet1") '<<===== CHANGE

With SH
iRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set rng = SH.Range("A1:A" & iRow)
End With

On Error GoTo XIT
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With


For Each rCell In rng.Cells
If Application.CountIf( _
rCell.EntireRow, "*" & sStr & "*") Then
If Rng2 Is Nothing Then
Set Rng2 = rCell
Else
Set Rng2 = Union(rCell, Rng2)
End If
End If
Next rCell

If Not Rng2 Is Nothing Then
With WB
Set destSH = .Worksheets.Add( _
After:=.Sheets(.Sheets.Count))
End With

With destSH
Rng2.Copy Destination:=destSH.Range("A1")
.Name = Format(Date, "mmmm")
.Copy
End With

With ActiveWorkbook
.SaveAs Filename:=destSH.Name & ".xls"
.Close SaveChanges:=False
End With
End If

XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With
End Sub
'<<================


---
Regards,
Norman


 
Reply With Quote
 
Norman Jones
Guest
Posts: n/a
 
      17th Apr 2007
Hi Kim,


Re-reading your post, replace:

> If Application.CountIf( _
> rCell.EntireRow, "*" & sStr & "*") Then



with

If Application.CountIf( _
rCell.EntireRow, "*" & sStr & "*") = 0 Then


---
Regards,
Norman


 
Reply With Quote
 
=?Utf-8?B?a2lt?=
Guest
Posts: n/a
 
      17th Apr 2007
Thanks Norman it produced the workbook but no data pasted. I moved my data to
cell A1 tried again . It did paste only the rows but only the data in column
A how can get to paste data from the other columns?
I tried to figure this out by looking a t the code but can't get there!
Thanks

"Norman Jones" wrote:

> Hi Kim,
>
>
> Re-reading your post, replace:
>
> > If Application.CountIf( _
> > rCell.EntireRow, "*" & sStr & "*") Then

>
>
> with
>
> If Application.CountIf( _
> rCell.EntireRow, "*" & sStr & "*") = 0 Then
>
>
> ---
> Regards,
> Norman
>
>
>

 
Reply With Quote
 
Norman Jones
Guest
Posts: n/a
 
      17th Apr 2007
Hi Kim,

'------------------
Thanks Norman it produced the workbook but no data pasted. I moved my data
to
cell A1 tried again . It did paste only the rows but only the data in column
A how can get to paste data from the other columns?
I tried to figure this out by looking a t the code but can't get there!
'------------------

(1) Change:

> Set rng = SH.Range("A1:A" & iRow)


to reflect a column which encompasses all of your data.


(2) Change

> Rng2.Copy Destination:=destSH.Range("A1")


to:

Rng2EntireRow.Copy Destination:=destSH.Range("A1")


---
Regards,
Norman


 
Reply With Quote
 
=?Utf-8?B?a2lt?=
Guest
Posts: n/a
 
      17th Apr 2007
Hi Norman
I chaged the range in the code from A1:A to A1:Z400
This caught the data was this the correct approach?
Thanks

"kim" wrote:

> Thanks Norman it produced the workbook but no data pasted. I moved my data to
> cell A1 tried again . It did paste only the rows but only the data in column
> A how can get to paste data from the other columns?
> I tried to figure this out by looking a t the code but can't get there!
> Thanks
>
> "Norman Jones" wrote:
>
> > Hi Kim,
> >
> >
> > Re-reading your post, replace:
> >
> > > If Application.CountIf( _
> > > rCell.EntireRow, "*" & sStr & "*") Then

> >
> >
> > with
> >
> > If Application.CountIf( _
> > rCell.EntireRow, "*" & sStr & "*") = 0 Then
> >
> >
> > ---
> > Regards,
> > Norman
> >
> >
> >

 
Reply With Quote
 
Norman Jones
Guest
Posts: n/a
 
      17th Apr 2007
Hi Kim,

'----------------
I chaged the range in the code from A1:A to A1:Z400
This caught the data was this the correct approach?
'----------------

See my response to your previous post..

However, it should be necessary only to replace 'A'
with a column that defines the last data row.

To copy the entire data rows, adopt also my second
suggestion.


---
Regards,
Norman


 
Reply With Quote
 
Norman Jones
Guest
Posts: n/a
 
      17th Apr 2007
Hi Kim,

To avoid the potential problem of column specification,
try the following version:

'================>>
Public Sub Tester()
Dim WB As Workbook
Dim sh As Worksheet
Dim destSH As Worksheet
Dim rng As Range
Dim rCell As Range
Dim Rng2 As Range
Dim iRow As Long
Dim CalcMode As Long
Const sStr As String = "keep" '<<===== CHANGE

Set WB = Workbooks("MyBook.xls") '<<===== CHANGE
Set sh = WB.Sheets("Sheet1") '<<===== CHANGE

With sh
iRow = LastRow(sh)
Set rng = sh.Range("A1:A" & iRow)
End With

On Error GoTo XIT
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

For Each rCell In rng.Cells
If Application.CountIf( _
rCell.EntireRow, "*" & sStr & "*") = 0 Then
If Rng2 Is Nothing Then
Set Rng2 = rCell
Else
Set Rng2 = Union(rCell, Rng2)
End If
End If
Next rCell

If Not Rng2 Is Nothing Then
With WB
Set destSH = .Worksheets.Add( _
After:=.Sheets(.Sheets.Count))
End With

With destSH
Rng2.EntireRow.Copy Destination:=destSH.Range("A1")
.Name = Format(Date, "mmmm")
.Copy
End With

With ActiveWorkbook
.SaveAs Filename:=destSH.Name & ".xls"
.Close SaveChanges:=False
End With
End If

XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With
End Sub

'--------------->
Function LastRow(sh As Worksheet, _
Optional rng As Range)
If rng Is Nothing Then
Set rng = sh.Cells
End If

On Error Resume Next
LastRow = rng.Find(What:="*", _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
'<<================


---
Regards,
Norman


 
Reply With Quote
 
=?Utf-8?B?a2lt?=
Guest
Posts: n/a
 
      17th Apr 2007
Hi Norman
Looking at this, away from the code ,for what I need to record it makes more
sense to have the code copy the rows where the word "Keep" does not appear is
it possible to change the code to do this?
Thanks for your help

"Norman Jones" wrote:

> Hi Kim,
>
> '------------------
> Thanks Norman it produced the workbook but no data pasted. I moved my data
> to
> cell A1 tried again . It did paste only the rows but only the data in column
> A how can get to paste data from the other columns?
> I tried to figure this out by looking a t the code but can't get there!
> '------------------
>
> (1) Change:
>
> > Set rng = SH.Range("A1:A" & iRow)

>
> to reflect a column which encompasses all of your data.
>
>
> (2) Change
>
> > Rng2.Copy Destination:=destSH.Range("A1")

>
> to:
>
> Rng2EntireRow.Copy Destination:=destSH.Range("A1")
>
>
> ---
> Regards,
> Norman
>
>
>

 
Reply With Quote
 
=?Utf-8?B?a2lt?=
Guest
Posts: n/a
 
      17th Apr 2007
Sorry please ignore last post!


"kim" wrote:

> Hi Norman
> Looking at this, away from the code ,for what I need to record it makes more
> sense to have the code copy the rows where the word "Keep" does not appear is
> it possible to change the code to do this?
> Thanks for your help
>
> "Norman Jones" wrote:
>
> > Hi Kim,
> >
> > '------------------
> > Thanks Norman it produced the workbook but no data pasted. I moved my data
> > to
> > cell A1 tried again . It did paste only the rows but only the data in column
> > A how can get to paste data from the other columns?
> > I tried to figure this out by looking a t the code but can't get there!
> > '------------------
> >
> > (1) Change:
> >
> > > Set rng = SH.Range("A1:A" & iRow)

> >
> > to reflect a column which encompasses all of your data.
> >
> >
> > (2) Change
> >
> > > Rng2.Copy Destination:=destSH.Range("A1")

> >
> > to:
> >
> > Rng2EntireRow.Copy Destination:=destSH.Range("A1")
> >
> >
> > ---
> > Regards,
> > Norman
> >
> >
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy rows from one Data sheet to another sheet based on cell conte John McKeon Microsoft Excel Misc 2 15th May 2010 06:49 AM
copy rows to new sheet based on specific cell value dlballard Microsoft Excel Worksheet Functions 4 18th Aug 2009 09:41 PM
copy specific rows using "IF" to another sheet Henry Microsoft Excel Worksheet Functions 3 24th Dec 2007 03:41 AM
Copy rows onto existing sheet / start a new sheet if full mg_sv_r Microsoft Excel Programming 0 29th Nov 2007 12:57 PM
Help: auto-copy entire rows from 1 sheet (based on cell criteria) to another sheet. bertbarndoor Microsoft Excel Programming 4 5th Oct 2007 04:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:55 PM.