首页 > 解决方案 > 在另一个工作表中复制行

问题描述

这是我的问题,我试图弄清楚如何制作一个按钮,该按钮将使用单元格中的数字在另一个工作表上生成单元格来设置重复项的数量。

所以在我的第一张纸上,我使用一个公式来确定所需的测量次数。

我需要按钮在不同的工作表(“第 1 行”和“第 2 行”)上复制预先配置的行(第 6 行)单元格(G9)中的次数加上第一个工作表中的 1(“TI 设置”) .

并且(第 6 行)在不同的工作表(“第 3 行”、“第 5 行”和“第 8 行”)单元格 (F9) 中从第一张工作表减去 1 的次数(“TI 设置”)。

最后(第 6 行)在不同的工作表上(“第 4 行”、“第 6 行”和“第 7 行”)单元格(D9)中的次数从第一个工作表(“TI 设置”)减去 1。

我希望这是足够的信息,我正在慢慢地教自己在 Excel 中做更复杂的事情,但这个让我望而却步。

终于找到开始的地方了。。

    Private Sub CommandButton1_Click()
    End Sub
    Public Sub CopyData()

    ' This routing will copy rows based on the quantity to a new sheet.
    Dim rngSinglecell As Range
    Dim rngQuantityCells As Range
    Dim intCount As Integer

    ' Set this for the range where the Quantity column exists. This works only if there are no empty cells
    Set rngQuantityCells = Range("G9", Range("G9").End(xlDown))

    For Each rngSinglecell In rngQuantityCells

    ' Check if this cell actually contains a number and if the number is greater than 0
    If IsNumeric(rngSinglecell.Value) And rngSinglecell.Value > 0 Then

    ' Copy this row as many rows as .value and 25 columns (because A:Y is 25 columns)
    Sheets("Line 1").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(rngSinglecell.Value, 25).Value = _
        Range(Range("A" & rngSinglecell.Row), Range("Y" & rngSinglecell.Row)).Value

    ' Copy this row as many rows as .value and 25 columns (because A:Y is 25 columns)
    Sheets("Line 2").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(rngSinglecell.Value, 25).Value = _
        Range(Range("A" & rngSinglecell.Row), Range("Y" & rngSinglecell.Row)).Value

        End If
    Next


    End Sub

它在大多数情况下都有效,但我需要弄清楚如何告诉它从正确的工作表中复制单元格。

标签: excelvba

解决方案


推荐阅读