【问题标题】:Copy/Paste n Times Based on Other Cell Values根据其他单元格值复制/粘贴 n 次
【发布时间】:2014-07-03 04:01:50
【问题描述】:

我遇到了这个问题。尽管像this one 这样的SO 上的帖子非常 相似或Kioskea 上的this one,但我只是无法在过滤单元格和基于公式结果进行复制之间连接点工作。这是数据表 - 简化 - 我正在使用:

     A    B   C        D     E      F    G    H
 R1 Name Num Status   #Orig #InPro #Act #Rem #RemStatus
 R2 ABC  032 Complete 22    0      11   11   Purged
 R3 LMN  035 In Prog  25    21     4    21   Pending Scan
 R4 XYZ  039 Not Act  16    16     0    16   Not Active

这描述了纸质文件盒的状态及其处置:

  • D 列是计划扫描的盒子数
  • E 列是要扫描的框数
  • F 列是实际扫描的框数

根据状态,G 列和 H 列可以具有三种含义:

  • 如果状态为 Not Active,则 G 列和 H 列匹配,无需执行任何操作
  • 如果状态为 In Progress,则假定 G 列中的数字是待扫描的框数(原始减去实际)
  • 如果状态为“完成”,则假定 G 列中的数字是不需要扫描并被清除的盒子的数量

我的代码(如下所示)应该做的是遍历范围内的每一行 (A2:H61)。如果状态为未激活,则可以忽略该行并移至下一行。如果状态为“进行中”或“完成”,则宏在“正在读取”的任何行中都需要复制单元格 A、B 和 H 并将其(列)“G”多次粘贴到另一个工作表中 - 在同一个工作簿中- 从下一个可用行开始。 深呼吸

我知道。这也伤了我的大脑。这是我到目前为止的代码:

Sub TEST_Copy_Process()

Dim srcrange As Range
Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet

Set wb = ActiveWorkbook
Set ws1 = Worksheets("SIS Agregate")
Set ws2 = Worksheets("Center Detail")
Set srcrange = Range(wb.ws2.Cells("A2:H61"))


For Each Row In srcrange.Rows

If Row = "Not Active" And Row.Offset(0, 3) = SectorType Then
Continue
ElseIf Row = "In Progress" And Row.Offset(0, 3) = SectorType Then

ElseIf Row = "Complete" And Row.Offset(0, 3) = SectorType Then

End If

    Set LastCell = wb.ws1.Cells(wb.ws1.Rows.Count, "A").End(xlUp)
    LastCellRowNumber = LastCell.Row + 1

Next Row

End Sub

一旦我接触到实际执行繁重工作的代码,我就无法确定哪个是最好的。如上所述,this 之类的帖子帮助我来到了这里。我慢慢开始理解我在Mr. Excel 上发现的东西。这个人似乎在做 If/Then 工作,但我不明白它是如何复制或粘贴的。

我感谢任何和所有的帮助。即使您可以向我指出一个有助于解释这一点的资源(除了亚马逊上的书籍:]),这将是一个很大的帮助!

【问题讨论】:

  • 我不清楚你到底在问什么。

标签: excel vba loops iteration copy-paste


【解决方案1】:

让我们看看这是否能让你走上正轨。对于不太了解的人来说,你的代码看起来非常好,所以也许你是一个快速学习的人:)

我很困惑你为什么使用.Offset(0, 3)(在你的解释中似乎没有提到),以及你为什么要与SectorType 进行比较,SectorType 是你提供的代码中未定义的变量。 我假设这些是不必要的,并且是无意中从其他示例中复制而来的(如果我弄错了,请告诉我)。

我还没有测试过,但我会改变这个分配:

Set srcrange = Range(wb.ws2.Cells("A2:H61"))

对此,如果没有其他原因,只是更直接一点。我还将此范围更改为参考 H 列,因为这是您的逻辑居中的列(注意:我们始终可以使用 Offset 和/或 @987654325 访问其他单元格@ 方法)。

Set srcrange = wb.ws2.Range("H2:H61")

你的逻辑的核心在这个块中,注意删除Row.Offset(9, 3) = SectorType。我还将使用Select Case 而不是If/Then。当要测试的条件超过一两个时,我发现这些更容易阅读/理解:

For Each Row In srcrange.Cells  '## In this case, Cells/Rows is the same, but I use Cells as I find it less ambiguous
    Select Case Row.Value
        Case "Not Active"
        '## If the status is Not Active, Column G and H match it, and nothing needs to be done
            'Do nothing

        Case "In Progress", "Complete"
        '## If the status is In Progress or Complete, ... copy cells A, B, and H _
        '    and paste it (column)"G" number of times in another worksheet - _
        '    within the same workbook - starting in the next available row.

        '# Get the next empty cell in column A of the ws1
        '  I modified this to use Offset(1, 0), to return the cell BENEATH
        '  the last cell.
            Set LastCell = wb.ws1.Cells(wb.ws1.Rows.Count, "A").End(xlUp).Offset(1)

        '## copy the values from columns A, B, H to ws1
        '## Column A goes in column A
            LastCell.Value = Row.Offset(0, -7).Value 
        '## Column B goes in column B
            LastCell.Offset(0, 1).Value = Row.Offset(0, -6).Value 
        '## Column H goes in column C (because you did not specify)
            LastCell.Offset(0, 2).Value = Row.Value 
    End Select
Next Row

【讨论】:

  • 这些解释使我能够弄清楚什么代码在做什么。谢谢你的指导。你是对的,当我把它拼凑在一起时,一些不必要的代码是从各种来源粘贴的。我做了一些简单的修改来移动列。我的下一个目标是根据 G 列中的数字迭代粘贴函数。
猜你喜欢
  • 2014-12-27
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 2013-12-11
  • 2023-02-14
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多