根据其他单元格值复制/粘贴n次

我已经跑到墙上了。 尽pipeSO这样的post与Kioskea上的post非常相似,但是我根本无法在过滤单元格和复制的基础上将我的脑海中的点连接起来。 这里是数据表 – 简化 – 我正在与:

ABCDEFGH 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列可以有三种含义:

  • 如果状态为“未激活”,则列G和H匹配,不需要执行任何操作
  • 如果状态为进行中,则假定列G中的数字是等待扫描的盒子的数量(简单地,原来的减去实际的)
  • 如果状态是完整的,则假定列G中的数字是不需要扫描的盒子的数量,并且被清除

我的代码(如下所示)应该做的是遍历范围内的每一行(A2:H61)。 如果状态为“未激活”,则该行可以被忽略,并移至下一行。 如果状态为“正在进行”或“完成”,则无论是“正在阅读”哪一行,macros都需要复制单元格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 

一旦我find实际上正在做这个咕-工作的代码,我就不知道哪一个是最好的。 如上所述,像这样的post有助于让我在这里。 我慢慢开始理解我在Excel先生上发现的东西。 这个人似乎在做If / Then工作,但我不明白它是如何复制或粘贴的。

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

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

我很困惑你为什么使用.Offset(0, 3) (在你的解释中似乎没有提到任何地方),还有你为什么要在你提供的代码中与SectorType进行比较,这是一个未定义的variables。 我会认为这些是不必要的,并且不经意地从其他例子中复制 (如果我错了,请让我知道)。

我没有testing过,但我会改变这个任务:

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

对此,如果没有其他原因,而不是多一点直接。 我也正在改变这个范围只是指H列,因为这是你的逻辑居中的列(注意:我们总是可以使用Offset和/或Resize方法访问其他单元格)。

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

你的逻辑肉是在这个块中,注意删除Row.Offset(9, 3) = SectorType 。 我也将使用Select Case而不是If/Then 。 当有多个或两个条件要testing时,我发现这些文件更容易阅读/理解:

 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