【问题标题】:Loop through data that are visible when column filtered遍历列过滤时可见的数据
【发布时间】:2021-12-15 23:44:12
【问题描述】:

以下代码是我经过大量研究和努力后编写的,但是我现在发现这没有用,因为我在 worksheetsheet 中有多个过滤器。我不想遍历工作表中的所有数据,我只想遍历过滤时可见的数据。但是,使用我的代码,它会遍历所有行。我一次最多过滤 5 列。

dt = CDate(Sheets("Sheet5").Range("P2").Value) 中,我给出了 P2,但是当我过滤它时它可能不可见,并且我不想检查此单元格中的数据。

Sub FindDuration()

Dim totalDuration As Single
Dim dt As Date
Dim nextDt As Date
Dim maxDt As Date
Dim DateDiff
Dim lr As Long
Dim lrw As Long
Dim lr1 As Long
Dim i As Long

totalDuration = 0
dt = 0
lr1 = ThisWorkbook.Sheets("Sheet5").Range("A" & Rows.Count).End(xlUp).Row

dt = CDate(Sheets("Sheet5").Range("P2").Value)
maxDt = dt + 1
  
For i = 2 To lr1

    DateDiff = maxDt - dt
 
    If DateDiff <= 1 And nextDt <= maxDt Then
        totalDuration = totalDuration + Sheets("Sheet5").Range("G" & i).Value
        nextDt = CDate(Sheets("Sheet5").Range("P" & i + 1).Value)
    Else
        lrw = ThisWorkbook.Sheets("Chart").Range("A" & Rows.Count).End(xlUp).Row
        Sheets("Chart").Range("A" & lrw + 1).Value = totalDuration
        totalDuration = Sheets("Sheet5").Range("G" & i).Value
        dt = CDate(Sheets("Sheet5").Range("P" & i).Value)
        maxDt = dt + 1

    End If

Next i
lrw = ThisWorkbook.Sheets("Chart").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Chart").Range("A" & lrw + 1).Value = totalDuration

End Sub

我知道这段代码并不完美,因为我不是 VBA 专家。

编辑:

您在下图中看到的只是示例数据。 J 和 K 列中的值(在我们的代码 G 和 P 中)是我们所拥有的 L、N、O 是我们想要的。我们几乎已经用我们的代码完成了“要添加的总持续时间”,而 N 和 O 列是我遇到问题的地方。

【问题讨论】:

标签: excel vba


【解决方案1】:

循环过滤的数据行

  • 未经测试。
Option Explicit

Sub FindDuration()
    
    Dim wb As Workbook: Set wb = ThisWorkbook
    
    ' Source
    Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet5")
    Dim slRow As Long: slRow = sws.Range("A" & sws.Rows.Count).End(xlUp).Row
    Dim slCol As Long
    slCol = sws.Cells(1, sws.Columns.Count).End(xlToLeft).Column
    Dim srg As Range: Set srg = sws.Range("A2", sws.Cells(slRow, slCol))
    Dim svrg As Range: Set svrg = srg.SpecialCells(xlCellTypeVisible)
    
    ' Destination
    Dim dws As Worksheet: Set dws = wb.Worksheets("Chart")
    Dim dCell As Range
    Set dCell = dws.Range("A" & dws.Rows.Count).End(xlUp).Offset(1)
    
    Dim sarg As Range
    Dim srrg As Range
    Dim dt As Date
    Dim dtNext As Date
    Dim dtMax As Date
    Dim dtDiff As Double ' possibly 'As Long' ???
    Dim totalDuration As Double
    Dim IsNotFirst As Boolean
    Dim DoGetNextDate As Boolean
    
    For Each sarg In svrg.Areas
        
        For Each srrg In sarg.Rows
            
            If Not IsNotFirst Then
                dt = CDate(srrg.Columns("P").Value)
                dtMax = dt + 1
                IsNotFirst = True
            End If
            
            dtDiff = dtMax - dt
            
            If DoGetNextDate Then
                dtNext = CDate(srrg.Columns("P").Value)
            End If
            
            If dtDiff <= 1 And dtNext <= dtMax Then
                totalDuration = totalDuration + srrg.Columns("G").Value
                DoGetNextDate = True
            Else
                dCell.Value = totalDuration
                Set dCell = dCell.Offset(1)
                totalDuration = srrg.Columns("G").Value
                dt = CDate(srrg.Columns("P").Value)
                dtMax = dt + 1
                DoGetNextDate = False
            End If
        
        Next srrg
    
    Next sarg

    'dcell.Value = totalDuration ' not quite sure???
     
End Sub

【讨论】:

  • 它有效,我正在测试,因为我需要对其添加更多更改,但是,'dt` 和 dtNext 在第一个循环中是相同的` CDate(srrg.Columns(" P").Value). How can I get the value in the next visible row for the dtNext` 在第一个循环本身?谢谢。
  • 我所说的下一个日期是指下一个可见行中可用的日期。如果我的当前行是 7 (P7),那么下一个日期 (dtNext) 就是 P8 中的值。
  • 对不起。我解决了这个问题。
  • 感谢您的努力,但是,dtNext 应该始终是下一个可见(已过滤)行中的值。当我们获得dt 时,我们必须从下一个可见行中获得dtNext。通过修改后的代码,我得到的 dtNextdt 相同。
  • 这就是我想要的。当dtNextdt 不同时,我想添加totalDurationdt。假设dt 是 P 列中的 3/1/2021,我想将 G 列中的所有相应值以及 2021 年 3 月 1 日和 2021 年 4 月 1 日的所有相应值相加(日期差异不超过一天)和在名为“图表”的工作表的 A 列中输入此内容。同时,我想将这个总和值与我们开始的日期相加,即 3/1/2021....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-15
  • 2016-01-09
  • 1970-01-01
  • 2020-04-04
  • 2022-01-03
  • 2018-09-18
  • 1970-01-01
相关资源
最近更新 更多