【发布时间】:2019-04-09 22:03:01
【问题描述】:
在“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
【问题讨论】:
-
拥有一个名为
Value的日期变量确实不是一个好主意... -
使用 With 块时,应在上面包含点运算符 (.)
-
不能对@QHarr 的评论给予足够的支持,所以我想我会重复一遍。因为它是您的
With...End With什么都不做 - 当您运行代码并查看它造成的混乱时,让另一个工作表处于活动状态。使用.Cells和.Range引用块内 Canadian 工作表中的单元格。
标签: excel vba loops error-handling skip