我是 VBA 新手。我想隐藏从任何行到工作表末尾的所有行。我遇到的问题是我不知道如何编程以隐藏最后写入的行。我使用下一个函数知道最后写入的单元格,但我不知道在哪里放置隐藏函数。
last = Range("A100000").End(xlUp).Row
非常感谢,任何帮助将不胜感激。
试试这个,这将在 colA 中查找最后一个值,并将所有行从 A1 直到 LastRow 隐藏。
Sub test()
'Hides all rows with data
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("A1:A" & LastRow).EntireRow.Hidden = True 'to unhide set to False
'Hides last row until the end of the sheet
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
Range("B" & LastRow + 1 & ":B1048576").EntireRow.Hidden = True 'to unhide set to False
'Hides last row + 1 until the end of the sheet
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
Range("B" & LastRow + 1 & ":B1048576").EntireRow.Hidden = True 'to unhide set to False
End Sub
有关代码的额外信息
假设我们有从 A1 到 A10 的数据
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
'we use this to get the lastrow number, in this case it will be 10
'the code is variable, so if we add data until A15, lastrow will become 15
Range("A1:A" & LastRow)
'this is the range that will be hidden A1 until A10
你可以;
Dim startRow As Long: startRow = 10
Dim lastRow As Long: lastRow = ActiveSheet.UsedRange.Rows.Count
ActiveSheet.Rows(startRow & ":" & lastRow).Hidden = True