简体   繁体   English

使用VBA查找合并单元格的最后一行

[英]using VBA to find the last row of a merged cell

I am trying to use find the last row of a merged cell with text and hide all rows beside that我正在尝试使用查找带有文本的合并单元格的最后一行并隐藏其旁边的所有行

For example:例如:

A1:A5 is a merged cell with text "A", A6:A10 is a merged cell with text "B", etc A1:A5 是带有文本“A”的合并单元格,A6:A10 是带有文本“B”的合并单元格,等等

I want to write a code that would find the last row of the merged cell with text "B", and would hide any rows above or below the merged cell.我想编写一个代码来找到带有文本“B”的合并单元格的最后一行,并隐藏合并单元格上方或下方的任何行。

At the moment I am defining the rows to hide manually, but these change frequently so my method is not very efficient.目前我正在定义要手动隐藏的行,但这些行经常更改,因此我的方法效率不高。

Any suggestions on how to find the last row instead?关于如何找到最后一行的任何建议?

Sub FindLastRow()

Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Test")

'Hide all rows above B
ws.Rows("1:5").EntireRow.Hidden = True

'Hide all rows below B
ws.Rows("11:80").EntireRow.Hidden = True

End Sub

I guess you could try the following, making use of Range.Find and Range.MergeArea :我想您可以尝试以下操作,利用Range.FindRange.MergeArea

Sub Test()

Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Test")
Dim fullRng As Range, fndRng As Range

Set fullRng = ws.Range("A1:A80")
Set fndRng = ws.Range("A1:A80").Find(What:="B", Lookat:=xlWhole)

If Not fndRng Is Nothing Then
    fullRng.Rows.Hidden = True
    fndRng.MergeArea.Rows.Hidden = False
End If

End Sub

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

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