types不匹配错误

如果ws1.cells(i,13)=“是”,那么“该列(列M)包含空白单元格或”是“ 。 我试过重新定义“我”作为string,并没有改变任何东西。 目标是对于列M中的“是”的每一行,将整行复制到名为“输出”的第二张表。 任何有关这个错误的帮助将非常感激,也可能适合我的目标的其他想法。 谢谢!

Sub Sadface() Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Trades") Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Output") For i = 2 To ws1.Range("M65536").End(xlUp).Row If ws1.Cells(i, 13) = "Yes" Then ws1.Rows(i).Copy ws2.Rows(ws2.Cells(ws2.Rows.Count, 2).End(xlUp).Row + 1) End If Next i End Sub 

这听起来好像你已经手动从外部数据中删除了错误。 如果将这些数据带入工作簿是一个定期重复的操作,您可能希望将其自动化。

 Sub Happyface() Dim i As Long Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Trades") Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Output") With ws1.Columns(13) On Error Resume Next .SpecialCells(xlCellTypeConstants, xlErrors).ClearContents .SpecialCells(xlCellTypeFormulas, xlErrors).ClearContents Err.Clear On Error GoTo 0 End With With ws1 For i = 2 To .Cells(Rows.Count, "M").End(xlUp).Row If .Cells(i, 13) = "Yes" Then .Rows(i).Copy ws2.Rows(ws2.Cells(ws2.Rows.Count, 2).End(xlUp).Row + 1) End If Next i End With End Sub 

尽量避免使用On Error Resume Next¹ ,这是处理Range.SpecialCells方法的最方便的方法,当您不确定它们是否存在时。

¹ 打破东西只是为了看看它是否存在的概念对我来说似乎一直是错误的。

试试这个,希望它有帮助

 Sub justforyou() x = 2 y = 2 'this will start the pasting process in the row 2, if you need to change it, you can change it Sheets("Output").Select Do While x <= 65536 If Sheets("Trades").Cells(x, 13).Value = "Yes" Then Sheets("Trades").Rows(x).Copy Cells(y, 1).Select ActiveSheet.Paste y = y + 1 End If x = x + 1 Loop 

结束小组