Excel 2010 中隐藏行的宏
我对 VBA 编程有点陌生。我在互联网上阅读了一些内容,但找不到我需要的内容或无法使其正常工作。我的问题:
在单元格 B6 的工作表“表 1”中,给出了项目将被利用的年数。
在工作表“sheet 2”和“sheet 3”中,我制作了 50 年的电子表格(第 1 年到第 50 年;第 7 行到第 56 行)。
在“sheet 1”的单元格 b6 中,我想输入一个 1 到 50 之间的值。当值为 49 时,我想隐藏“sheet2”和“sheet 3”中的第 56 行。当值为 48 时,我想隐藏“sheet2”和“sheet 3”中的第 55:56 行,依此类推。 这是我到目前为止所得到的,但当我更改单元格 B6 中的值时,我无法让它自动工作:
Sub test1()
If Range("sheet1!B6") = 50 Then
Rows("52:55").EntireRow.Hidden = False
Else
If Range("sheet1!B6") = 49 Then
Rows("55").EntireRow.Hidden = True
Else
If Range("sheet1!B6") = 48 Then
Rows("54:55").EntireRow.Hidden = True
End If: End If: End If:
End Sub
我希望有人可以帮助我解决我的问题。
谢谢
I'm kinda new to programming in VBA. I read some stuff on the internet but I couldnt find what I need or couldnt get it working. My problem:
in worksheet 'sheet 1' in cell B6 a value is given for how many years a project will be exploited.
in worksheets 'sheet 2' and 'sheet 3' i made a spreadsheet for 50 years ( year 1 to year 50; row 7 to row 56).
in cell b6 in 'sheet 1' i want to enter a value between 1 and 50. when the value is 49 i want to hide row 56 in 'sheet2' and 'sheet 3'. when the value is 48 i want to hide rows 55:56 in 'sheet2' and 'sheet 3', and so on.
this is what i got so far but i cant get it to work automaticly when i change the value in cell B6:
Sub test1()
If Range("sheet1!B6") = 50 Then
Rows("52:55").EntireRow.Hidden = False
Else
If Range("sheet1!B6") = 49 Then
Rows("55").EntireRow.Hidden = True
Else
If Range("sheet1!B6") = 48 Then
Rows("54:55").EntireRow.Hidden = True
End If: End If: End If:
End Sub
i hope someone can help me with my problem.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你几乎明白了。
您正在隐藏活动工作表中的行。这没关系。但更好的方法是在它所在的位置添加。
。
没有它我就会发生奇怪的事情 至于让它自动化。您需要在 VBA 编辑器(不是模块,双击编辑器最左侧的sheet1)中的工作表宏中使用 worksheet_change 事件。)在该工作表中,使用编辑器本身上方的下拉菜单(应该有2 个列表框)。左侧的列表框将包含您要查找的事件。之后只需放入宏即可。它应该类似于下面的代码,
就是这样。每当您更改某些内容时,它都会运行宏 test1。
You almost got it.
You are hiding the rows within the active sheet. which is okay. But a better way would be add where it is.
becomes
i've had weird things happen without it. As for making it automatic. You need to use the worksheet_change event within the sheet's macro in the VBA editor (not modules, double click the sheet1 to the far left of the editor.) Within that sheet, use the drop down menu just above the editor itself (there should be 2 listboxes). The listbox to the left will have the events you are looking for. After that just throw in the macro. It should look like the below code,
That's it. Anytime you change something, it will run the macro test1.
好吧,你走在正确的道路上,本诺!
有一些有关 VBA 编程的提示可能会对您有所帮助。
始终使用对要与之交互的工作表的显式引用。否则,Excel 可能会“假设”您的代码适用于活动工作表,最终您会发现它搞砸了您的电子表格。
正如 lionz 提到的,接触 Excel 提供的本机方法。您可能会在大多数技巧中使用它们。
显式声明您的变量...它们将显示每个对象在 VBA 中提供的方法列表。它可能会节省您在互联网上挖掘的时间。
现在,让我们有一个草稿代码...
请记住,此代码必须位于 Excel Sheet 对象内,如 lionz 所解释的。它仅适用于 Sheet 2,您可以按照自己喜欢的方式将其适应 Sheet 2 和 Sheet 3。
希望有帮助!
Well, you're on the right path, Benno!
There are some tips regarding VBA programming that might help you out.
Use always explicit references to the sheet you want to interact with. Otherwise, Excel may 'assume' your code applies to the active sheet and eventually you'll see it screws your spreadsheet up.
As lionz mentioned, get in touch with the native methods Excel offers. You might use them on most of your tricks.
Explicitly declare your variables... they'll show the list of methods each object offers in VBA. It might save your time digging on the internet.
Now, let's have a draft code...
Remember this code must be within the Excel Sheet object, as explained by lionz. It only applies to Sheet 2, is up to you to adapt it to both Sheet 2 and Sheet 3 in the way you prefer.
Hope it helps!