代码适用于其他工作簿,但不适用于此工作簿

时间:2019-12-18 01:13:26

标签: excel vba

嗨,我有将此代码分解为单元格的代码,它在另一个工作簿中运行时会运行,但是当我在其原始工作簿bdncasemacro.xlsm中运行它时会出错,它给出的错误代码为9。解决此问题,因为我无法在另一个工作簿中运行该错误。

Dim wscopy As Worksheet
Dim wspaste As Worksheet
Workbooks.Open "C:\Users\attkeej1\Desktop\testing\jobsummarydownloadmacro.xls"
Workbooks.Open "C:\Users\attkeej1\Desktop\testing\bdncasemacro.xlsm"
    Set wscopy = Workbooks("jobsummarydownloadmacro.xls").Worksheets("Report")
    Set wspaste = Workbooks("bdncasemacro.xlsm").Worksheets("Raw")

 wscopy.Range("A1:AZ1000").Copy _
    wspaste.Range("A1")

    wspaste.Cells.Select
        With Selection
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
            End With
    wspaste.Rows("1:5").Delete Shift:=xlShiftUp
    wspaste.Cells.Select
    Selection.ColumnWidth = 8.29
    wspaste.Range("C:C,E:F,H:H,J:M,O:R,T:T,V:W,Y:AA").Select
    wspaste.Range("Y1").Activate
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 4
    ActiveWindow.ScrollColumn = 5
    ActiveWindow.ScrollColumn = 6
    ActiveWindow.ScrollColumn = 7
    ActiveWindow.ScrollColumn = 8
    ActiveWindow.ScrollColumn = 9
    ActiveWindow.ScrollColumn = 10
    ActiveWindow.ScrollColumn = 11
    ActiveWindow.ScrollColumn = 12
    ActiveWindow.ScrollColumn = 13
    ActiveWindow.ScrollColumn = 14
    ActiveWindow.ScrollColumn = 15
    ActiveWindow.ScrollColumn = 16
    ActiveWindow.ScrollColumn = 17
    ActiveWindow.ScrollColumn = 18
    ActiveWindow.ScrollColumn = 19
    ActiveWindow.ScrollColumn = 20
    wspaste.Range( _
        "C:C,E:F,H:H,J:M,O:R,T:T,V:W,Y:AA,AC:AD,AF:AH,AJ:AJ,AL:AM,AO:AO,AQ:AR,AT:AU"). _
        Select
    wspaste.Range("AT1").Activate
    ActiveWindow.ScrollColumn = 21
    ActiveWindow.ScrollColumn = 22
    ActiveWindow.ScrollColumn = 23
    wspaste.Range( _
        "C:C,E:F,H:H,J:M,O:R,T:T,V:W,Y:AA,AC:AD,AF:AH,AJ:AJ,AL:AM,AO:AO,AQ:AR,AT:AU,AW:AY" _
        ).Select
   wspaste.Range("AW1").Activate
    Selection.Delete Shift:=xlToLeft
    wspaste.Range("AG23").Select
    ActiveWindow.ScrollColumn = 22
    ActiveWindow.ScrollColumn = 21
    ActiveWindow.ScrollColumn = 20
    ActiveWindow.ScrollColumn = 19
    ActiveWindow.ScrollColumn = 18
    ActiveWindow.ScrollColumn = 16
    ActiveWindow.ScrollColumn = 14
    ActiveWindow.ScrollColumn = 11
    ActiveWindow.ScrollColumn = 9
    ActiveWindow.ScrollColumn = 6
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 1
    wspaste.Cells.Select
    Selection.ColumnWidth = 9.71
    Selection.ColumnWidth = 13.71
    wspaste.Range("D14").Select

End Sub

但是当我尝试在原始工作簿中运行它并给出错误代码9时,它不起作用

