VBA Excel:运行时错误'1004'object'_Worksheet'faile的方法'范围'

我试图清除最后一行从工作表1复制到工作表2后的50行。因此,工作表2中的最后一行数据应该总是有50后面的空白行。

Public copied_row_count As Integer Public ws1 As Worksheet Public ws2 As Worksheet Sub Button1_Click() 'Defining the constants below. Change if need based on the value definitions' Dim Test_Set_Colmn_Number As Integer 'Defining the value of the Cylinder Test Set column number' Test_Set_Colmn_Number = 23 'W is the current column with the cylinder test set values' Set ws1 = ThisWorkbook.Sheets("Concrete Log") Set ws2 = ThisWorkbook.Sheets("Concrete Tracking Graphs") copied_row_count = 1 Dim j As Integer j = 0 For i = 1 To Rows.count If Not IsEmpty(Cells(i, Test_Set_Colmn_Number).Value) Then ws1.Rows(i).Copy _ ws2.Rows(copied_row_count) copied_row_count = copied_row_count + 1 End If Next i Do While j < 50 ws2.Range("copied_row_count + j:copied_row_count + j").Clear j = j + 1 Loop End Sub 

我已经将错误缩小到variablescopied_row_count及其在中使用

 ws2.Range("copied_row_count + j:copied_row_count + j").Clear 

谢谢!

您在引号内使用variables名称,因此将它们视为文字文本。

如果您要清除(copied_row_count + j)处的行,可以使用以下行:

 ws2.Rows(copied_row_count + j).ClearContents 

这应该是variables和string的组成:

 ws2.Range(copied_row_count & j & ":" & copied_row_count + j).Clear 

像之前一样( ws2.Range("copied_row_count + j:copied_row_count + j").Clear ),你直接在范围copied_row_count & j & : & copied_row_count + j显然不存在 – > Object defined error。

您编写范围查找的方式是查找字面上标记为"copied_row_count + j:copied_row_count + j" 。 如果使用的是像这样的引号,则不使用该variables。 我想你可以replace你的整个循环。

删除Do循环并尝试这一行

 ws2.Range(copied_row_count & ":" & copied_row_count + 50)