简体   繁体   English

Excel VBA:将表格中的单元格复制到新单元格的代码

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

相关问题 自动将单元格复制到Excel VBA中的新工作表 - Auto Copy Cells to New Sheet in Excel VBA 将单元格中的单元格复制到多张Excel - VBA中 - copy cells from one sheet into multiple sheets Excel - VBA Excel-将单元格+宏+ VBA从一张纸复制到另一张纸? - Excel - copy cells+macro+VBA from one sheet to another? 将非空白单元格从一张纸复制到另一张纸的 VBA 代码 - VBA Code to Copy Non Blank Cells From one Sheet to Another VBA代码将行从一个Excel工作表复制到另一个 - VBA code to copy rows from one excel sheet to another VBA 代码从上一个工作表复制一系列单元格并导入到新工作表 - VBA code to copy a range of cells from previous sheet and import to a new sheet Excel将VBA宏复制单元格循环到新工作表 - Excel loop VBA Macro copy cells to new sheet Excel VBA按钮(将单元格复制到另一张工作表中的新行) - Excel VBA button (copy cells to a new row in another sheet) Excel VBA代码,用于在一个工作簿中选择包含特定文本字符串的单元格,然后将这些单元格复制并粘贴到新工作簿中 - Excel VBA code to select cells that contain a certain text string in one workbook, and then copy and paste these cells into a new workbook VBA代码 - 根据匹配条件将单元格中的单元格复制到Excel中的另一个工作表 - VBA Code - Copying cells from one sheet to another sheet in excel depending on matching condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM