Copying Data From one worksheet to another containing todays date

travieman90

New Member
Joined
Aug 17, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Attempting to copy data from sheet "2020bymachine" to A3 on activesheet. Only want to copy rows containing today's or yesterdays date in column A. The code below runs and presents no errors but does not copy anything.


VBA Code:
Sub CopyData()

Dim LastRow As Long, d As Long
Dim CopyRange As Range, count As Long

LastRow = Cells(Cells.Rows.count, "A").End(xlUp).Row

For d = LastRow To 10 Step -1

If IsDate(Sheets("2020byMachine").Cells(d, "A")) And Sheets("2020byMachine").Cells(d, "A").Value = Date Then
count = count + 1
      If CopyRange Is Nothing Then
                Set CopyRange = Rows(d).EntireRow
        Else
                Set CopyRange = Union(CopyRange, Rows(d).EntireRow)
        End If
    End If


CopyRange.Copy

Sheets("DataSheet").Select

Range("A3").Select

ActiveSheet.Paste

End Sub
 
Last edited by a moderator:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
How about
VBA Code:
Sub CopyData()

Dim LastRow As Long, d As Long
Dim CopyRange As Range, count As Long

With Sheets("2020byMachine")
   LastRow = .Cells(.Rows.count, "A").End(xlUp).Row
   
   For d = LastRow To 10 Step -1
   
   If .Cells(d, "A") = Date - 1 Or .Cells(d, "A").Value = Date Then
      count = count + 1
         If CopyRange Is Nothing Then
                   Set CopyRange = .Rows(d).EntireRow
           Else
                   Set CopyRange = Union(CopyRange, .Rows(d).EntireRow)
           End If
       End If
   
End With
   CopyRange.Copy Sheets("DataSheet").Range("A3")
End Sub
 
Upvote 0
How about
VBA Code:
Sub CopyData()

Dim LastRow As Long, d As Long
Dim CopyRange As Range, count As Long

With Sheets("2020byMachine")
   LastRow = .Cells(.Rows.count, "A").End(xlUp).Row
  
   For d = LastRow To 10 Step -1
  
   If .Cells(d, "A") = Date - 1 Or .Cells(d, "A").Value = Date Then
      count = count + 1
         If CopyRange Is Nothing Then
                   Set CopyRange = .Rows(d).EntireRow
           Else
                   Set CopyRange = Union(CopyRange, .Rows(d).EntireRow)
           End If
       End If
  
End With
   CopyRange.Copy Sheets("DataSheet").Range("A3")
End Sub


This looks promising but getting a compile error "end with without with"
 
Upvote 0
It's missing the Next d line
VBA Code:
Sub CopyData()

Dim LastRow As Long, d As Long
Dim CopyRange As Range, count As Long

With Sheets("2020byMachine")
   LastRow = .Cells(.Rows.count, "A").End(xlUp).Row
   
   For d = LastRow To 10 Step -1
   
   If .Cells(d, "A") = Date - 1 Or .Cells(d, "A").Value = Date Then
      count = count + 1
         If CopyRange Is Nothing Then
                   Set CopyRange = .Rows(d).EntireRow
           Else
                   Set CopyRange = Union(CopyRange, .Rows(d).EntireRow)
           End If
       End If
   Next d
End With
   CopyRange.Copy Sheets("DataSheet").Range("A3")
End Sub
 
Upvote 0
It's missing the Next d line
VBA Code:
Sub CopyData()

Dim LastRow As Long, d As Long
Dim CopyRange As Range, count As Long

With Sheets("2020byMachine")
   LastRow = .Cells(.Rows.count, "A").End(xlUp).Row
  
   For d = LastRow To 10 Step -1
  
   If .Cells(d, "A") = Date - 1 Or .Cells(d, "A").Value = Date Then
      count = count + 1
         If CopyRange Is Nothing Then
                   Set CopyRange = .Rows(d).EntireRow
           Else
                   Set CopyRange = Union(CopyRange, .Rows(d).EntireRow)
           End If
       End If
   Next d
End With
   CopyRange.Copy Sheets("DataSheet").Range("A3")
End Sub
Now im getting "next without for"
 
Upvote 0
It's missing the Next d line
VBA Code:
Sub CopyData()

Dim LastRow As Long, d As Long
Dim CopyRange As Range, count As Long

With Sheets("2020byMachine")
   LastRow = .Cells(.Rows.count, "A").End(xlUp).Row
  
   For d = LastRow To 10 Step -1
  
   If .Cells(d, "A") = Date - 1 Or .Cells(d, "A").Value = Date Then
      count = count + 1
         If CopyRange Is Nothing Then
                   Set CopyRange = .Rows(d).EntireRow
           Else
                   Set CopyRange = Union(CopyRange, .Rows(d).EntireRow)
           End If
       End If
   Next d
End With
   CopyRange.Copy Sheets("DataSheet").Range("A3")
End Sub
This is working fantastic. I really appreciate it. Id like to try and have it automated when i open that sheet rather than using a button to force the update if possible but regardless massive thanks to you.
 
Upvote 0
You can put it in an activate event like
VBA Code:
Private Sub Worksheet_Activate()

Dim LastRow As Long, d As Long
Dim CopyRange As Range, count As Long

With Sheets("2020byMachine")
   LastRow = .Cells(.Rows.count, "A").End(xlUp).Row
   
   For d = LastRow To 10 Step -1
   
   If .Cells(d, "A") = Date - 1 Or .Cells(d, "A").Value = Date Then
      count = count + 1
         If CopyRange Is Nothing Then
                   Set CopyRange = .Rows(d).EntireRow
           Else
                   Set CopyRange = Union(CopyRange, .Rows(d).EntireRow)
           End If
       End If
   Next d
End With
   CopyRange.Copy Sheets("DataSheet").Range("A3")

End Sub
This code needs to go in the relevant sheet module.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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