简体   繁体   English

如何复制工作簿中一个工作表/选项卡中使用的代码以在同一工作簿中的另一个工作表/选项卡中创建报告

[英]How to copy the code used in one sheet/tab in a workbook to create a report in another sheet/tab within the same workbook

Let's say, there is a report that has already been created in a sheet in a work book using macros. 假设有一个使用宏在工作簿中的工作表中创建的报告。 I want to create another report within the same workbook in another sheet(tab) with the exact same code in VBA which was used to create the report that has already been created. 我想在另一个工作表(选项卡)的同一工作簿中创建另一个报表,并使用VBA中完全相同的代码来创建已经创建的报表。 How do i copy and make those codes work in a different sheet to create a similar report? 如何复制这些代码并使它们在另一张纸上工作以创建类似的报告?

Any code in the Worksheet itself should use Me to refer to itself - this way, it will work when copied/duplicated 工作表中的任何代码本身都应使用Me来引用自身-这样,在复制/复制时它将起作用

Any other code should take the Worksheet to act on as a Argument, or as Module Level Object: 任何其他代码都应采用工作表作为参数或模块级对象:

Option Explicit

Sub CreateReport(TargetSheet As Worksheet)
    TargetSheet.Cells(1,1).Value = "Hello"
End Sub

or 要么

Option Explicit
Public TargetSheet AS Worksheet

Sub CreateReport()
    If TargetSheet Is Nothing Then Exit Sub 'In case the object has not been set
    TargetSheet.Cells(1,1).Value = "Hello"
End Sub

Then you can duplicate a "master" template sheet, and run the macros to target it 然后,您可以复制一个“主”模板表,并运行宏以对其进行定位

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将一个选项卡从工作表复制到另一工作簿 - Copy one tab from a sheet to another workbook 从一张纸上复制数据并将其从另一张相同的工作簿中过去 - copy data from one sheet and past it on another sheet of same workbook 如何将数据复制到同一工作簿的另一张纸和另一工作簿? - How to copy data to another sheet of the same workbook and to a different workbook? VBA将数据从一个工作簿表复制到另一工作簿表 - VBA copy data from one workbook sheet to another workbook sheet 将所有使用的单元格从一个工作簿表复制到另一个现有工作簿表 - Copy all used cells from one workbook sheet to another existing workbook sheet 将工作表复制到另一个工作簿 - Copy sheet to another workbook 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python? 将一个工作簿中的工作表中的VBA代码复制到另一个工作簿? - Copy VBA code from a Sheet in one workbook to another? 如何将一个文件夹中多个 excel 工作簿中除工作表 1 和 2 之外的所有工作表复制到另一个工作簿中 - How to Copy every sheet except sheet 1 and 2 on multiple excel workbook in one folder into another workbook 将列从一张纸复制到另一张工作簿 - Copy columns from one sheet to another workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM