VBA:复制值运行时错误1004

时间:2017-05-22 18:18:09

标签: excel vba excel-vba

我尝试将第六张中的范围复制到第一张或第三张,具体取决于子字符串值。我收到了"运行时错误' 1004'"当我尝试使用此代码时。我尝试找到一个解决方案 - 我能找到的唯一可能的答案是添加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"

在错误之后启动调试器时,它会特别突出显示ElseIf结果:

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

修改

感谢下面回答的人的帮助,我能够解决这个问题。我从.Cells移除了.Range,并通过.Range("I" & y1 & ":AV" & y1)引用了我想要的单元格(更改的第一个示例)。另外改变了我引用我的工作表的方式,因为我将它们定义为变量wsRwsLwsH。也添加在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

2 个答案:

答案 0 :(得分:4)

这是一个非常常见的问题 - 您尚未向单元格添加工作表引用(并且并非所有范围都符合您应该补救的范围)。

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

答案 1 :(得分:2)

99% of the time the 1004 error is because you have sloppily defined range objects.

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进行动态分配方法导致失败,因为Cells作为隐藏_Globals命名空间的一部分,引用ActiveSheet.Cells

无论哪张表处于活动状态,此陈述都将失败,因为该陈述的一方或另一方无法进行评估。