Workbooks.Open "C:\Users\attkeej1\Desktop\testing\jobsummarydownloadmacro.xls"
    Set wscopy = Workbooks("jobsummarydownloadmacro.xls").Worksheets("Report")
    Set wspaste = ThisWorkbook.Worksheets("Raw")
 wscopy.Range("A1:AZ1000").Copy _
    wspaste.Range("A1")

    wspaste.Cells.Select
        With Selection
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
            End With
    wspaste.Rows("1:5").Delete Shift:=xlShiftUp
    wspaste.Cells.Select
    Selection.ColumnWidth = 8.29
    wspaste.Range("C:C,E:F,H:H,J:M,O:R,T:T,V:W,Y:AA").Select
    wspaste.Range("Y1").Activate
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 4
    ActiveWindow.ScrollColumn = 5
    ActiveWindow.ScrollColumn = 6
    ActiveWindow.ScrollColumn = 7
    ActiveWindow.ScrollColumn = 8
    ActiveWindow.ScrollColumn = 9
    ActiveWindow.ScrollColumn = 10
    ActiveWindow.ScrollColumn = 11
    ActiveWindow.ScrollColumn = 12
    ActiveWindow.ScrollColumn = 13
    ActiveWindow.ScrollColumn = 14
    ActiveWindow.ScrollColumn = 15
    ActiveWindow.ScrollColumn = 16
    ActiveWindow.ScrollColumn = 17
    ActiveWindow.ScrollColumn = 18
    ActiveWindow.ScrollColumn = 19
    ActiveWindow.ScrollColumn = 20
    wspaste.Range( _
        "C:C,E:F,H:H,J:M,O:R,T:T,V:W,Y:AA,AC:AD,AF:AH,AJ:AJ,AL:AM,AO:AO,AQ:AR,AT:AU"). _
        Select
    wspaste.Range("AT1").Activate
    ActiveWindow.ScrollColumn = 21
    ActiveWindow.ScrollColumn = 22
    ActiveWindow.ScrollColumn = 23
    wspaste.Range( _
        "C:C,E:F,H:H,J:M,O:R,T:T,V:W,Y:AA,AC:AD,AF:AH,AJ:AJ,AL:AM,AO:AO,AQ:AR,AT:AU,AW:AY" _
        ).Select
   wspaste.Range("AW1").Activate
    Selection.Delete Shift:=xlToLeft
    wspaste.Range("AG23").Select
    ActiveWindow.ScrollColumn = 22
    ActiveWindow.ScrollColumn = 21
    ActiveWindow.ScrollColumn = 20
    ActiveWindow.ScrollColumn = 19
    ActiveWindow.ScrollColumn = 18
    ActiveWindow.ScrollColumn = 16
    ActiveWindow.ScrollColumn = 14
    ActiveWindow.ScrollColumn = 11
    ActiveWindow.ScrollColumn = 9
    ActiveWindow.ScrollColumn = 6
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 1
    wspaste.Cells.Select
    Selection.ColumnWidth = 9.71
    Selection.ColumnWidth = 13.71
    wspaste.Range("D14").Select

End Sub

有建议吗,谢谢

1 个答案:

答案 0 :(得分:0)

短期解决方案

我不确定为什么会出现错误9,该错误是在调用Select时出现“下标超出范围”消息。因此,我现在假设您遇到了另一个错误。

如果是,则似乎是因为如果包含该范围的工作簿不是活动工作簿,则该范围上的Select失败。当您从其他工作簿中运行代码时,您第二次打开“ bdncasemacro.xlsm ”,这意味着它成为活动工作簿(上次打开的工作簿保持活动状态)。因此,由于wspaste bdncasemacro.xlsm 中,因此选择单元格没有问题。但是,当您从 bdncasemacro.xlsm 中执行代码时,您仅打开另一个工作簿,使其成为活动工作簿,然后创建wspaste使用ThisWorkbook。因此,wspaste是工作簿上未激活的工作表。这肯定会导致wspaste.Cells.Select失败。

一种快速的解决方案是通过在打开第一个工作簿后 插入此语句来强制 bdncasemacro.xlsm 成为活动工作簿。 :

ThisWorkbook.Activate

从另一工作簿运行代码时,从本质上讲,此行将替代第二个Workbooks.Open。您还必须确保包含单元格的工作表是活动的;否则,即使工作簿处于活动状态,Select也会失败。为此,必须在第一个wspaste.Cells.Select之前添加以下行:

wspaste.Activate

长期解决方案

话虽如此,我相信一个更永久的解决方案是完全避免.Select.Activate。宏记录器之所以添加该代码,是因为它机械地记录了您执行的每个动作,但是,在代码中,通常不需要选择单元格对其执行操作,如以下链接中“ 尽可能避免选择并激活 ”(您必须向下滚动到长页的中间才能看到该部分)...

http://www.excelcampus.com/vba/select-vs-activate-method/

该代码还具有多个SelectActivate语句,可以互相“覆盖”,也就是说,您可以删除中间的SelectActivate语句并且仅在工作表上完成实际工作之前保留最后的SelectActivate语句。

似乎也不需要ScrollWindow调用来执行所需的操作。在您滚动工作表以执行操作时,宏记录器必须添加了这些调用。

此外,似乎该过程没有在打开工作簿后关闭它们。这可能是您可能在某个时候看到的错误9中的原因。

考虑到所有这些,代码可以简化为:

    Dim wbDownload As Workbook
    Set wbDownload = Workbooks.Open("C:\Users\attkeej1\Desktop\testing\jobsummarydownloadmacro.xls")

    Set wscopy = wbDownload.Worksheets("Report")
    Set wspaste = ThisWorkbook.Worksheets("Raw")

    wscopy.Range("A1:AZ1000").Copy wspaste.Range("A1")
    With wspaste.Cells
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
    End With
    wspaste.Rows("1:5").Delete Shift:=xlShiftUp

    wspaste.Cells.ColumnWidth = 8.29

    With wspaste.Range("C:C,E:F,H:H,J:M,O:R,T:T,V:W,Y:AA,AC:AD,AF:AH,AJ:AJ,AL:AM,AO:AO,AQ:AR,AT:AU,AW:AY")
        .Delete Shift:=xlToLeft
    End With

    With wspaste.Cells
        .ColumnWidth = 9.71
        .ColumnWidth = 13.71
    End With

    wbDownload.Close

希望我不会过多删除您的原始代码。如果是这样,我表示歉意,但希望以上内容对缩短代码有帮助。