运行时错误' 1004':应用程序定义或对象定义的错误。 (复制和粘贴结果)

时间:2015-07-26 11:47:39

标签: excel

我正在尝试将表单a的结果相应地放到不同的表格中。

Sub update()
Dim cell As String, sheet As String, rcdate As String, row As Integer
row = 2
ThisWorkbook.Sheets("Auto Generate V2").Activate
cell = CStr(ThisWorkbook.Sheets("Auto Generate V2").Cells(row, 10).value)
Do While cell <> ""
sheet = Left(cell, 10)
rcdate = Right(cell, 3)
ThisWorkbook.Sheets(sheet).Activate
ThisWorkbook.Sheets(sheet).Range(rcdate).value =                           ThisWorkbook.Sheets("Auto Generate V2").Cells(row, 5).value
row = row + 1
cell = CStr(ThisWorkbook.Sheets("Auto Generate V2").Cells(row, 10).value)
Loop
End Sub

错误发生在     ThisWorkbook.Sheets(sheet).Range(acdate).value = ThisWorkbook.Sheets(&#34; Auto Generate V2&#34;)。Cells(row,5).value 我该如何解决呢?

1 个答案:

答案 0 :(得分:0)

不必要的Worksheet.Activate method无疑会导致不稳定的环境。我已修改您的例程以消除对.ActiveSheet的依赖。

Sub update()
    Dim cell As String, sht As String, rcDate As String, rw As Long
    rw = 2
    With ThisWorkbook.Worksheets("Auto Generate V2")
        cell = Trim(CStr(.Cells(rw, 10).Value))
        Do While cell <> ""
            sht = Left(cell, 10)
            rcDate = Right(cell, 3)
            'Debug.Print .Parent.Worksheets(sht).Range(rcDate).Address(external:=True)
            .Parent.Worksheets(sht).Range(rcDate) = .Cells(rw, 5).Value
            rw = rw + 1
            cell = Trim(CStr(.Cells(rw, 10).Value))
        Loop
    End With
End Sub

如果您仍然遇到问题,请考虑关闭所有内容并重新启动计算机。在开发sub期间发生的一系列崩溃可能导致潜在的不稳定编译器环境。一次简单的机器重启不止一次修复了一个未知问题。