Excel Vba 宏来换行、居中、自动调整列和行的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42795641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel Vba Macro to Wrap, Center, Auto Size Columns and Rows
提问by Defca Trick
I am trying to get this column to do a few things, but when it gets to wrap text and center it isn't doing it...it doesn't error but it doesn't wrap or center...any thoughts? ty in advance!
我试图让这个专栏做一些事情,但是当它开始环绕文本并居中时它没有这样做......它没有错误但它没有环绕或居中......有什么想法吗?提前!
Sub Resize_Columns_And_Rows_No_Header()
'
'Resize_Columns_And_Rows Macro
'
'Declaration
Dim wkSt As String
Dim wkBk As Worksheet
Dim temp As Variant
Dim lastCol As Long
wkSt = ActiveSheet.Name
' This Loops Through All Sheets
For Each wkBk In ActiveWorkbook.Worksheets
On Error Resume Next
wkBk.Activate
lastCol = wkBk.Cells(1, Columns.Count).End(xlToLeft).Column
'This is only needed if you are wrapping the text
wkBk.Rows.WrapText = True
'This is to center align all rows
wkBk.Rows.VerticalAlignment = xlCenter
' Resize Columns
wkBk.Columns.EntireColumn.AutoFit
' Resize Rows
wkBk.Rows.EntireRow.AutoFit
Next wkBk
Sheets(wkSt).Select
End Sub
采纳答案by Bill Roberts
This worked for me.
这对我有用。
The thing is though, WrapText = Trueand Columns.EntireColumn.AutoFit, sort of contradict each other.
事情是,WrapText = True和Columns.EntireColumn.AutoFit,有点相互矛盾。
Sub Resize_Columns_And_Rows_No_Header2()
Dim currentSheet As Worksheet
Set currentSheet = ActiveSheet
Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Worksheets
With sheet
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
By the way, do you also need a:
顺便说一句,你还需要一个:
.HorizontalAlignment = xlCenter
?
?