为什么VBA宏会在活动小区更改时停止?

时间:2016-06-23 14:57:18

标签: excel-vba vba excel

我离开写作VBA已有7年了,今天我被要求做一件简单的事情而且不起作用

我正在尝试将每周52个摘要的内容整理成一张:

所以我可以转到第一张表 - 复制我需要的数据 - 转到摘要表 - 粘贴数据 - 然后停止...如果我只是更改单元格的值,则会相同 - 它会更改值然后停下来。

我错过了安全设置或什么?

这是VBA

Sub Macro3()
'
' Macro3 Macro
'

' Take cell contents of active sheet and paste into summary sheet

Sheets("we 03 Jan").Select
Do
    shtName = ActiveSheet.Name
    Range("A10:U39").Select
    Application.CutCopyMode = False
    Selection.Copy

    Sheets("Full Year").Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.Value = shtName
    ' stops here
    ActiveCell.Offset(1, 0).Range("A1").Select
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
     False, Transpose:=False
    ' stops here if previous line causing stop is removed 
    Sheets(shtName).Select
    If ActiveSheet.Index = Worksheets.Count Then
        Worksheets(1).Select
        Exit Sub
        Else
        ActiveSheet.Next.Select
        End If
Loop
End Sub

1 个答案:

答案 0 :(得分:1)

这有用吗?

Sub Macro3()
    Dim n                     As Long
    Dim ws                    As Worksheet

    On Error GoTo clean_up

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Sheets("we 03 Jan").Select
    For n = ActiveSheet.Index To Sheets.Count
        Set ws = Sheets(n)
        ws.Range("A10:U39").Copy

        With Sheets("Full Year").Cells(Rows.Count, "A").End(xlUp).Offset(1)
            .Value = ws.Name
            .Offset(1, 0).PasteSpecial Paste:=xlValues, Operation:=xlNone, _
                                       SkipBlanks:=False, Transpose:=False
        End With
    Next n

clean_up:
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

End Sub