Excel VBA;尝试将数据从工作簿中的一个工作表复制到同一工作簿中的另一工作表时,运行时错误'438'

时间:2018-07-03 17:56:45

标签: excel vba excel-vba

我正在开发一种工具,该工具可以从一张工作表(称为“数据”)中获取数据行,然后将其粘贴到另一张工作表(称为“可打印报告”)中。每行对应一个不同的作业,并将获得自己的报告。当我运行代码时,出现运行时错误438。我尝试了不同的解决方案,但似乎都没有。如果有人可以看一下并帮助我弄清楚我的问题出在哪里,将不胜感激。错误发生在第8行。谢谢!

Sub ExportToReport()

Worksheets("Data").Activate


Dim x As Long, y As Long, z As Long


y = Sheets("Data").Range("A1", Range("A1").End(xlDown)).Rows.Count

For x = 2 To y

    Sheets("Data").Cells(x, 4).Select
    Selection.Copy
    Worksheets("Printable Report").Activate
    Sheets("Printable Report").Range("B1").Select
    Selection.Paste

    Worksheets("Data").Activate
    Sheets("Data").Cells(x, 9).Select
    Selection.Copy
    Worksheets("Printable Report").Activate
    Sheets("Printable Report").Range("B2").Select
    Selection.Paste

    Worksheets("Data").Activate
    Sheets("Data").Cells(x, 8).Select
    Selection.Copy
    Worksheets("Printable Report").Activate
    Sheets("Printable Report").Range("B3").Select
    Selection.Paste

    Worksheets("Data").Activate
    Sheets("Data").Cells(x, 10).Select
    Selection.Copy
    Worksheets("Printable Report").Activate
    Sheets("Printable Report").Range("B4").Select
    Selection.Paste

    Worksheets("Data").Activate
    Sheets("Data").Cells(x, 11).Select
    Selection.Copy
    Worksheets("Printable Report").Activate
    Sheets("Printable Report").Range("B5").Select
    Selection.Paste

    Worksheets("Data").Activate
    Sheets("Data").Cells(x, 12).Select
    Selection.Copy
    Worksheets("Printable Report").Activate
    Sheets("Printable Report").Range("B6").Select
    Selection.Paste

    Worksheets("Data").Activate
    Sheets("Data").Cells(x, 29).Select
    Selection.Copy
    Worksheets("Printable Report").Activate
    Sheets("Printable Report").Range("B7").Select
    Selection.Paste

    Worksheets("Data").Activate
    Sheets("Data").Cells(x, 25).Select
    Selection.Copy
    Worksheets("Printable Report").Activate
    Sheets("Printable Report").Range("B8").Select
    Selection.Paste

    Worksheets("Data").Activate
    Sheets("Data").Cells(x, 15).Select
    Selection.Copy
    Worksheets("Printable Report").Activate
    Sheets("Printable Report").Range("B9").Select
    Selection.Paste
    'Then insert a set number of cells on print report when move to next row on Data
    Sheets("Printable Report").Range("A1").EntireRow.Offset(1).Resize(36).Insert Shift:=xlDown
Next x
End Sub

2 个答案:

答案 0 :(得分:2)

您没有指定错误的出处,而是像这样更改所有代码块的结构:

Sheets("Data").Cells(x, 4).Select
Selection.Copy
Worksheets("Printable Report").Activate
Sheets("Printable Report").Range("B1").Select
Selection.Paste

对此:

Sheets("Printable Report").Range("B" & x - 1).Value = Sheets("Data").Cells(x,4).Value

,它应该可以正常工作,并且以后更易于阅读/重构。

答案 1 :(得分:2)

更改此内容

Worksheets("Data").Activate
Sheets("Data").Cells(x, 9).Select
Selection.Copy
Worksheets("Printable Report").Activate
Sheets("Printable Report").Range("B2").Select
Selection.Paste

...对此,

Worksheets("Data").Cells(x, 9).Copy _
  destination:=Worksheets("Printable Report").Range("B2")

您应该能够在代码的更下方重复进行方法上的更改。

请阅读How to avoid using Select in Excel VBA