简体   繁体   English

仅循环过滤可见行

[英]Loop through only filtered visible rows

I have a problem with below code.我对以下代码有疑问。 I would like to filter "OS" (filed 61) then if first cell in 1st column below filters is not empty macro should go to first cell below filters in column "57", check if value in that cell is > 365 if yes it should go to column 62 in the same row and put there "overdue" if no then put there "OK".我想过滤“OS”(归档 61),然后如果过滤器下方第一列中的第一个单元格不是空宏,则应将 go 到“57”列中过滤器下方的第一个单元格,检查该单元格中的值是否 > 365(如果是)应该将 go 填入同一行的第 62 列,如果没有则放在那里“过期”,然后放在那里“确定”。 After that it should go to next row and check the same till the end of the filtered rows.之后它应该 go 到下一行并检查相同的内容直到过滤行的末尾。

The problem is with visible only cells.问题在于仅可见的单元格。 Macro is doing it on all rows even not visible.宏正在对所有行执行此操作,甚至不可见。

It should work only for filtered visible rows.它应该只适用于过滤后的可见行。 Any suggestions?有什么建议么?

Sub Patch_Overdue()

Dim i As Long
Dim LastRow As Long
  
Sheets("Sheet1").Select

'filter AIX OS

 Selection.Autofilter Field:=61, Criteria1:="AIX*"
 ActiveSheet.Autofilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 61).Select

 If IsEmpty(Selection) = False Then

 LastRow = Range("a7").End(xlDown).Row

  For i = 1 To LastRow
     
   If ActiveSheet.Autofilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(i, 57).Value > 365 Then
   
   ActiveSheet.Autofilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(i, 62).Select
   ActiveCell.FormulaR1C1 = "Overdue"
     
   Else
   
   ActiveSheet.Autofilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(i, 62).Select
   ActiveCell.FormulaR1C1 = "OK"
     
   End If
     
   Next i

  Else

    ActiveSheet.ShowAllData

 End If

End Sub

Please, try the next code.请尝试下一个代码。 It is not tested, but it should work.它没有经过测试,但它应该可以工作。 Basically, it set the range to be processed based on the last cell in A:A and UserRange number of columns, extract the visible cells range, iterate between its areas and the between each area rows and check what you need:基本上,它根据 A:A 中的最后一个单元格和UserRange列数设置要处理的范围,提取可见单元格范围,在其区域和每个区域行之间迭代并检查您需要什么:

Sub Patch_Overdue()
 Dim sh As Worksheet, rngUR As Range, rngVis As Range, i As Long, LastRow As Long
  
 Set sh = Sheets("Sheet1")
 If sh.AutoFilterMode Then sh.AutoFilterMode = False     'eliminate a previous filter to correctly calculate last row
 LastRow = sh.Range("A" & sh.rows.count).End(xlUp).row   'last row

 'filter AIX OS
  Set rngUR = sh.Range("A7", sh.cells(LastRow, sh.UsedRange.Columns.count)) 'set the range to be filtered
  rngUR.AutoFilter field:=61, Criteria1:="AIX*"                'filter the range according to criteria
  Set rngVis = rngUR.Offset(1).SpecialCells(xlCellTypeVisible) 'set the visible cells range

 Dim arRng As Range, r As Range
 For Each arRng In rngVis.Areas                 'iterate between the range areas:
    For Each r In arRng.rows                    'iterate between the area rows:
        If WorksheetFunction.CountA(r) > 0 Then 'for the case of the last row which is empty because of Offset
            If r.cells(1, 57).value > 356 Then
                r.cells(1, 62).value = "Overdue"
            Else
                r.cells(1, 62).value = "OK"
            End If
        End If
    Next
 Next
 sh.ShowAllData
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM