2012-11-06 42 views
2

我是VBA中的新成员。我想隐藏任何行的所有行到页尾的结束。 我有它的问题是我不知道如何编程来隐藏最后一个书面行。 我使用下一个函数来了解最后写入的单元格,但我不知道在哪里放置Hide Function。隐藏/取消隐藏最后写入的行

last = Range("A100000").End(xlUp).Row 

非常感谢,任何帮助将不胜感激。

回答

1

试试这个,它将查找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 
+0

它的作品真的很好,但问题是,我想从最后写入单元格藏 - >在纸张上的最后一个单元格,但是这个代码隐藏,就像你说的,从单元格A1 - - >到最后写的。我不明白“代码”“A1:A”,它是如何工作的? – themolestones

+0

哦,我的坏,你想要另一种方式吗?那么让我们说A10是最后一行,你想隐藏A10行到最后? – CustomX

+0

@themolestones,检查我编辑的答案。 – CustomX

1

你能;

Dim startRow As Long: startRow = 10 
Dim lastRow As Long: lastRow = ActiveSheet.UsedRange.Rows.Count 

ActiveSheet.Rows(startRow & ":" & lastRow).Hidden = True 
+0

如果隐藏,则不包括最后一行 – Hogan