Excel Vba宏包装,中心,自动大小列和行

时间:2017-03-14 20:11:10

标签: excel vba excel-vba

我试图让这个专栏做一些事情,但是当它包装文本和中心时它不会这样做......它没有错误,但它并没有包裹或中心...任何想法? ty提前!

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

1 个答案:

答案 0 :(得分:3)

这对我有用。

但事情是, 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

顺便问一下,你还需要一个:

        .HorizontalAlignment = xlCenter