在excel vba中使用WorksheetFunction.Year For循环

时间:2018-02-20 04:07:38

标签: excel vba excel-vba

我的工作表DailyData A列中有一系列日期。我需要找出它的闰年(在A列中)并且取决于在F列中执行计算。(对于闰年,我需要使用366个其他365)

发现使用以下公式,我可以检查它的闰年。

b = 4 a =要检查的年份

If a - (b * (a \ b)) = 0, then its leap year

现在,我使用下面的代码来执行计算,但它在"Object doesn't support this property or method"行上给出了错误a = WorksheetFunction.Year("A", J)

检查它的方法是什么?

Sub DayTest()
    Dim MaxGain As Workbook
    Dim Main As Worksheet
    Dim DailyData As Worksheet
    Dim n As Long
    Dim J As Long
    Dim a As String, b As Integer

    Set MaxGain = Excel.Workbooks("MaxGain.xlsm")
    Set DailyData = MaxGain.Worksheets("DailyData")
    Set Main = MaxGain.Worksheets("Main")
    DailyData.Activate


    n = DailyData.Cells(Rows.Count, "A").End(xlUp).Row

    J = 1
    b = 4

    For J = 2 To n

    a = WorksheetFunction.Year("A", J)

    If a - (b * (a \ b)) = 0 Then

    DailyData.Range("F" & J).Value = Main.Range("B2").Value / 366

    Else

    DailyData.Range("F" & J).Value = Main.Range("B2").Value / 365

    End If

    Next


End Sub

更新: 我已经想到了。改变了行

a = WorksheetFunction.Year("A", J)

a = Year(DailyData.Range("A" & J))

1 个答案:

答案 0 :(得分:0)

我已经想到了。更改为

以下
a = WorksheetFunction.Year("A", J)

a = Year(DailyData.Range("A" & J))