我需要默认自动调整所有列的excel vba代码,然后循环遍历每个列的宽度,如果任何宽度超过特定值,例如50,则将该特定列的宽度限制为30并将自动换行设置为true。
Public Function LastColumn(Optional wks As Worksheet) As Long
If wks Is Nothing Then: Set wks = ActiveSheet
LastColumn = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
End Function
Sub Macro1()
Dim LastCol As Long
Cells.Select
Cells.EntireColumn.AutoFit
LastCol = LastColumn(ThisWorkbook.Sheets("Sheet1"))
For i = 1 To LastCol
If Columns(i).ColumnWidth > 70 Then
Columns(i).ColumnWidth = 70
Columns(i).WrapText = True
End If
Next i
End Sub
有没有更好的方法来实现这一目标?