PasteSpecial(运行时错误1004)

时间:2017-07-10 11:36:38

标签: excel vba excel-vba for-loop copy-paste

我想将数据(公式)从一本书的一张复制到另一张,然后也在其他书籍及其表格中。给出错误(运行时错误1004)。 插入的位置只插入值(不带公式)。 行错误:Selection.PasteSpecial粘贴:= xlPasteAll

    Sub Скопирователь()
    Call Скопирователь1
    Call Скопирователь2
    End Sub


    Sub Скопирователь1()
    Dim book1 As Workbook
    Dim book2 As Workbook
    Dim A As String
    Dim n As Long
    Dim B()
    A = "P34:P99"
    Set book2 = Workbooks.Open("E:\Super M\Проект ставки\Поиск решения\Усов 6\Пишем условие vba\4-ое место\6.xlsm")
    book2.Worksheets("6").Activate
    Range("" + A + "").Copy

    B = Array("7", "8", "16", "17", "21", "22", "23", "25", "26", "29", "30", "31", "54", "55", "56", "57", "58", "59")

    'условия
    For n = 0 To 17

    Set book1 = Workbooks.Open("E:\Super M\Проект ставки\Поиск решения\Усов 6\Пишем условие vba\4-ое место\" & B(n) & ".xlsm")
    book1.Worksheets("" & B(n) & "").Activate
    Range("" + A + "").Select
    Selection.PasteSpecial Paste:=xlPasteAll
    'все листы
    'Листы
    book1.Save
    book1.Close
    Next n

   End Sub

   Sub Скопирователь2()
   Dim book1 As Workbook
   Dim book2 As Workbook
   Dim A As String
   Dim n, t As Long
   Dim B()
   Dim M()
   A = "P34:P99"
   B = Array("6", "7", "8", "16", "17", "21", "22", "23", "25", "26", "29", "30", "31", "54", "55", "56", "57", "58", "59")
   M = Array("Лист1", "Лист2", "Лист3", "Лист4", "Лист5", "Лист6", "Лист7", "Лист8", "Лист9", "Лист10", "Лист11", "Лист12", "Лист13", "Лист14", "Лист15", "Лист16", "Лист17", "Лист18", "Лист19")
   'условия
   For n = 0 To 18

   Set book1 = Workbooks.Open("E:\Super M\Проект ставки\Поиск решения\Усов 6\Пишем условие vba\4-ое место\" & B(n) & ".xlsm")


   book1.Worksheets("" & B(n) & "").Activate
   Range("" + A + "").Copy
   'все листы
   'Листы
    For t = 0 To 18
       book1.Worksheets("" & M(t) & "").Activate
           Range("" + A + "").Select
           Selection.PasteSpecial Paste:=xlPasteAll
    Next t
    book1.Save
    book1.Close
    Next n

    End Sub

1 个答案:

答案 0 :(得分:3)

虽然这是西里尔语代码的一半(顺便说一句,我喜欢西里尔语)但我无法复制它,我想你的错误是在第二个Selection.PasteSpecial Paste:=xlPasteAll

因此,改变:

Range("" + A + "").Select 至: Range("A:A").Select

总共有4次Range("" + A + ""),你应该随处更改它。通常,在VBA中应避免使用SelectActivate

玩得开心! :)