gpt4 book ai didi

vba - 错误处理程序不处理类型不匹配

转载 作者:行者123 更新时间:2023-12-03 08:22:14 26 4
gpt4 key购买 nike

由于某种原因,我的代码在下一行(记录出现在“#N/A”的位置)不断被打断为“错误13-类型不匹配”:

For x = FirstDataRow To LastRow
For y = Crrncy_Rng.Column + 2 To LastColumn
Select Case .Cells(x, y)
Case IsError(.Cells(x, y)) = True
GoTo err_handler
Case Is = "CHECK"
.Cells(x, LastColumn + 1) = "CHECK"
GoTo 20
Case Else
End Select
19 Next y
20 Next x

err_handler:
GoTo 19 '19 is a line where next j is specified
end sub

我的印象是,将“错误”作为潜在案例包括在内,可以使我下一步行动。还有其他办法吗?

谢谢!

最佳答案

是的,使用Select Case是一种忽略带有错误的单元格的方法。在您的情况下,如果遇到错误,则无需执行任何操作,因为您希望它转到下一个列,该列将全部完成。

Sub CheckForCheck()

Dim FirstDataRow As Long
Dim LastRow As Long
Dim Crrncy_Rng As Range
Dim LastColumn As Long
Dim x As Long, y As Long

FirstDataRow = 1
LastRow = 3
Set Crrncy_Rng = Sheet1.Range("A1")
LastColumn = 6

For x = FirstDataRow To LastRow
For y = Crrncy_Rng.Column + 2 To LastColumn
Select Case True
Case IsError(Sheet1.Cells(x, y).Value)
'Do nothing here and it will check the next column
Case Sheet1.Cells(x, y).Value = "CHECK"
Sheet1.Cells(x, LastColumn).Value = "CHECK"
Exit For 'This moves to the next row and is better than goto
End Select
Next y
Next x

End Sub

我鼓励任何愿意听的人不要使用 GOTOExit For完成了您想要的,但是具有仅向一个方向移动的优点。

关于vba - 错误处理程序不处理类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53155516/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com