[英]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.