自动显示(取消隐藏)Excel 中的列
有没有一种方法可以让 Excel(2003、2007 和/或 2010)中的列在左侧列包含数据时自动显示(取消隐藏)?
像这样的事情:
IF column to the left contains data
THEN show column
+-----+-----+
| C | C | //If column1 contains data
| O | O | //Then reveal/show (unhide) column2
| L | L |
| U | U |
| M | M |
| N | N |
| 1 | 2 |
+-----+-----+
我猜测需要 VB 代码,但不确定这是什么。
除此之外,有没有办法自动显示按日期(每个月的第一天)的列?这有点复杂。例如:
FOR all dates
IF system date = year(month.day1) //If it is the first day of a new month
THEN show column(month) //Then show the corresponding column for that month
ENDIF
ENDFOR
i.e. IF system date = 01/09/2012
THEN show column(September)
这可能吗?
谢谢。
Is there a method whereby columns in Excel (2003, 2007 and/or 2010) can be automatically shown (un-hidden) when the column to the left contains data?
Something like this:
IF column to the left contains data
THEN show column
+-----+-----+
| C | C | //If column1 contains data
| O | O | //Then reveal/show (unhide) column2
| L | L |
| U | U |
| M | M |
| N | N |
| 1 | 2 |
+-----+-----+
I'm guessing that VB code is required but am unsure as to what this would be.
Further to this, is there a way to automatically show the column going by the date (first day of each month)? This is a little more complicated. For example:
FOR all dates
IF system date = year(month.day1) //If it is the first day of a new month
THEN show column(month) //Then show the corresponding column for that month
ENDIF
ENDFOR
i.e. IF system date = 01/09/2012
THEN show column(September)
Is this possible?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的,你需要 VBA 来实现这一点。使用 Worksheet_Change 事件,每当发生更改时就会触发该事件。然后,使用各种方法之一来确定列是否不为空(http://ewbi.blogs.com/develops/2006/03/define_if_a_.html,或只是谷歌)。或者,如果由于几乎一直触发而导致速度变慢,您可以使用 Worksheet_Activate() 事件(可以在 此处)。
如果第 7 列包含数据,则可以使用 MyWorksheet.Columns("8:8").Hidden = False 取消隐藏第 8 列。
你的第二个问题可以用类似的方式解决。在Worksheet_Activate()中,使用Day(Date) = 1检查今天是否是一个月中的第一天(我猜需要考虑到用户这一天可能没有使用Excel,所以代码应该有点更复杂)并使用 MyWorksheet.Columns("12:12").Hidden = False (或以哪个值为准)显示 12 月 1 日的该列。
所有这些代码都假设列已经存在,只是隐藏了。
Correct, you need VBA to achieve that. Use the Worksheet_Change event which fires whenever something changes. Then, use one of the various methods to determine if a column is not empty (http://ewbi.blogs.com/develops/2006/03/determine_if_a_.html, or just google). Alternatively, if this is to slow because if fires almost all the time, you could use the Worksheet_Activate() event (an overview of all Excel events can be found here).
If your column 7 contains data, you can unhide column 8 using MyWorksheet.Columns("8:8").Hidden = False.
Your second problem can be solved in a similar way. In the Worksheet_Activate(), check if today is the first day in a month using Day(Date) = 1 (I guess it needs to take into account that the user may not be using Excel this day, so the code should be a little more complex) and show that column using MyWorksheet.Columns("12:12").Hidden = False (or whichever it is) for December 1st.
All this code assumes that the columns are already there, just hidden.