我必须生成一个即将发生的事件的电子表格,并且我使用一个宏来创建一条粗线,该粗线将每个日期与其上方的日期分隔开。它基于“日期”(Date)列中的值更改。但是,有时我必须通过其他条件(例如,县)来过滤数据。在这种情况下,我一直使用的offset宏并不总是有效,因为更改和产生该行的数据位于隐藏行中,因此该行也是如此。有人可以帮忙吗?
我尝试了多种将范围定义为仅活动单元格的方法,但我认为我做得不正确。
我正在使用的宏如下,而不适用于隐藏行:
Sub UpcomingLines()
Application.ScreenUpdating = False
Dim LastRow As Long
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim rng As Range
For Each rng In Range("A1:A100" & LastRow)
If rng <> rng.Offset(1, 0) Then
Range("A" & rng.Row & ":H" & rng.Row).Borders(xlEdgeBottom).Weight = xlThick
End If
Next rng
Application.ScreenUpdating = True
End Sub
我已经尝试过像这样集成SpecialCell:
Sub UpcomingLines()
Application.ScreenUpdating = False
Dim LastRow As Long
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim rng As Range
Set myrange = Range("A1:H" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible)
For Each rng In Range("A1:A100" & LastRow)
If rng <> rng.Offset(1, 0) Then
Range("A" & myrange.Row & ":H" & rng.Row).Borders(xlEdgeBottom).Weight = xlThick
End If
Next rng
Application.ScreenUpdating = True
End Sub
但是,这会在我不希望出现的位置上生成行-基本上,日期之间的显示会发生变化,但即使在隐藏行之前或之后没有日期更改,每个地方也会有一个隐藏行。
答案 0 :(得分:1)
尝试这样的事情:
Sub UpcomingLines()
Dim ws As Worksheet, LastRow As Long, c As Range, theDate
Application.ScreenUpdating = False
Set ws = ActiveSheet
ws.Range("A1").CurrentRegion.Borders.LineStyle = xlNone 'remove existing borders
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
theDate = 0
For Each c In ws.Range("A2:A" & LastRow).SpecialCells(xlCellTypeVisible)
'different date from previous visible row?
If c.Value <> theDate Then
'add border to top of row if not the first change
If theDate <> 0 Then c.Resize(1, 8).Borders(xlEdgeTop).Weight = xlThick
theDate = c.Value 'remember this date
End If
Next c
Application.ScreenUpdating = True
End Sub