VBA code to copy data to a new sheet base on the current year

TRY369

New Member
Joined
Dec 16, 2020
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi there so I have this problem whereby I am not able to copy a row of data from sheet(general_logsheet) to the new sheet. I only want to paste the data that has the current year in it onto a new sheet. The catch is that every year the new sheet will be created named as (year(date) & "_logged"). Example: 2020_Logged, 2021_Logged.


VBA Code:
Private Sub LoggedAss_Click()
Dim lastrow As Long, erow As Long, i As Long
Dim mydate As Date
Dim currentDate As Date

lastrow = Sheets("General_LogSheet").Cells(Rows.Count, 1).End(xlUp).Row
currentDate = Year(Date)

With Sheets("General_LogSheet")
  .Select
  .Range("A1").Select
End With

For i = 2 To lastrow

    mydate = Cells(i, 4)
    If mydate = currentDate Then
    erow = Sheets(Year(Date) & "_logged").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    Range(Cells(i, 1), Cells(i, 4)).COpy Destination:=Sheets(Year(Date) & "_logged").Cells(erow, 1)
    End If
    Next i

End Sub
 

Attachments

  • Screenshot (188).png
    Screenshot (188).png
    99.1 KB · Views: 12

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hello Try,

You could try this as a starting point:-

VBA Code:
Sub Test()
       
        Dim stName As String
        stName = Format([Today()], "YYYY")
        Dim wsGL As Worksheet: Set wsGL = Sheets("General_LogSheet")
        Dim wsD As Worksheet: Set wsD = Sheets(stName & "_Logged")

Application.ScreenUpdating = False

        wsD.UsedRange.Offset(1).Clear
       
        With wsGL.[A1].CurrentRegion
                .AutoFilter 4, 13, 11
                .Offset(1).EntireRow.Copy wsD.Range("A" & Rows.Count).End(3)(2)
                .AutoFilter
        End With
        wsGL.[A1].AutoFilter

Application.ScreenUpdating = True

End Sub

I'm not sure if the destination worksheet is in table format (which is not necessary BTW) but I'm sure you'll let us know.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Solution
Hi there! Thanks for your help, the code works wonderfully as per what I needed. Just for curiosity though, what is this line? Especially the auto-filter part with the numbers.
VBA Code:
With wsGL.[A1].CurrentRegion
                .AutoFilter 4, 13, 11
                .Offset(1).EntireRow.Copy wsD.Range("A" & Rows.Count).End(3)(2)
                .AutoFilter
        End With
 
Upvote 0
Hello Try,

I'm glad that it's all working for you.

Just a brief explanation of the autofilter line:-
- 4 represents Column D which is the date column on which the filter is set.
- 13 is the Excel enumeration for xlFilterThisYear.
- 11 is the Excel enumeration for XlFilterDynamic.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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