Error trying to extract year from cell

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to extract the year only from a cell which is formatted as custom "dd/mm/yyyy hh:mm".

I'm using this but it's giving me an incorrect value;

VBA Code:
Cell.Offset(0, 16).Value = Year(Cell)

'Cell' refers to the cell with the date and time, in this particular example it's '01/07/2019 10:00' and this should give me 2019 but I get 1905 - anyone?

I should add that I want to achieve the result by VBA and want to avoid a formula if possible.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
It works fine for me. Are you sure you are referencing the correct cell?
How is "cell" being defined?
 
Upvote 0
Maybe 'Cell' is referring to the wrong cell? The code should work correctly, although (not essential but considered better practice) I would change it to Year(Cell.Value)
A return of 1905 would suggest a numeric value between 1828 and 2192 in the cell being used.
 
Upvote 0
Guys thanks, just realised the error - the column was formatted as custom "yyyy" - changing it to General solved it.

Thanks to both though!
 
Upvote 0
Solution

Forum statistics

Threads
1,224,828
Messages
6,181,212
Members
453,023
Latest member
alabaz

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