[英]Excel VBA: code to copy cells from sheet to new one
I have the below codes to create sheets and rename it from specific list in the master sheet I need a help to add a code to copy some cells from another sheet ( pay slip ) to be pest in each of these new sheets 我有以下代码来创建工作表并从主工作表中的特定列表重命名我需要帮助,以添加代码以复制另一工作表中的某些单元格(工资单)以在每个新工作表中成为有害对象
Sub CreateSheetsFromAList()
Dim MyCell As Range, MyRange As Range
Set MyRange = Sheets("MASTER").Range("E9:E27")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
For Each MyCell In MyRange
Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
Next MyCell
End Sub
Which Cells from "Pay Slip" should be copied? 应该复制“工资单”中的哪些单元格? For example, I will assume that you want to copy cell (1,1) & cells(2,1) from "Pay Slip" to be copied to new cell.
例如,假设您要复制“付款单”中的单元格(1,1)和单元格(2,1),以将其复制到新单元格中。
Modify your code as explained below. 如下所述修改您的代码。
For Each MyCell In MyRange
Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
''''Include the Copy steps from PaySlip sheet below this
Sheets(Sheets.Count).cells(1,1) = Sheets("Pay Slip").cells(1,1)
Sheets(Sheets.Count).cells(2,1) = Sheets("Pay Slip").cells(2,1)
Next MyCell
You could use .Copy & .PasteSpecial method of Range-object.. Here a simple example - 您可以使用Range对象的.Copy和.PasteSpecial方法。这里有一个简单的示例-
Sub copySomeRange(r As Range, target As Range)
r.Copy
target.PasteSpecial Paste:=xlPasteAll
End Sub
Sub callMeExample()
Call copySomeRange(Range("A1:A5"), Range("B1"))
End Sub
The first Sub() does the copying so U can use that part of the code as is. 第一个Sub()进行复制,因此U可以按原样使用该部分代码。 Just add your logic to what to copy.
只需在复制内容中添加逻辑即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.