VBA:复制值运行时错误 1004

标签 vba excel

这个问题在这里已经有了答案:





Is the . in .Range necessary when defined by .Cells?

(3 个回答)


5年前关闭。




我正在尝试根据子字符串值将第六张纸中的范围复制到第一张或第三张纸。当我尝试使用此代码时,我收到“运行时错误'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) 引用了我想要的单元格(更改的第一个示例)。另外改变了我引用工作表的方式,因为我将它们定义为变量 wsR , wsL , 和 wsH .还添加到 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

关于VBA:复制值运行时错误 1004,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44119566/

相关文章:

vba - 使用 Vlookup 在 VBA 中查找日期时出现错误 2042

r - 通过 R 保持保护(或重新保护)导入的 Excel 工作簿的某些列

excel - 无法连接到 Excel SSIS

excel - ShellExecuteEx 在 Excel VBA 中崩溃

ms-access - 获取给定日期的周数

excel - 如何获取列中最长字符串的长度(Excel)?

vba - 遍历所有工作表以分别在每个选项卡中查找最底部的数据行

excel - 在 Excel 中使用 SQL "IN"函数

excel - Powershell等待Excel中执行宏

sorting - 排序而不移动格式