DoC上的VBAtypes不匹配错误,而ActiveCell.Value <>“”

嘿,我已经有代码移动到另一个工作表的单元格的名单,一个循环,直到它在数据的末尾空白,这里的代码提取;

Range("AF2").Select Do While ActiveCell.Value <> "" strDestinationSheet = ActiveCell.Value ActiveCell.Offset(0, -31).Resize(1, ActiveCell.CurrentRegion.Columns.count).Select Selection.Copy Sheets(strDestinationSheet).Select N = Cells(Rows.count, "AF").End(xlUp).Row lastRow = N Cells(lastRow + 1, 1).Select Selection.PasteSpecial xlPasteValues Application.CutCopyMode = False Sheets(strSourceSheet).Select ActiveCell.Offset(0, 31).Select ActiveCell.Offset(1, 0).Select Loop 

然而,虽然这部分代码工作正常之前运行它的最新实例现在在第二行抛出一个错误尽pipeActiveCell.Value <>“”,说types不匹配。 我不确定发生了什么变化,突然停止工作,有什么想法? 非常感谢。

我个人不喜欢Do循环遍历一组单元格。 我更喜欢For Each循环。

同样如@bruceWayne所述,避免使用Select as as slowed the code。

适当的缩进使得代码更易于阅读并避免简单的错误。

 Dim cel As Range With Sheets(strSourceSheet) For Each cel In .Range("AF2", .Range("AF2").End(xlDown)) If Not IsError(cel) Then strDestinationSheet = cel.Value cel.Offset(0, -31).Resize(1, cel.CurrentRegion.Columns.Count).Copy N = Sheets(strDestinationSheet).Cells(Sheets(strDestinationSheet).Rows.Count, "AF").End(xlUp).Row Sheets(strDestinationSheet).Cells(N + 1, 1).PasteSpecial xlPasteValues End If Next cel End With