Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

excel - How can I fix this code so VarYear outputs the correct number in VBA?

I have the below code that I am trying to have VarYear equal 2021 if the current date is before October 1 and 2022 if the date is after October 1. Here is my code:

Dim NowDate As Variant
Dim NowYear As Var
    NowDate = Date
    NowYear = Year(NowDate)
Dim VarYear As Variant
If NowYear < 10 / 1 Then
    VarYear = NowYear
ElseIf NowYear >= 10 / 1 Then
    VarYear = NowYear + 1
End If

I know it probably has something to do with not assigning the variables correctly but I have troubleshot a couple things with no solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Or a slightly more general approach using Month:

If Month(Date) < 10 Then
   VarYear = Year(Date)
Else
   VarYear = Year(Date) + 1
End If

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...