将数据从一列复制到范围中的下一个空列

时间:2018-09-20 10:57:42

标签: excel vba worksheet

我有一个宏,它可以成功地从一列复制数据,然后将其粘贴到特定范围内的下一个空列中,然后删除另一范围内的单元格内容。

它仅在打开的工作表上起作用。

我试图添加一个循环以循环浏览工作簿中的一些工作表,并且在宏运行时,它仍然仅在打开的工作表上执行过程。

有人可以帮助我使其在数组中列出的每个工作表上运行吗?

Sub CopyPasteEoY_OtherSubjects()
'
' CopyPaste Macro
'
Dim Sh As Variant                'Sh = All Foundation Subjects sheets
Dim targetRng As Excel.Range
Dim destRng As Excel.Range
Dim objsRng As Excel.Range
Set targetRng = Range("AU4:AU103")
Set destRng = Range("V4:AB103")
Set objsRng = Range("AC4:AT103")

Application.ScreenUpdating = False

For Each Sh In Array("Art", "Computing", "Design Technology", "Geography", "History", "MFL", "Music", "PE", "RE", "Science")
        With destRng
            Set destRng = .Cells(.Columns.Count).End(Excel.xlToLeft).Offset(0, 1).Resize(targetRng.Rows.Count, targetRng.Columns.Count)
                destRng.Value = targetRng.Value
                    With objsRng
                    .ClearContents
                    End With
        End With
Next Sh

Application.ScreenUpdating = True

End Sub

非常感谢。

1 个答案:

答案 0 :(得分:0)

尝试一下。

Sub CopyPasteEoY_OtherSubjects()
    '
    ' CopyPaste Macro
    '
    Dim Sh As Variant                'Sh = All Foundation Subjects sheets
    Dim targetRng As Excel.Range
    Dim destRng As Excel.Range
    Dim objsRng As Excel.Range

    Application.ScreenUpdating = False

    'Your concept is nothing wrong, just remember to specify the certain sheet you are dealing with
    For Each Sh In Array("Art", "Computing", "Design Technology", "Geography", "History", "MFL", "Music", "PE", "RE", "Science")

        With Worksheets(Sh)
            Set targetRng = .Range("AU4:AU103")
            Set destRng = .Range("V4:AB103")
            Set objsRng = .Range("AC4:AT103")
        End With

        With destRng
            'This part is weird, you should avoid changing the main object itself in the with statement
            Set destRng = .Cells(.Columns.Count).End(Excel.xlToLeft).Offset(0, 1).Resize(targetRng.Rows.Count, targetRng.Columns.Count)
            'destRng.Address and .Address are different now, be aware
            destRng.Value = targetRng.Value
            objsRng.ClearContents
        End With

    Next Sh

    Application.ScreenUpdating = True

End Sub

如果我误解了什么,请告诉我。

Set rng = Range("A1")实际上是Set rng = ActiveSheet.Range("A1")。因此,当您需要对另一张纸做一些事情时,必须通过

将其限制在该纸上
Set rng = Worksheets(B).Range("A1")

或者您可以在设置rng对象之前激活工作表

Worksheets(B).Activate
Set rng = Range("A1")

但是,由于Activate是一项浪费资源的工作,因此这不是一种有效的方法。

请注意,即使您激活了其他工作表,范围对象在重新设置之前仍将保持相同的父对象。检查下面的代码。

Worksheets(A).Activate
Set rng = Range("A1")        'now it belongs to Worksheets(A)
Worksheets(B).Activate
Debug.Print rng.Parent.Name  'Worksheet(A) will appear
Set rng = Range("A1")        'now it belongs to Worksheets(B)
Debug.Print rng.Parent.Name  'it is Worksheet(B) now

With部分中,您可以根据需要编辑主对象的任何属性,但不能将其设置为另一个对象。检查代码。

Dim rng As Range
Set rng = Range("A1")
With rng
    Set rng = Range("B100")
    Debug.Print rng.Address     'gives $B$100
    Debug.Print .Address        'gives the original one, $A$1
End With

最好不要这样做,这会令人困惑。