Excel 基于其他单元格值复制/粘贴n次
我用这个撞到墙上了。尽管像这样的帖子非常相似,或者在Kioskea上,我只是无法将我头脑中的点连接在过滤细胞和基于公式结果的复制之间,而这正是实现这一点所需要的。以下是数据表-简化-我正在使用:Excel 基于其他单元格值复制/粘贴n次,excel,vba,loops,iteration,copy-paste,Excel,Vba,Loops,Iteration,Copy Paste,我用这个撞到墙上了。尽管像这样的帖子非常相似,或者在Kioskea上,我只是无法将我头脑中的点连接在过滤细胞和基于公式结果的复制之间,而这正是实现这一点所需要的。以下是数据表-简化-我正在使用: 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 Pur
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列将与之匹配,无需执行任何操作
- 如果状态为“进行中”,则假定G列中的数字是待扫描的框数(简单地说是原始框数减去实际框数)
- 如果状态为“完成”,则假定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
一旦我找到了真正在做繁重工作的代码,我就不知道哪一个是最好的。如上所述,像这样的帖子帮助我来到这里。我慢慢地开始理解我在上面的发现。这个人似乎在做If/Then工作,但我不明白它是如何复制或粘贴的
我感谢所有的帮助。即使你能给我指出一个有助于解释这一点的资源(除了亚马逊的书籍:),这也会是一个很大的帮助 让我们看看这是否能让你走上正轨。您的代码对于不太了解的人来说非常好,因此您可能需要快速学习:) 我不明白您为什么要使用
.Offset(0,3)
(在您的解释中似乎没有提到)以及为什么要与SectorType
进行比较,后者是您提供的代码中未定义的变量我将假设这些是不必要的,并且是无意中从其他示例中复制的(如果我错了,请告诉我)
我没有测试过,但我会更改此任务:
Set srcrange = Range(wb.ws2.Cells("A2:H61"))
对于这一点,如果没有其他原因的话,它更直接一点。我还将此范围更改为仅参考H列,因为H列是逻辑的中心列(注意:我们始终可以使用Offset
和/或Resize
方法访问其他单元格)
逻辑的核心在这个块中,请注意删除了行。偏移量(9,3)=SectorType
。我还将使用选择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列中的数字迭代粘贴函数。
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