如何循环dynamic范围并将该范围内的select信息复制到另一个表单

我已经创build了一个大约160行的VBA脚本,它生成了您在下面看到的报告。

如果不使用单元格引用(因为每次运行时都会更改date范围),现在我需要将用户标识,名称,总小时数,总分钟数,加class时间1和加class时间2复制到表单2中。

任何关于如何构buildVBA脚本来search行B的build议,直到find空白为止,find空白时,复制该行J,K,L,M行上的值以及复制值上面的行C – 现在将这些值粘贴在图表2上。 – 继续此过程,直到find两个连续的空白或数据的结尾…

即使你可以build议一个不同的方式来解决这个问题,而不是我上面所说的逻辑,这将是不胜感激。 如果您有兴趣,我可以分享整个代码,并向您展示我开始使用的数据。

预先感谢你,J

例

正如所讨论的,这是我的方法。 所有的细节都在代码的注释中,所以确保你阅读它们。

 Sub GetUserNameTotals() Dim ShTarget As Worksheet: Set ShTarget = ThisWorkbook.Sheets("Sheet1") Dim ShPaste As Worksheet: Set ShPaste = ThisWorkbook.Sheets("Sheet2") Dim RngTarget As Range: Set RngTarget = ShTarget.UsedRange Dim RngTargetVisible As Range, CellRef As Range, ColRef As Range, RngNames As Range Dim ColIDIndex As Long: ColIDIndex = Application.Match("ID", RngTarget.Rows(1), 0) Dim LRow As Long: LRow = RngTarget.SpecialCells(xlCellTypeLastCell).Row 'Turn off AutoFilter to avoid errors. ShTarget.AutoFilterMode = False 'Logic: Apply filter on the UserName column, selecting blanks. We then get two essential ranges. 'RngTargetVisible is the visible range of stats. ColRef is the visible first column of stats. With RngTarget .AutoFilter Field:=ColIDIndex, Criteria1:="=", Operator:=xlFilterValues, VisibleDropDown:=True Set RngTargetVisible = .Range("J2:M" & LRow).SpecialCells(xlCellTypeVisible) Set ColRef = .Range("J2:J" & LRow).SpecialCells(xlCellTypeVisible) End With 'Logic: For each cell in the first column of stats, let's get its offset one cell above 'and 7 cells to the left. This method is not necessary. Simply assigning ColRef to Column C's 'visible cells and changing below to CellRef.Offset(-1,0) is alright. I chose this way so it's 'easier to visualize the approach. RngNames is a consolidation of the cells with ranges, which we'll 'copy first before the stats. For Each CellRef In ColRef If RngNames Is Nothing Then Set RngNames = CellRef.Offset(-1, -7) Else Set RngNames = Union(RngNames, CellRef.Offset(-1, -7)) End If Next CellRef 'Copy the names first, then RngTargetVisible, which are the total stats. Copying headers is up 'to you. Of course, modify as necessary. RngNames.Copy ShPaste.Range("A1") RngTargetVisible.Copy ShPaste.Range("B1") End Sub 

截图:

build立:

在这里输入图像说明

结果:

在这里输入图像说明

演示video在这里:

使用filter和可见的单元格

让我们知道这是否有帮助。