Copy in a new sheeet, rows that have a certain date in them

daniels012

Well-known Member
Joined
Jan 13, 2005
Messages
5,219
Using code:
How can I copy rows that have specific dates from a sheet named "Total" to the current sheet I am in?

Rather than manually copying and pasting, it would be much easier if I could do it using code.

Any Ideas?

Michael
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
After all of that here is what was done by "Gibbs"

Code:
Sub CreateNewMOnth2()
Application.ScreenUpdating = False
Dim ShName As String, ShExists As Boolean
Dim c As Range, ws As Worksheet, wks1 As Worksheet, wks2 As Worksheet
Dim rngtoSearch As Range, rngDestination As Range, rngFound As Range
Dim MyDate As String, rngFirst As Range, rngAllrecords As Range
ShName = ActiveSheet.Range("E1").Text
MyDate = Left(Range("F2"), 2) - 1 & Right(Range("F2"), 4)
On Error Resume Next
ShExists = Len(Worksheets(ShName).Name) > 0
    On Error GoTo 0
    If ShExists Then
    MsgBox "Worksheet already exists", 48, "Title"
    Else
    ActiveWorkbook.Worksheets.Add After:=Worksheets(Worksheets.Count)
    ActiveSheet.Name = ShName
    End If

'will return 11 2005
MsgBox MyDate
Set wks1 = Sheets("running total")
Set wks2 = Sheets(ShName)
Sheets("Running Total").Range("A6").EntireRow.Copy
wks2.Range("A5").PasteSpecial
Application.CutCopyMode = False
wks2.Cells(3, 1) = ShName
Set rngtoSearch = wks1.Columns("BN") 'column BN has a formula in it to return the month year of date in column K in 'mmyyyy'
Set rngDestination = wks2.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
   Set rngFound = rngtoSearch.Find _
   (What:=MyDate, LookIn:=xlValues, LookAt:=xlWhole)
            If Not rngFound Is Nothing Then
             On Error Resume Next
             lngNextRow = 1
             Set rngFirst = rngFound
             Set rngAllrecords = rngFound
               Do
                Set rngAllrecords = Union(rngAllrecords, rngFound)
                Set rngFound = rngtoSearch.FindNext(rngFound)
                Loop Until rngFound.Address = rngFirst.Address
                
                rngAllrecords.EntireRow.Copy rngDestination.EntireRow
                
            End If
            Columns("A:O").Select
    Columns("A:O").EntireColumn.AutoFit
    Range("A3").Select
    Selection.NumberFormat = "mmmm yyyy"
    Range("A3:B3").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = False
    End With
    Selection.Merge
    Selection.Font.Bold = True
    Columns("A:A").EntireColumn.AutoFit
    Columns("B:B").EntireColumn.AutoFit
    Application.ScreenUpdating = True
End Sub

Thank You so much :pray:
Michael
 
Upvote 0

Forum statistics

Threads
1,214,896
Messages
6,122,132
Members
449,066
Latest member
Andyg666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top