我有一张包含 6 个表格的工作表,每个表格使用 B:N 列。B 列包含每个表格从凌晨 1 点到晚上 12 点的小时数。我需要删除单元格 AF2 中包含特定值的单元格下方的所有行。例如,AF2 包含下午 5 点。每个表格 B 列中下午 5 点以下的所有行都应删除。
这是我目前所拥有的:
Sub HideRows1()
Dim rCheck As Range
Dim rHide As Range
Dim rCheckCell As Range
Dim time1 As String
time1 = Sheet8.Range("AF2").Value
Set rCheck = ActiveWorkbook.ActiveSheet.Range("B:B")
rCheck.EntireRow.Hidden = False
For Each rCheckCell In rCheck.Cells
If InStr(1, rCheckCell, time1, vbTextCompare) > 0 Then
If Not rHide Is Nothing Then Set rHide = Union(rHide, rCheckCell) Else Set rHide = rCheckCell
End If
Next rCheckCell
If Not rHide Is Nothing Then rHide.EntireRow.Hidden = True
子目录结束
答案1
如果 B 列中具有值的单元格下方至少有一个空单元格12AM
,则
Set sh = Sheets("Sheet8")
Set cell1 = sh.Range("B1")
valueToFind = sh.Range("F2").Value
Do
Set cell1 = sh.Range(cell1.Address & ":B" & sh.UsedRange.Rows.Count).Find(What:=valueToFind, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If cell1 Is Nothing Then Exit Sub
Set cell2 = sh.Range(cell1.Address & ":B" & sh.UsedRange.Rows.Count).End(xlDown)
If IsEmpty(cell1.Offset(1, 0)) Then Exit Sub
Rows(cell1.Offset(1, 0).Row & ":" & cell2.Row).Delete
Set cell1 = cell1.Offset(1, 0)
Loop