我是VBA的新手,希望有人能帮忙。到目前为止,这就是我所拥有的:
Public Sub Late()
Dim LastRow As Long
Dim MasterLastRow As Integer
Dim NewRange As Range
Dim TrackingCount As Integer
Worksheets("Master").Select
Range("A4").Select
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Set NewRange = Cells(LastRow + 1, 1)
Dim i As Integer
For i = 1 To LastRow
If TrackingCount > 14 Then
'Copy row, col. A:M of "Master" worksheet into Col. A:M of "Late Report" worksheet
End If
Next i说明:对于“主”工作表Col Q中的每个TrackingCount (即> 14),我只需要从A到M列复制这些行,并将它们粘贴到“迟交报告”工作表(从A:3开始),一行接着一行(对于每个TrackingCount > 14的行)。
一旦完成,当跟踪计数介于7到14之间(在第一个报告之后粘贴后期报表电子表格)、第三个报告在2-6之间,最后当最后报告为=< 0时,我需要做同样的事情。
所有的报告都需要逐个粘贴。这是一份每周一次的报告,在报告中,每周的行计数将不同。
发布于 2014-10-04 08:54:38
好吧,让我们看看这是否足以让你开始。
Option Explicit
Public Sub LateReport()
Dim MasterSheet As Worksheet
Dim DestinationSheet As Worksheet
Dim workingCell As Range
Dim workingRange As Range
Dim DestinationRow As Integer
Set MasterSheet = ThisWorkbook.Worksheets("Sheet1") ' You may want to correct the name
Set DestinationSheet = ThisWorkbook.Worksheets("Sheet2") ' And for this as well
Set workingRange = MasterSheet.Range("A4:M4").CurrentRegion ' Adjust this starting point as required
For Each workingCell In workingRange.Columns(1).Cells ' We'll work our way down each cell in the first column
If workingCell.Offset(0, 16).Value > 14 Then ' The Offset method looks at the cell X rows and Y columns away; in our case, 0 rows down and 16 columns across
workingCell.Range(workingCell, workingCell.Offset(0, 20)).Copy ' Copy that row
DestinationSheet.Range("A3").Offset(DestinationRow, 0).PasteSpecial xlPasteValuesAndNumberFormats ' Paste it to our destination
End If
DestinationRow = DestinationRow + 1
Next
Set MasterSheet = Nothing
Set DestinationSheet = Nothing
End Sub几个关键词
Option Explicit是必须的。如果我们没有正确地声明我们的变量,它将确保我们不会出现模糊的错误。
下一个是CurrentRegion属性。它选择我们定义的单元格(在本例中,单元格A4通过M4)周围的所有具有值的单元格。这是查看和处理集合中的一个单元格的一种快速而简单的方法。
下一个任务是修改这段代码,以处理列"Q“中的值介于7到14、2到6之间、小于或等于1的情况(我假设这是您最初问题中的一个错误:您已经说了"0")。
祝好运!
发布于 2014-10-03 19:46:05
没有工作表,我看不出整个问题,但在我看来,您似乎试图复制一定数量的符合要求的行。
一个更有效的编程方法是先
如果你不知道如何编程,我建议录制一个宏。还可以查看VBA中的Excel。
https://stackoverflow.com/questions/26185438
复制相似问题