Help : Vba to take last 4 numbers

Revathi

New Member
Joined
May 26, 2015
Messages
34
Hi all,

Can anyone help me out for the below requirement.

> I have dates in Column S wherein i want to copy only the year (which is last 4 digits) from Column S and paste it in column T.

> I need to use the used-range based on Column A. Please see below example.

[TABLE="width: 283"]
<colgroup><col span="3"><col></colgroup><tbody>[TR]
[TD]Column S[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Column T[/TD]
[/TR]
[TR]
[TD]29-08-2018[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]2018[/TD]
[/TR]
[TR]
[TD]29-08-2018[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]2018[/TD]
[/TR]
[TR]
[TD]29-08-2018[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]2018[/TD]
[/TR]
[TR]
[TD]29-08-2018[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]2018[/TD]
[/TR]
[TR]
[TD]29-08-2018[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]2018[/TD]
[/TR]
[TR]
[TD]29-08-2018[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]2018[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi all,

Can anyone help me out for the below requirement.

> I have dates in Column S wherein i want to copy only the year (which is last 4 digits) from Column S and paste it in column T.

> I need to use the used-range based on Column A. Please see below example.

[TABLE="width: 283"]
<colgroup><col span="3"><col></colgroup><tbody>[TR]
[TD]Column S[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Column T[/TD]
[/TR]
[TR]
[TD]29-08-2018[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]2018[/TD]
[/TR]
[TR]
[TD]29-08-2018[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]2018[/TD]
[/TR]
[TR]
[TD]29-08-2018[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]2018[/TD]
[/TR]
[TR]
[TD]29-08-2018[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]2018[/TD]
[/TR]
[TR]
[TD]29-08-2018[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]2018[/TD]
[/TR]
[TR]
[TD]29-08-2018[/TD]
[TD] [/TD]
[TD] [/TD]
[TD]2018[/TD]
[/TR]
</tbody>[/TABLE]

Assuming your values start on Row 1, the if those dates are text values, the you can use this...

=RIGHT(S1,4)

However, if your dates are real Excel dates formatted to look like you showed us, then you would need to use this instead...

=YEAR(S1)
 
Upvote 0
Thanks for your immediate response. But i am not able to use this using vba code.. Is it possible.:)
 
Upvote 0
Rick did mention that the dates might be text or real Excel dates (numbers). Which are they?
 
Upvote 0
try this..
Code:
Sub Macro1()


Dim cn As Integer, i As Integer


With Worksheets("Sheet2")
For i = 2 To cn
    cn = .Range("S2").End(xlDown).Row
    .Cells(i, "T").Value = Right(.Cells(i, "S").Value, 4)
Next i
End With


End Sub
 
Upvote 0
Those cells are not text, that is excel dates..
Then try
Code:
Sub Extract_Year()
  With Range("S2:S" & Range("A" & Rows.Count).End(xlUp).Row)
    .Offset(, 1).Value = Evaluate(Replace("if(#="""","""",Year(#))", "#", .Address))
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,983
Members
452,540
Latest member
haasro02

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