Copying data over based on the year

TRY369

New Member
Joined
Dec 16, 2020
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi there, I am trying to make a code where I will copy the row of date if the date meets the criteria. The data will be pasted onto another sheet in the workbook. However, I have this error which i was not able to solve..
VBA Code:
Private Sub LoggedAss_Click()
Dim lastrow As Long, erow As Long, i As Long
Dim mydate As Date
lastrow = General_Logsheet.Cells(Rows.Count, 1).End(xlUp).Row

General_Logsheet.Range("A1").Select

For i = 2 To lastrow

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

End Sub
 

Attachments

  • Screenshot (172).png
    Screenshot (172).png
    29.7 KB · Views: 5

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
What is "General_Logsheet"?
Is it a worksheet variable?
Where is it defined and where is it set?
 
Upvote 0
What is "General_Logsheet"?
Is it a worksheet variable?
Where is it defined and where is it set?
Hi there, "General_Logsheet" is actually one of the sheet in excel. Im trying to copy data from this sheet to another sheet.
 

Attachments

  • Screenshot (174).png
    Screenshot (174).png
    14.7 KB · Views: 5
Upvote 0
Hi there, "General_Logsheet" is actually one of the sheet in excel. Im trying to copy data from this sheet to another sheet.

General_Logsheet.Range("A1").Select
You cannot refer to your sheet like that.

You either have to do something like this:
VBA Code:
Sheets("General_Logsheet")...
or you need to first create a worksheet object like this:
VBA Code:
Dim General_Logsheet as Worksheet
Set General_Logsheet = Sheets("General_Logsheet")
Then you can refer to it like you did in your code.


Also note, that if you are NOT already on that sheet, you cannot select the sheet and range in one step like this:
General_Logsheet.Range("A1").Select
You need to do it in 2 steps, i.e. first select the sheet, then the range:
VBA Code:
General_Logsheet.Activate
Range("A1").Select
 
Upvote 0
You cannot refer to your sheet like that.

You either have to do something like this:
VBA Code:
Sheets("General_Logsheet")...
or you need to first create a worksheet object like this:
VBA Code:
Dim General_Logsheet as Worksheet
Set General_Logsheet = Sheets("General_Logsheet")
Then you can refer to it like you did in your code.


Also note, that if you are NOT already on that sheet, you cannot select the sheet and range in one step like this:

You need to do it in 2 steps, i.e. first select the sheet, then the range:
VBA Code:
General_Logsheet.Activate
Range("A1").Select
All right I will edit parts of my code first. Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,577
Members
449,039
Latest member
Arbind kumar

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