【问题标题】:Error 1004 Paste Method of Worksheet Class Failed, Intermittent错误 1004 工作表类的粘贴方法失败,间歇性
【发布时间】:2016-06-15 13:24:04
【问题描述】:

此代码的目的是从 Word 文档中搜索数字并将其复制到 Excel 电子表格中。它不会一直发生,但是当我运行此脚本时,我会不时收到 1004 错误。调试器将“Do While Loop”下的first“ActiveSheet.Paste”语句突出显示为代码问题。我没有看到与脚本的任何其他部分有任何冲突。有人发现有什么不对吗?

Sub TorCopy()

    ' Set variables
    Dim Word As New Word.Application
    Dim WordDoc As New Word.Document
    Dim i As Integer
    Dim j As Integer
    Dim r As Word.range
    Dim Doc_Path As String
    Dim TOR_Tracker As Excel.Workbook
    Dim TOR_Tracker_Path As String
    Dim Whiteboard_Path As String
    Dim Whiteboard As Excel.Workbook
    Dim n As Integer

    ' Set File Path that contains TOR
    ' Open File
    Doc_Path = "C:\Word_File.doc"
    Set WordDoc = Word.Documents.Open(Doc_Path)

    TOR_Tracker_Path = "C:\Tracker_Spreadsheet.xlsm"
    Set TOR_Tracker = Workbooks.Open(TOR_Tracker_Path)

    Whiteboard_Path = "C:\Excel_Spreadsheet_Acting_As_A_Whiteboard.xlsm"
    Set Whiteboard = Workbooks.Open(Whiteboard_Path)

    Whiteboard.Worksheets("Sheet1").Activate

    ' Create a range to search
    Set r = WordDoc.Content

    j = 1

    ' Find TOR numbers and copy them to whiteboard spreadsheet
    With r
        .Find.ClearFormatting
        With .Find
            .Text = "TP[0-9]{4}"
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
        End With
        Do While .Find.Execute = True
            .Copy
            ActiveSheet.Cells(j, 1).Select
            ActiveSheet.Paste
            j = j + 1
        Loop
    End With

    ' Filter out duplicate TOR numbers
    n = Cells(Rows.Count, "A").End(xlUp).Row
    ActiveSheet.range("A1:A" & n).RemoveDuplicates Columns:=1, Header:=xlNo

    ' Copy TOR numbers from whiteboard
    With ActiveSheet
        .range("A1").Select
        .range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
    End With

    ' Paste to TOR Tracker
    TOR_Tracker.Worksheets("Sheet1").Activate
    With ActiveSheet
        .range("A1").Select
        Selection.End(xlDown).Select
        Selection.End(xlDown).Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1, 4).Select
        ActiveSheet.Paste
    End With

    Whiteboard.Close
    WordDoc.Close
    Word.Quit

End Sub

【问题讨论】:

  • 有没有 TOR_Tracker.Worksheets("Sheet1") 不存在的实例?你也可以只放 .Paste,With 已经指定了 ActiveSheet。
  • 尽可能避免使用.SelectActiveSheet。你真的不需要使用任何一个。将对象引用设置为您要在文件中使用的特定工作表,并改为引用该工作表。它更快、更可靠。
  • 我刚刚编辑了我的帖子,有两个 ActiveSheet.Paste 语句,我只有第一个有问题。 @Dave,您能否举例说明将对象引用设置为特定工作表的含义?
  • Set whiteboard = Whiteboard.Worksheets("Sheet1") 将允许您使用whiteboard 在代码中的任何位置(好吧,无论如何它在范围内)引用该工作表,例如whiteboard.Range("A1").Value 将为您提供指定工作表上 A1 的内容。如果不出意外,它将使您的代码更具可读性
  • 查看发布的答案。如果它不起作用,请告诉我,它是在我的脑海中编码的,我还没有机会测试它!

标签: excel vba ms-office


【解决方案1】:

根据 cmets,我已修改代码以删除使用 .Select.Activate 等类型语句

Sub TorCopy()

    ' Set variables
    Dim Word As New Word.Application
    Dim WordDoc As New Word.Document
    Dim i As Integer
    Dim j As Integer
    Dim r As Word.range
    Dim Doc_Path As String
    Dim TOR_Tracker As Excel.Workbook
    Dim TOR_Tracker_Path As String
    Dim Whiteboard_Path As String
    Dim Whiteboard As Excel.Workbook
    Dim whiteSheet as Worksheet
    Dim torSheet as Worksheet
    Dim n As Integer

    ' Set File Path that contains TOR
    ' Open File
    Doc_Path = "C:\Word_File.doc"
    Set WordDoc = Word.Documents.Open(Doc_Path)

    TOR_Tracker_Path = "C:\Tracker_Spreadsheet.xlsm"
    Set TOR_Tracker = Workbooks.Open(TOR_Tracker_Path)
    Set torSheet = TOR_Tracker.Worksheets("Sheet1")

    Whiteboard_Path = "C:\Excel_Spreadsheet_Acting_As_A_Whiteboard.xlsm"
    Set Whiteboard = Workbooks.Open(Whiteboard_Path)
    Set whiteSheet = Whiteboard.Worksheets("Sheet1")

    ' Create a range to search
    Set r = WordDoc.Content

    j = 1

    ' Find TOR numbers and copy them to whiteboard spreadsheet
    With r
        .Find.ClearFormatting
        With .Find
            .Text = "TP[0-9]{4}"
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
        End With
        Do While .Find.Execute = True
            .Copy
            whiteSheet.Cells(j, 1).PasteSpecial
            j = j + 1
        Loop
    End With

    ' Filter out duplicate TOR numbers
    n = whiteSheet.Cells(whiteSheet.Rows.Count, "A").End(xlUp).Row
    whiteSheet.range("A1:A" & n).RemoveDuplicates Columns:=1, Header:=xlNo
    n = whiteSheet.Cells(whiteSheet.Rows.Count, "A").End(xlUp).Row ' re-getting the last row now duplicates are removed

    lastRowTor = torSheet.Cells(torSheet.Rows.Count, "A").End(xlUp).Row

    torSheet.Range("A" & lastRowTor & ":A" & (lastRowTor + n)-1).Value = whiteSheet.Range("A1:A" & n).Value ' sets values in Tor from White without Select, Copy or Paste

    Whiteboard.Close
    WordDoc.Close
    Word.Quit

End Sub

【讨论】:

  • 嘿,我尝试运行它,但出现“对象不支持此属性或方法”错误,调试器说这是因为“whiteSheet.Cells(j, 1).粘贴”行。
  • 哎呀 - Excel VBA 使用 PasteSpecial 而不是 Paste。我糟糕的更新代码
  • 不幸的是,我现在收到“PasteSpecial 范围类方法失败”错误。你得到同样的东西吗?
猜你喜欢
  • 2016-06-16
  • 1970-01-01
  • 1970-01-01
  • 2014-09-08
  • 2022-08-14
  • 1970-01-01
  • 2017-09-03
  • 2018-12-07
  • 1970-01-01
相关资源
最近更新 更多