macros将最后5列中的空单元格的行复制到其他工作表

如上所述,我试图编写一个程序,查看Sheet2中每行的最后5列,如果最后5列中的任何单元格为空,则复制该行,然后将该行粘贴到新工作表Sheet3 。 我一直在search,并find一些有用的链接,但我不断收到“对象需要”的错误。 我有一种感觉,我忘记了一件简单的事情,但我没有得到任何地方。 这是我第二天尝试macros,我感谢任何帮助!

Sub missingDataCopy() Dim startColumn As Integer Dim startRow As Integer Dim totalRows As Integer Dim totalColumns As Integer Dim currentColumn As Integer Dim currentRow As Integer Dim shouldCopyRow As Boolean Dim j As Long startColumn = 7 totalColumns = 11 startRow = 1 j = 1 totalRows = Sheet2.Cells(Rows.Count, startColumn).End(xlUp).Row For currentRow = totalRows To startRow Step -1 shouldCopyRow = False For currentColumn = startColumn To totalColumns If IsEmpty(Sheet2.Cells(currentRow, currentColumn)) Then shouldCopyRow = True Exit For End If Next If shouldCopyRow Then Sheet2.Cells(currentRow, currentColumn).EntireRow.Copy Destination:=Worksheets("Sheet3").Range("A" & j) j = j + 1 End If 

您正在收到的错误表明您的Excel文件中没有CodeName Sheet2 。 看看下面的插图,看看工作表的Name (在标签中看到的)和CodeName之间的区别:

在这里输入图像说明

如果你想用CodeName编码,那么你必须在VBE中修改它,如上所示。 或者,您也可以使用工作表的Name 。 但是,你的行应该是这样的:

 totalRows = ThisWorkbook.Worksheets("Sheet1").Cells(ThisWorkbook.Worksheets("Sheet1").Rows.Count, startColumn).End(xlUp).Row 

因为在上面的示例中工作表的名称是Sheet1