Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel year()函数有问题_Excel_Vba - Fatal编程技术网

Excel year()函数有问题

Excel year()函数有问题,excel,vba,Excel,Vba,我正试图创建一个模块,从一个巨大的电子表格中提取数据,我每年都会在另一张表格上看到这些数据。代码的每一部分都有效,除了与用户输入的年份相匹配的部分 下面是我如何定义用户输入以及如何尝试编写if语句 Dim y As variant y = InputBox("Input year here") If Year(RptSht.Cells(i, 2)) = y 在这一点上,我得到了一个类型不匹配(我尝试将y设置为整数)。也可以作为我可以使用的注释 Year(RptSht.Cells(i, 2))

我正试图创建一个模块,从一个巨大的电子表格中提取数据,我每年都会在另一张表格上看到这些数据。代码的每一部分都有效,除了与用户输入的年份相匹配的部分

下面是我如何定义用户输入以及如何尝试编写if语句

Dim y As variant
y = InputBox("Input year here")

If Year(RptSht.Cells(i, 2)) = y
在这一点上,我得到了一个类型不匹配(我尝试将y设置为整数)。也可以作为我可以使用的注释

Year(RptSht.Cells(i, 2)) 

要获得一个值,它只是与
y
不匹配。任何帮助都将不胜感激。

我想问题在于
I
没有分配,或者
(I,2)
不是日期。试试这个:

Sub TestMe()

    Dim y As Variant
    Dim i As Long

    y = InputBox("Input year here")
    i = 5

    If IsDate(Worksheets(1).Cells(i, 2)) Then
        If Year(Worksheets(1).Cells(i, 2)) = y Then
             'Logic here
        End If
    End If

End Sub

因此,
i
5
,参考单元格为
B5
IsDate()
检查单元格是否为日期。

以下是处理此问题的一种方法:

Sub gotimm()
    Dim y As Long, RptSht As Worksheet, i As Long

    y = Application.InputBox(Prompt:="Input year here", Type:=1)
    Set RptSht = ActiveSheet
    i = 1

    With RptSht
        If IsDate(.Cells(i, 2)) Then
            If .Cells(i, 2) = y Then
                MsgBox "match"
            Else
                MsgBox "nomatch"
            End If
        Else
            MsgBox "no date in cell " & .Cells(i, 2).Address
        End If
    End With
End Sub
那太多了。把它分开

首先,您希望在
(i,2)
处获取单元格:

现在,我们不能仅仅假设
yearCellValue
是一个有效日期。我们必须知道这一点,否则如果假设有任何错误,我们很可能会遇到类型不匹配错误。使用
IsDate
功能确保查看的是
Date
值:

If IsDate(yearCellValue) Then

End If
在该条件块中,
Year(yearCellValue)
是安全的。除此之外,情况并非如此

If IsDate(yearCellValue) Then
    If Year(yearCellValue) = y Then
        '...
    End If
End If
问题是,我们也不知道
y
是有效值

Dim y As variant
y = InputBox("Input year here")
If Not IsNumeric(y) Then Exit Sub 'bail out, we can't go any further.

VBA中的
Year()
返回一个
Integer
Long
类型。您正在将
y声明为Variant
。更改为
Dim y,只要长
。还要注意的是,无论何时与用户交互,都应该查看错误处理。也就是说,当使用
输入框时
如何检查用户输入的信息是一年?单元格(i,2)的格式是什么?它是日期、整数还是文本?另外,如果你仍然得到
类型不匹配
,那么你需要评估
RptSht.Cells(i,2)是否包含日期,因为
Year()`需要日期。@Dean我试着将y改为整数和long,但仍然得到错误我的预感是OP将其从帖子中删除为“无关”,由于
.Cells(0,2)
将是错误1004,而不是13。解决了该问题,一些单元格为空白,并返回了vba试图比较的错误y@gotimm还有其他问题,
y
也没有得到验证。++vaildating
y
也很重要。@MathieuGuindon我同意。您的答案更好。IDK关于-
应用程序。InputBox
with
Type:=1
是一种简洁的方法,可以确保您在IMO中获得有效的数值-只需处理可能的取消。我只有一个单元格在数据列表的最开始处格式不正确,但是我添加了日期检查,以确保在订单报告输入错误时不会中断future@gotimm请注意,它利用了
应用程序.InputBox
功能(这与
InputBox
不同,后者完全限定为
VBA.Interaction.InputBox
)为了确保用户只能输入一个数值,基本上可以免费获得验证。
If IsDate(yearCellValue) Then
    If Year(yearCellValue) = y Then
        '...
    End If
End If
Dim y As variant
y = InputBox("Input year here")
If Not IsNumeric(y) Then Exit Sub 'bail out, we can't go any further.