如何在文本输入期间自动调整 Excel 中的列宽
我通常会尝试避免在 Excel 中使用 VBA,但是能够在单元格中键入文本并使其列变宽或变窄以容纳输入或删除时剩余的文本会很方便。
当然,这取决于该列中其他单元格中文本的长度。
我猜你可能会这么称呼它:“键入时自动调整”。
有没有一种简单的方法可以在合适的处理程序中做到这一点?
I usually try to avoid VBA in Excel, but it would be convenient to be able to type text into a cell, and have its column get wider or narrower to accommodate the text remaining as it's entered or deleted.
This would be subject, of course, to the lengths of the text in the other cells in the column.
'Auto-fit as you type', I guess you might call it.
Is there an easy way to do this in a suitable handler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在现代版本的 Excel(2010+,我不知道 2007 版本)中,您可以在单元格中输入数据后立即使用宏来调整列大小以适应数据。
将宏放入
ThisWorkbook
模块中In modern versions of Excel (2010+, I don't know about the 2007 version) you could use a macro to resize your column to fit data as soon you finish entering data in a cell.
Put the macro in the
ThisWorkbook
module这将自动适应列宽
This will automatically fit columns width
这是我的答案,我验证它有效:
Here is my answer, I verified it works:
我不确定是否有办法在您打字时执行此操作。我认为 Excel 通常会在触发 worksheet_change 事件之前拉伸单元格视图以显示所有文本。
在您更改目标并将目标移动到新范围后,此代码将调整列的大小。将其放入工作表模块中。
如果您想针对特定列执行此操作,则需要像这样检查目标列:
I'm not sure if there is a way to do it while you're typing. I think Excel generally stretches the cell view to display all the text before it fires the worksheet_change event.
This code will resize the column after you have changed and moved the target to a new range. Place it in the worksheet module.
If you're looking to do it for a particular column, you would need to check the target column like this: