VBA:复制值运行时错误1004

我试图根据子string值将第六个表格中的范围复制到第一个或第三个表格中。 当我尝试使用此代码时,出现“运行时错误”1004“”。 我试图find一个解决scheme – 我能find的唯一可能的答案是添加一个ThisWorkbook.Save ,但这并没有改变结果。

 Dim LastRow As Long 'Finds last row With ActiveSheet LastRow = .Cells(.Rows.count, "A").End(xlUp).Row End With 'Iterates through rows in column A, and copies the row into proper sheet depending on "H" or "L" For i = 4 To LastRow If InStr(1, Range("A" & i), "L") <> 0 Then ThisWorkbook.Worksheets(1).Range(Cells(i, 9), Cells(i, 48)).Value = ThisWorkbook.Worksheets(6).Range(Cells(i, 1), Cells(i, 40)).Value ElseIf InStr(1, Range("A" & i), "H") <> 0 Then ThisWorkbook.Worksheets(3).Range(Cells(i, 9), Cells(i, 48)).Value = ThisWorkbook.Worksheets(6).Range(Cells(i, 1), Cells(i, 40)).Value End If Next i 'Message Box when tasks are completed MsgBox "Complete" 

当错误发生后,debugging器启动时,它特别强调了ElseIf结果:

 ThisWorkbook.Worksheets(3).Range(Cells(i, 9), Cells(i, 48)).Value = ThisWorkbook.Worksheets(6).Range(Cells(i, 1), Cells(i, 40)).Value 

编辑

感谢下面回答的人的帮助,我解决了这个问题。 我从.Range删除了.Cells ,并通过.Range("I" & y1 & ":AV" & y1) (第一个变化示例.Range("I" & y1 & ":AV" & y1)引用了我想要的单元格。 另外改变了我引用我的工作表的方式,因为我将它们定义为variableswsRwsLwsH 。 还在With / End With语句中添加。

 Private Sub CommandButton2_Click() Dim LastRow As Long Dim wsR As Worksheet Dim wsL As Worksheet Dim wsH As Worksheet Dim y1 As Integer Dim y2 As Integer 'Set row value for light chain y1 = 4 'Set row value for heavy chain y2 = 4 'Set raw data worksheet Set wsR = ThisWorkbook.Worksheets(6) 'Set light chain worksheet Set wsL = ThisWorkbook.Worksheets(1) 'Set heavy chain worksheet Set wsH = ThisWorkbook.Worksheets(3) 'Finds last row With wsR LastRow = .Cells(.Rows.count, "A").End(xlUp).Row End With 'Iterates through rows in column A, and copies the row into proper sheet depending on "H" or "L" For i = 4 To LastRow 'Checks for "L" for light chain If InStr(1, Range("A" & i), "L") <> 0 Then With wsL .Range("I" & y1 & ":AV" & y1).Value = wsR.Range("A" & i & ":AN" & i).Value End With y1 = y1 + 1 'Checks for "H" for heavy chain ElseIf InStr(1, Range("A" & i), "H") <> 0 Then With wsH .Range("I" & y2 & ":AV" & y2).Value = wsR.Range("A" & i & ":AN" & i).Value End With y2 = y2 + 1 End If Next i 'Message Box when tasks are completed MsgBox "Complete" End Sub 

这是一个非常普遍的问题 – 您没有为您的单元格添加工作表引用(并且并非您所有的范围都有资格进行修改)。

 Sub x() Dim LastRow As Long, ws As Worksheet Set ws = ThisWorkbook.Worksheets(6) 'Finds last row With ActiveSheet LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With 'Iterates through rows in column A, and copies the row into proper sheet depending on "H" or "L" For i = 4 To LastRow If InStr(1, Range("A" & i), "L") <> 0 Then With ThisWorkbook.Worksheets(1) .Range(.Cells(i, 9), .Cells(i, 48)).Value = ws.Range(ws.Cells(i, 1), ws.Cells(i, 40)).Value End With ElseIf InStr(1, Range("A" & i), "H") <> 0 Then With ThisWorkbook.Worksheets(3) .Range(.Cells(i, 9), .Cells(i, 48)).Value = ws.Range(ws.Cells(i, 1), ws.Cells(i, 40)).Value End With End If Next i 'Message Box when tasks are completed MsgBox "Complete" End Sub 

994%的时间1004错误是因为你有斜坡定义的范围对象。

 ThisWorkbook.Worksheets(3).Range(Cells(i, 9), Cells(i, 48)).Value = _ ThisWorkbook.Worksheets(6).Range(Cells(i, 1), Cells(i, 40)).Value 

这个赋值语句的左边和右边都是合格的范围(分别是Worksheets(3)Worksheets(6) ),但是使用Cells方法的dynamic分配会导致失败,因为Cells as隐藏的_Globals名称空间的一部分,是指ActiveSheet.Cells

无论哪个表单处于活动状态,此语句都将失败,因为该语句的一个或另一个不能被评估。