简体   繁体   English

根据单个单元格值将项目复制/粘贴到另一个工作表上

[英]Copy/Paste Item onto another sheet based on a single cell value

I am trying to create a macro that loops a copy/paste of items in (Column A) of my "Backend" worksheet based on a single cell's value (B2) onto my "Backend 2" Worksheet.我正在尝试创建一个宏,该宏根据单个单元格的值 (B2) 将“后端”工作表(A 列)中的项目的副本/粘贴循环到我的“后端 2”工作表上。 To give some context, I have forecast data on building floors and trying to reformat my spreadsheet so that Tableau will read the dates as "dimensions."为了提供一些背景信息,我有关于建筑楼层的预测数据,并尝试重新格式化我的电子表格,以便 Tableau 将日期读取为“尺寸”。 In order to accomplish this I would need a macro that would copy/paste my 83 floors of data 15 times for the 15 months in my forecast.为了实现这一点,我需要一个宏来在我的预测中在 15 个月内复制/粘贴我的 83 层数据 15 次。 I would also like the reference cell (B2) so that I can add months to the forecast if needed.我还想要参考单元格 (B2),以便我可以在需要时将月份添加到预测中。 Thanks!谢谢!

Copy From:复制自:
在此处输入图片说明

Paste to:粘贴到:
在此处输入图片说明


The current answer allows me to copy one value type "floor," but I was wondering if I could run a macro that would copy/paste an entire row based on the copy amount.当前的答案允许我复制一个值类型“floor”,但我想知道我是否可以运行一个宏来根据复制量复制/粘贴整行。 Please refer to the example below.请参考下面的例子。 I have 3 unique teams on sheet 1 that I want copied four times based on the cell L2 on sheet 2.我在工作表 1 上有 3 个独特的团队,我想根据工作表 2 上的单元格 L2 将它们复制四次。

Before (Sheet 1)之前(表 1)在此处输入图片说明

After (Sheet 2)之后(表 2)在此处输入图片说明

Based on my test ,worded code resembles the following.根据我的测试,措辞代码类似于以下内容。 Modify the soucreSheet and targetWorksheet to yours :将 soucreSheet 和 targetWorksheet 修改为您的

Sub Test11()

Dim rowCount As Long
Dim sourceSheet As Worksheet
Dim targetWorksheet As Worksheet
Dim copyTimes As Integer

  Set sourceSheet = Worksheets("Sheet11")
  Set targetWorksheet = Worksheets("Sheet12")
    rowCount = targetWorksheet.Cells(Rows.Count, 1).End(xlUp).row

    copyTimes = CInt(sourceSheet.Cells(2, 2).Value)

    For i = 2 To sourceSheet.UsedRange.Rows.Count
        MsgBox sourceSheet.Cells(i, 1).Value
        sourceSheet.Cells(i, 1).Copy
        For j = 1 To copyTimes
            targetWorksheet.Activate
            targetWorksheet.Cells(rowCount + 1, 1).Select
            targetWorksheet.Paste
            rowCount = rowCount + 1
        Next

            sourceSheet.Activate
    Next

    Application.CutCopyMode = False
End Sub

This should work for you:这应该适合你:

Sub floors()

    Dim ws1 As Worksheet
    Set ws1 = sheets("Bcknd")

    Dim ws2 As Worksheet

    If Not sheetExists("Migration Plan Data Extract") Then
        sheets.Add After:=ws1
        Set ws2 = sheets(ws1.index + 1)
        ws2.name = "Migration Plan Data Extract"
    Else
        Set ws2 = sheets("Migration Plan Data Extract")
    End If

    If Len(ws1.Range("B2").Value2) > 0 And IsNumeric(ws1.Range("B2").Value2) Then
        ws2.Range("A1").Value2 = ws1.Range("A1").Value2

        Dim vals As Variant
        vals = ws1.Range("A2:A" & ws1.Range("A" & Rows.Count).End(xlUp).Row).Value

        Dim i As Long
        Dim j As Long: j = 1

        For i = 1 To ws1.Range("B2").Value2 * UBound(vals)
            ws2.Range("A" & i + 1).Value2 = vals(j, 1)

            If i Mod ws1.Range("B2") = 0 Then
                j = j + 1
            End If
        Next i

    End If

End Sub

Alright, this should copy down the entire row :)好的,这应该复制整行:)

Sub floors2()

    Dim ws1 As Worksheet
    Set ws1 = sheets("Bcknd")

    If Len(ws1.Range("L2")) > 0 And IsNumeric(ws1.Range("L2").Value2) Then

        Dim ws2 As Worksheet

        If Not sheetExists("Migration Plan Data Extract") Then
            sheets.Add After:=ws1
            Set ws2 = sheets(ws1.index + 1)
            ws2.name = "Migration Plan Data Extract"
        Else
            Set ws2 = sheets("Migration Plan Data Extract")
        End If

        ws1.Range("A1:J1").copy Destination:=ws2.Range("A1:J1")

        Dim lastRow As Long
        lastRow = ws1.Range("A" & rows.count).End(xlUp).row

        Dim rng As Range
        Set rng = ws1.Range("A2:J" & lastRow)

        Dim currentRow As Long: currentRow = 2

        Dim i As Long
        Dim j As Long
        For i = 1 To rng.rows.count
            For j = 1 To ws1.Range("L2").Value2
                rng.rows(i).copy Destination:=ws2.Range("A" & currentRow)
                currentRow = currentRow + 1
            Next j
        Next i

    End If

End Sub

This sub is used by both to see if the sheet "Migration Plan Data Extract" already exists两者都使用此子项来查看“迁移计划数据提取”表是否已存在

Function sheetExists(sheetToFind As String) As Boolean

    sheetExists = False

    Dim sheet As Worksheet
    For Each sheet In Worksheets
        If sheetToFind = sheet.name Then
            sheetExists = True
            Exit Function
        End If
    Next sheet

End Function

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

相关问题 根据超链接单击将单元格值复制并粘贴到另一个工作表 - Copy and paste cell value to another sheet based on hyperlink click 从另一张纸上的单元格值复制同一张纸中范围的一部分的粘贴范围 - Copy Range From One Sheet Paste Part of Range In Same Sheet Based On Cell Value On Another Sheet 基于一个单元格值复制行并引用另一个单元格值并粘贴到新工作表上 - Copy rows based on one cell value and in reference to another cell value and paste on a new sheet 根据单元格值将数据从一张表复制并粘贴到另一张表 - Copy and paste data from one sheet to another sheet(s) based on cell value 如果单元格包含这个或那个粘贴到另一张纸上 - If Cell Contains This or That Paste onto Another Sheet 根据单元格中的值将行复制到另一张工作表 - Copy row to another sheet based on value in a cell VBA 根据日期将工作表 1 中的单元格值复制/粘贴到工作表 2 的宏 - VBA Macro to copy/paste cell value from sheet 1 to 2 based on date 根据单元格值复制行并粘贴到新工作表上 - Copy rows based on cell value and paste on a new sheet 将一个单一值多次复制粘贴到另一张纸上 - copy paste one single value multiple times to another sheet 在每个工作表匹配的单元格上复制一个单元格并将其粘贴到另一个VBA - Copy a cell on one sheet and paste it on another VBA based on cell from each sheet matching
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM