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

Copying data from one column to next empty column in a range

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

它只适用于打开的工作表。

我尝试添加一个循环来循环浏览工作簿中的工作表选择,而宏 运行s 仍然只在打开的工作表上执行该过程。

任何人都可以帮助我在数组中列出的每张工作表上做到 运行 吗?

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

非常感谢。

试一试。

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")。因此,当您需要对另一个 sheet 做某事时,您必须通过

将其限制为 sheet
Set rng = Worksheets(B).Range("A1")

或者您可以在设置 rng 对象之前激活 sheet

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

然而,这不是一种有效的方法,因为 Activate 是一项资源浪费工作。

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

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 $B0
    Debug.Print .Address        'gives the original one, $A
End With

这会让人感到困惑,所以最好不要这样做。