excel - 设置列宽大小命令不循环遍历所有活动工作表
问题描述
下面的代码执行没有错误,除了下面的行没有在所有活动工作表中执行(可能是因为需要包含行?)。
Columns("T").ColumnWidth = 50
完整的宏是:
Sub Resize_Columns_And_Rows_No_Header2()
Dim currentSheet As Worksheet
Set currentSheet = ActiveSheet
Dim sheet As Worksheet
Columns("T").ColumnWidth = 50
For Each sheet In ActiveWorkbook.Worksheets
With sheet
Columns("T").ColumnWidth = 50
With .Cells.Rows
.WrapText = True
.VerticalAlignment = xlCenter
.EntireRow.AutoFit
End With '.Cells.Rows
.Columns.EntireColumn.AutoFit
End With 'sheet
Next sheet
currentSheet.Activate
End Sub
解决方案
问题是Columns("T").ColumnWidth = 50
您With sheet
需要以点开头的行,否则将不会在sheet
.
因此,将其中的行更改With sheet
为:
.Columns("T").ColumnWidth = 50
实际上,您不需要这些currentSheet
东西,最终也不需要,currentSheet.Activate
因为活动工作表永远不会更改运行以下代码:
Public Sub Resize_Columns_And_Rows_No_Header2()
Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Worksheets
With sheet
.Columns("T").ColumnWidth = 50
With .Cells.Rows
.WrapText = True
.VerticalAlignment = xlCenter
.EntireRow.AutoFit
End With '.Cells.Rows
.Columns.EntireColumn.AutoFit
End With 'sheet
Next sheet
End Sub
推荐阅读
- python - subprocess.run() 不能使用多个命令
- symfony - Symfony: Doctrine cannnot find table with first letter Uppercase
- r - R:遍历 ggplot 的回归模型列表
- django - django 会话如何工作
- asp.net - asp.net 网站,url 带 www 不带 www 时的认证
- javascript - 如何同步获取响应和异步请求?
- javascript - 在反应中单击按钮时访问使用道具初始化的输入值的正确方法
- php - Make a page with a parameter same as a page without parameter
- angular - 检索输入字段设置值
- axure - axure 只生成 html 代码,没有其他任何东西