简体   繁体   English

在Excel VBA中隐藏行

[英]Hide Rows in excel VBA

I am trying to hide rows using VBA. 我正在尝试使用VBA隐藏行。 I have the start row from where I have to start hiding the rows. 我有一个开始行,从这里我必须开始隐藏行。 I have written the following code: 我写了以下代码:

sht.Rows(32 + end_row).Resize(, 65536 - (32 + end_row)).Select
Selection.EntireRow.Hidden = True

where end_row is the row til which the data will be populated in my excel sheet and 32 is the index of the row from which data will start populating. 其中end_row是将在excel工作表中填充数据的行til,而32是将开始填充数据的行的索引。 When i am trying to run this code it gives an error, 当我尝试运行此代码时,出现错误,

application-defined or object-defined error 应用程序定义或对象定义的错误

How do I resolve this and what should be the best way to do it? 我该如何解决?最好的方法是什么?

In this statement. 在此声明。

Resize(, 65536 - (32 + end_row)).Select   

you mix up rows and columns.. 您混合行和列。

you probably mean Resize(65536 - (32 + end_row)).Select 您可能是说Resize(65536 - (32 + end_row)).Select

Resize([Rows],[Columns])

Much better to write something like this though (avoid use of Select ) 最好写这样的东西(避免使用Select

Sheet1.Rows.Resize(65536 - (32 + end_row)).EntireRow.Hidden = True

This code hides all the rows at the top of the sheet though and leaves the rows at the bottom of the sheet visible. 该代码虽然隐藏了工作表顶部的所有行,但使工作表底部的行可见。 I assume that you want this the other way round. 我认为您反过来希望这样做。


This piece of code may help. 这段代码可能会有所帮助。 It will hide all blank rows below the last entry in Column A 它将所有空白行隐藏在A列中最后一个条目下方

Dim sht As Worksheet
Set sht = Sheet1

sht.Range(sht.Cells(sht.Rows.Count, 1), sht.Cells(sht.Rows.Count, 1).End(xlUp).Offset(1)).Rows.Hidden = True

An other way could be : 另一种方法可能是:

'Expected sht is a sheet object, else use : ActiveSheet.Rows("32:" & end_row).Hidden = True
sht.Rows("32:" & end_row).Hidden = True

Avoid to use the 65535 limitation 避免使用65535限制

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

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