VBA - If date is today than

KirovHC

New Member
Joined
Jan 19, 2021
Messages
32
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello,
I have to be honest: I am quite new to VBA and I copied a macro trying to adapt it to my needs.
My problem: in column A there is the whole year, in the next cells there are a series of data.
I need to copy the cells next to today's date to another sheet.

VBA Code:
Sub selectdata()

Dim nextrow As Long
   For Row = 4 To 368
        If Worksheets("SHEET1").Cells(Row, 1).Value = Date Then
           Worksheets("SHEET1").Cells(Row, 1).Copy
            Worksheets("SHEET2").Activate
           nextrow = Range("A65536").End(xlUp).Row
           Cells(nextrow, 1).Select
            ActiveSheet.Paste
       End If
        
    Next Row
    
Worksheets("SHEET1").Select
End Sub

In this case I copy the specific cell, how can I set a range of cells instead?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
If you mean including the cell in column A then try the code below (untested)

VBA Code:
Sub selectdata()
    Dim myRow As Long

    Application.ScreenUpdating = False

    For myRow = 4 To 368
        If Worksheets("SHEET1").Cells(myRow, 1).Value = Date Then _
             Worksheets("SHEET1").Cells(myRow, 1).Resize(, 5).Copy _
                 Worksheets("SHEET2").Cells(Rows.Count, "A").End(xlUp)(2)
    Next myRow
    
    Application.ScreenUpdating = True

End Sub

If the code is too slow for you then we can use the autofilter which will be quicker.
 
Upvote 0
Solution
If you mean including the cell in column A then try the code below (untested)

VBA Code:
Sub selectdata()
    Dim myRow As Long

    Application.ScreenUpdating = False

    For myRow = 4 To 368
        If Worksheets("SHEET1").Cells(myRow, 1).Value = Date Then _
             Worksheets("SHEET1").Cells(myRow, 1).Resize(, 5).Copy _
                 Worksheets("SHEET2").Cells(Rows.Count, "A").End(xlUp)(2)
    Next myRow
   
    Application.ScreenUpdating = True

End Sub

If the code is too slow for you then we can use the autofilter which will be quicker.

No, it works correctly. Thanks so much!
I take advantage of your kindness to ask you: what if I then later want to copy, for example, the sixth cell next to the date in a specific cell of another sheet?
 
Upvote 0
If you mean as a continuous range from column A to F then you would just change the 5 in the resize to a 6.
If you mean as a single cell then you would do
VBA Code:
Worksheets("SHEET1").Cells(myRow, 1).Offset(, 5).Copy
 
Upvote 0
If you mean as a continuous range from column A to F then you would just change the 5 in the resize to a 6.
If you mean as a single cell then you would do
VBA Code:
Worksheets("SHEET1").Cells(myRow, 1).Offset(, 5).Copy

Now I have copied the use of resize!
I intend to copy a cell on another sheet but in a cell of my choice (example C3, not on the first row as in this case)
 
Upvote 0
Copying to a cell...
VBA Code:
Worksheets("SHEET1").Cells(myRow, 1).Offset(, 5).Copy Worksheets("SHEET2").Range("C3")
or
VBA Code:
Worksheets("SHEET1").Cells(myRow, 1).Offset(, 5).Copy Worksheets("SHEET2").Cells(3, "C")
or if you don't need the formatting copied
VBA Code:
Worksheets("SHEET2").Range("C3").Value = Worksheets("SHEET1").Cells(myRow, 1).Offset(, 5).Value
 
Upvote 0
Copying to a cell...
VBA Code:
Worksheets("SHEET1").Cells(myRow, 1).Offset(, 5).Copy Worksheets("SHEET2").Range("C3")
or
VBA Code:
Worksheets("SHEET1").Cells(myRow, 1).Offset(, 5).Copy Worksheets("SHEET2").Cells(3, "C")
or if you don't need the formatting copied
VBA Code:
Worksheets("SHEET2").Range("C3").Value = Worksheets("SHEET1").Cells(myRow, 1).Offset(, 5).Value

Many thanks!!
 
Upvote 0

Forum statistics

Threads
1,214,901
Messages
6,122,157
Members
449,068
Latest member
shiz11713

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