Error trying to extract year from cell

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,340
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.
 

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,492
Office Version
  1. 365
Platform
  1. Windows
It works fine for me. Are you sure you are referencing the correct cell?
How is "cell" being defined?
 

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
12,405
Office Version
  1. 365
Platform
  1. Windows
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.
 

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,340
Office Version
  1. 2016
Platform
  1. Windows
Guys thanks, just realised the error - the column was formatted as custom "yyyy" - changing it to General solved it.

Thanks to both though!
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,127,001
Messages
5,622,125
Members
415,878
Latest member
jjj12345

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
Top