Extracting Year from date cell

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The contents of the cell that I'm copying (and then pasting) in the formula
below is a date:

Worksheets(Sheet1Name).Cells(Sheet1DateRowPointer, h). _
Copy Worksheets(Sheet2Name).Cells(Sheet2RowPointer, _
Sheet2YearColPointer)

Prior to pasting, however, I need to extract just the Year value. But as
best as I can tell, VBA does not support the YEAR function.

Can someone kindly tell me how I can modify my code to extract and paste
just the Year? Thanks in advance for any help.
 
Sub sistence()
Dim dd As Date
dd = Date
MsgBox (Format(dd, "yyyy"))
End Sub
 
Maybe your "date" isn't really a date.

This works for me:

Option Explicit
Sub testme()
MsgBox Year(Date)
End Sub

But you can't copy|Paste special|just the year of a date.

dim myYear as long

myyear = year(Worksheets(Sheet1Name).Cells(Sheet1DateRowPointer, h).value)

Worksheets(Sheet2Name).Cells(Sheet2RowPointer, Sheet2YearColPointer).value _
= myyear

(With no checking at all)
 
Dave,
Thanks for the help. I verified that the date I'm copying is truly a date,
so the line:

myYear = Year(Worksheets(Sheet1Name).Cells(Sheet1DateRowPointer, h).Value)

does correctly "strip out" the year portion of the date.

Now that I have the year portion stored in myYear, could you kindly tell me
how to modify the line below so that it pastes myYear:

Worksheets(Sheet1Name).Cells(Sheet1DateRowPointer, h). _
Copy Worksheets(Sheet2Name).Cells(Sheet2RowPointer, _
Sheet2YearColPointer)

Thanks again,
Bob
 
Dave,
Please ignore my previous post. I just realized that you already provided
me with the answer:

Worksheets(Sheet2Name).Cells(Sheet2RowPointer, Sheet2YearColPointer).value _
= myyear

Thanks again for all your help!
Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top