Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 跳过类型不匹配_Excel_Vba_Loops_Error Handling_Skip - Fatal编程技术网

Excel 跳过类型不匹配

Excel 跳过类型不匹配,excel,vba,loops,error-handling,skip,Excel,Vba,Loops,Error Handling,Skip,在“Value=”行中,循环中有一些实例会遇到类型不匹配或空单元格 有人能解释一下,如果数据集中有错误,我如何使用错误测试跳过这一步并继续我的循环吗 谢谢大家! Sub ExpDate() Dim bRow As Double Dim tRow As Double Dim lCol As Double Dim fCol As Double Dim ListRow As Double Dim Value As Date With ThisWorkbook.Worksheets("Canadi

在“Value=”行中,循环中有一些实例会遇到类型不匹配或空单元格

有人能解释一下,如果数据集中有错误,我如何使用错误测试跳过这一步并继续我的循环吗

谢谢大家!

Sub ExpDate()

Dim bRow As Double
Dim tRow As Double
Dim lCol As Double
Dim fCol As Double
Dim ListRow As Double

Dim Value As Date

With ThisWorkbook.Worksheets("Canadian")

bRow = Cells(Rows.Count, 5).End(xlUp).row
tRow = 5
fCol = 7

Do While tRow <= bRow
    lCol = Cells(tRow, Columns.Count).End(xlToLeft).Column

    Do While fCol <= lCol


        Value = Cells(tRow, fCol).Value

        ListRow = Cells(Rows.Count, 1).End(xlUp).row + 1
        Cells(ListRow, 1).Value = Value


    fCol = fCol + 1
    Loop

fCol = 7
tRow = tRow + 1
Loop


Range("A5:A1000").RemoveDuplicates Columns:=Array(1, 1), Header:=xlYes

End With

End Sub
Sub-ExpDate()
双眉
双倍暗步
Dim lCol作为双
将fCol设置为双精度
将列表行变为双行
将值变为日期
使用此工作簿。工作表(“加拿大”)
bRow=单元格(Rows.Count,5).结束(xlUp).行
tRow=5
fCol=7
边走边做几件事

您只需要检查单元格是否包含日期

对整数变量使用
Long
,而不是
Double

您的With语句是多余的,因为您需要在范围引用前面使用点-我已经添加了它们

Sub ExpDate()

Dim bRow As Long
Dim tRow As Long
Dim lCol As Long
Dim fCol As Long
Dim ListRow As Long
Dim Value As Date

With ThisWorkbook.Worksheets("Canadian")
    bRow = .Cells(Rows.Count, 5).End(xlUp).Row
    tRow = 5
    fCol = 7

    Do While tRow <= bRow
        lCol = .Cells(tRow, Columns.Count).End(xlToLeft).Column
        Do While fCol <= lCol
            If IsDate(.Cells(tRow, fCol).Value) Then
                ListRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
                .Cells(ListRow, 1).Value = .Cells(tRow, fCol).Value
                fCol = fCol + 1
            End If
        Loop
        fCol = 7
        tRow = tRow + 1
    Loop
    .Range("A5:A1000").RemoveDuplicates Columns:=Array(1, 1), Header:=xlYes
End With

End Sub
Sub-ExpDate()
黯淡的眉毛
昏昏欲睡
暗淡的lCol尽可能长
模糊fCol尽可能长
长得一样暗
将值变为日期
使用此工作簿。工作表(“加拿大”)
bRow=.Cells(Rows.Count,5).End(xlUp).Row
tRow=5
fCol=7
边走边做几件事

您只需要检查单元格是否包含日期

对整数变量使用
Long
,而不是
Double

您的With语句是多余的,因为您需要在范围引用前面使用点-我已经添加了它们

Sub ExpDate()

Dim bRow As Long
Dim tRow As Long
Dim lCol As Long
Dim fCol As Long
Dim ListRow As Long
Dim Value As Date

With ThisWorkbook.Worksheets("Canadian")
    bRow = .Cells(Rows.Count, 5).End(xlUp).Row
    tRow = 5
    fCol = 7

    Do While tRow <= bRow
        lCol = .Cells(tRow, Columns.Count).End(xlToLeft).Column
        Do While fCol <= lCol
            If IsDate(.Cells(tRow, fCol).Value) Then
                ListRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
                .Cells(ListRow, 1).Value = .Cells(tRow, fCol).Value
                fCol = fCol + 1
            End If
        Loop
        fCol = 7
        tRow = tRow + 1
    Loop
    .Range("A5:A1000").RemoveDuplicates Columns:=Array(1, 1), Header:=xlYes
End With

End Sub
Sub-ExpDate()
黯淡的眉毛
昏昏欲睡
暗淡的lCol尽可能长
模糊fCol尽可能长
长得一样暗
将值变为日期
使用此工作簿。工作表(“加拿大”)
bRow=.Cells(Rows.Count,5).End(xlUp).Row
tRow=5
fCol=7

当tRow时,假设您的输入看起来像列
A
,并且您希望将日期传递到列
B

有两个问题需要考虑——第3行和第5行的单元格。第5行可以很容易地检查,只要它是一个错误,
?iError(单元格(5,1)
将返回
True
。但是,如果试图检查
?iError(Cdate(“K”)
,就会出现问题

一种快速修复它的方法是一个专用的布尔函数,其中有
On Error Resume Next
,如果
CDate(value)
转换中有任何特定错误,则返回
True

Sub TestMe()

    Dim target As Range
    Dim myCell As Range
    Set target = Worksheets(1).Range("A1:A6")

    For Each myCell In target
        If IsCellDate(myCell) Then
            Dim someDate As Date
            someDate = myCell
            myCell.Offset(0, 1) = someDate
        End If
    Next

End Sub

Public Function IsCellDate(myData As Variant) As Boolean

    On Error Resume Next '- use this line really with caution!

    If IsError(CDate(myData)) Then
        IsCellDate = False
        Exit Function
    End If
    IsCellDate = True

End Function

或者,您可以使用
IsDate()
并避免使用中的自定义函数。

假设您的输入看起来像列
A
,您希望将日期传递到列
B

有两个问题需要考虑-第3行和第5行中的单元格。第5行可以很容易地检查,只要它是一个错误,
?iError(单元格(5,1)
将返回
True
。但是,如果试图检查
?iError(Cdate(“K”)
,就会出现问题

一种快速修复它的方法是一个专用的布尔函数,其中有
On Error Resume Next
,如果
CDate(value)
转换中有任何特定错误,则返回
True

Sub TestMe()

    Dim target As Range
    Dim myCell As Range
    Set target = Worksheets(1).Range("A1:A6")

    For Each myCell In target
        If IsCellDate(myCell) Then
            Dim someDate As Date
            someDate = myCell
            myCell.Offset(0, 1) = someDate
        End If
    Next

End Sub

Public Function IsCellDate(myData As Variant) As Boolean

    On Error Resume Next '- use this line really with caution!

    If IsError(CDate(myData)) Then
        IsCellDate = False
        Exit Function
    End If
    IsCellDate = True

End Function

或者您可以使用
IsDate()
并避免使用中的自定义函数。

使用名为
Value的日期变量其实不是一个好主意……您应该包括点运算符(.)当使用With BLOCK时,在上面的内部无法充分向上投票@QHarr的评论,因此我想我会重复它。因为它是你的
With…End With
没有任何作用-当你运行代码时,让另一个工作表处于活动状态,并看到它造成的混乱。使用
.Cells
范围
引用加拿大工作表中的单元格ide块。使用名为
Value
的日期变量并不是一个好主意……您应该包括点运算符(.)当使用With BLOCK时,在上面的内部无法充分向上投票@QHarr的评论,因此我想我会重复它。因为它是你的
With…End With
没有任何作用-当你运行代码时,让另一个工作表处于活动状态,并看到它造成的混乱。使用
.Cells
范围
引用加拿大工作表中的单元格这是我的荣幸,这是我的荣幸。