How to find the current year in vba

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
What is the vba code to find the current year?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I have a procedure that copies the value of a text box to a cell, and I want the year after the copied value.

Code:
Worksheets("Jordyn").Range("E1") = txtDate.Value & Call CurrentYear

This code won't work. What is wrong with the syntax?
 
Upvote 0
Current year is:

Code:
Private Sub CurrentYear()
    Dim MyYear As Integer
    
    MyYear = Year(Now)
End Sub
 
Upvote 0
I think I need to use vba for this but I am not sure. I have a cell in a spreadsheet that is populated with a set text string then a date range that is entered in a text box and I want the current year to be displayed after the string using the current year. Any ideas how I could do this?

This is all the code I have so far:

Code:
Private Sub CopyE1()
    Worksheets("Jordyn").Range("E1") = txtDate.Value & Call CurrentYear
    Worksheets("Unallocated clients").Range("E1") = txtDate.Value
    Worksheets("Sophia").Range("E1") = txtDate.Value
    Worksheets("Grace").Range("E1") = txtDate.Value
    Worksheets("Rebecca").Range("E1") = txtDate.Value
    Worksheets("Briony").Range("E1") = txtDate.Value
    Worksheets("Therese").Range("E1") = txtDate.Value
    Worksheets("Luke").Range("E1") = txtDate.Value
    Worksheets("Yiri").Range("E1") = txtDate.Value
    

    
    
    
End Sub

Private Sub cmdCopy_Click()
Call CopyE1
Call CopyB3


End Sub

Private Sub CopyB3()

    Worksheets("Jordyn").Range("B3") = "Number of Notes in TCM as of " & txtDate.Value
    Worksheets("Unallocated clients").Range("B3") = "Number of Notes in TCM as of " & txtDate.Value
    Worksheets("Sophia").Range("B3") = "Number of Notes in TCM as of " & txtDate.Value
    Worksheets("Grace").Range("B3") = "Number of Notes in TCM as of " & txtDate.Value
    Worksheets("Rebecca").Range("B3") = "Number of Notes in TCM as of " & txtDate.Value
    Worksheets("Briony").Range("B3") = "Number of Notes in TCM as of " & txtDate.Value
    Worksheets("Therese").Range("B3") = "Number of Notes in TCM as of " & txtDate.Value
    Worksheets("Luke").Range("B3") = "Number of Notes in TCM as of " & txtDate.Value
    Worksheets("Yiri").Range("B3") = "Number of Notes in TCM as of " & txtDate.Value
    
End Sub



Private Sub CurrentYear()
    Dim MyYear As Integer
    
    MyYear = Year(Now)
End Sub
 
Last edited:
Upvote 0
E1 needs to have the date range that is entered in txtDate. I want the current year to appear following the date range in B3.
 
Upvote 0
Here is the answer of one of your questions:
Code:
Sub My_Year()
'Modified  11/18/2018  9:46:52 PM  EST
Worksheets("Jordyn").Range("E1") = Sheets("Jordyn").txtDate.Value & Year(Date)
End Sub
 
Upvote 0
Not sure what's happening here. You have asked several questions in just the last 5 minutes.

What is:
E1 needs to have the date range that is entered in txtDate

What is date range.

What are you entering into the textbox named txtDate

Are you entering a value like "Mary" or are you entering a Date?
 
Upvote 0
You may be able to use something like this, but what is entered in txtDate ??

Code:
Private Sub CopyB3()
Dim ws As Worksheet
For Each ws In Worksheets
    If ws.Name <> "Sheet1" And ws.Name <> "Sheet2" Then 'change worksheet names that aren't affected here
    With ws
    .Range("B3") = "Number of Notes in TCM as of " & Year(Now)
    End With
Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,839
Members
449,051
Latest member
excelquestion515

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