重复宏代码 [英] Repeating macro code

查看:63
本文介绍了重复宏代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我录制了这个宏:

Sheets("Sheet1").Select
Range("D4:E4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("ALB3").Select
Range("C1").Select
ActiveSheet.Paste

我想做一个循环以重复该过程.选择时从范围D4:E4到D200:E200

I want to make a loop to repeat the process. From range D4:E4 to D200:E200 when do select

将其粘贴在从ALB3到ALB196的各个工作表名称上.

To paste that on respective sheet name from ALB3 to ALB196.

我在工作表1中的数据.

My data in sheet 1.

a列是工作表名称,d4和e4列是我要粘贴到已创建的每张工作表上的数据.

Column a is sheets name, column d4 and e4, is the data that I want to paste on every sheet already created.

推荐答案

如果您尝试将范围从一张纸复制到另一张纸,则不需要循环,也不需要选择.您可以使用不使用剪贴板的复制语法.

If you're trying to copy a range from one sheet to another, you don't need a loop and you don't need to select. You can use copy syntax that doesn't use your clipboard.

尝试一下:

Sub CopyRangeToAnotherSheet()
    Dim source As Worksheet
    Dim target As Worksheet

    Set source = ActiveWorkbook.Sheets("Sheet1")
    Set target = ActiveWorkbook.Sheets("Sheet2")

    source.Range("D4:E200").Copy target.Range("ALB3")

End Sub

要将源范围复制到工作簿中除源工作表之外的所有表中,请尝试以下操作:

To copy the source range to all sheets in the workbook except the source worksheet, try this:

Sub CopyToAllSheets()
    Dim ws As Worksheet

    For Each ws In Worksheets
        CopyRangeToAnotherSheet (ws.Name)
    Next
End Sub

Sub CopyRangeToAnotherSheet(targetName As String)
    Dim source As Worksheet
    Dim target As Worksheet

    Set source = ActiveWorkbook.Sheets("Sheet1")
    Set target = ActiveWorkbook.Sheets(targetName)

    If target.Name <> source.Name Then
        source.Range("D4:E200").Copy target.Range("ALB3")
    End If
End Sub

这篇关于重复宏代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