【问题标题】:How to loop a dynamic range and copy select information within that range to another sheet如何循环动态范围并将该范围内的选择信息复制到另一个工作表
【发布时间】:2015-08-10 23:10:02
【问题描述】:

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

不使用单元格引用(因为每次运行时日期范围都会改变)我现在需要获取用户 ID、姓名、总小时数、总休息时间、加班时间 1 和加班时间 2,并将此数据复制到工作表 2 .

关于如何构建 VBA 脚本以搜索 B 行直到找到空白的任何建议,当找到空白时,复制该行和该行的 J、K、L、M 列中的值以上复制值 C - 现在将这些值粘贴到工作表 2 上。 - 继续此过程,直到找到两个连续的空白或数据末尾...

即使您可以提出与我在上面假设的逻辑不同的方法来解决此问题,也将不胜感激。如果您有兴趣,我可以分享整个代码并向您展示我开始使用的数据。

提前谢谢你, J

【问题讨论】:

  • copy the values from the cell above,您的意思是复制数据块中的最后一行空白,是吗?像上面的截图一样,复制C15J15:M15?
  • 请添加您拥有的代码并告诉我们您遇到的问题
  • @BK201 不行,所以复制图片中的C15和J16:M16
  • @threeFourOneSixOneThree 我可以复制我用来获取上面屏幕截图的代码,但这不会有帮助。我正在寻找处理宏的最后一部分的最佳逻辑。
  • 我喜欢你处理这个问题的方式,要求逻辑而不是确切的代码。我给你一个,非常好。使用AutoFilter 并过滤Blanks。您最终将在 J16:M16 中获得每个 Username 的总统计信息。找到一种通过循环将Username 与统计信息挂钩/关联的方法。那应该可以完美地工作。 :)

标签: vba excel dynamic range


【解决方案1】:

如前所述,这是我的方法。所有详细信息都在代码的 cmets 中,因此请务必阅读它们。

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

截图:

设置:

结果:

此处的演示视频:

Using Filters and Visible Cells

如果这有帮助,请告诉我们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多