对工作簿中的所有活动工作表

huangapple go评论54阅读模式
英文:

For all active sheets in a workbook

问题

我有一个VB脚本来自动调整合并单元格的高度,但它只适用于工作簿的第一个工作表/当前工作表。

这个VBA脚本最初来自于使用VBA自动调整合并单元格,我稍微改变了一些范围。

我想让所有可用/活动工作表运行相同的VB脚本。所以,我在"AutofixMergedCellHeight" VB脚本中添加了以下"SelectedSheets"代码,但它没有起作用。

有人可以帮忙吗?非常感谢!

AutofixMergedCellHeight:

Sub FixMerged()
    Dim mw As Single
    Dim cM As Range
    Dim rng As Range
    Dim cw As Double
    Dim rwht As Double
    Dim ar As Variant
    Dim i As Integer
    Application.ScreenUpdating = False
    '下面是单元格范围,根据需要更改。
    ar = Array("B32", "B33")
    For i = 1 To UBound(ar)
        On Error Resume Next
        Set rng = Range(Range(ar(i)).MergeArea.Address)
        With rng
            .MergeCells = False
            cw = .Cells(1).ColumnWidth
            mw = 0
            For Each cM In rng
                cM.WrapText = True
                mw = cM.ColumnWidth + mw
            Next
            mw = mw + rng.Cells.Count * 0.66
            .Cells(1).ColumnWidth = mw
            .EntireRow.AutoFit
            rwht = .RowHeight
            .Cells(1).ColumnWidth = cw
            .MergeCells = True
            .RowHeight = rwht
        End With
    Next i
    Application.ScreenUpdating = True
End Sub

我还添加了一个新的脚本,通过使用'For Each'循环遍历工作簿中的所有工作表,并插入了对"SelectSheets"和"FixMerged"的调用。但这种方式也只适用于当前单个工作表。

Sub WorksheetLoop()
    Dim Current As Worksheet
    For Each Current In Worksheets
        SelectSheets
        FixMerged
    Next
End Sub
英文:

I have a VB script to autofit merged cells height, but it only works for the first sheet/current sheet of the workbook.

The VBA Script is originally from Autofit Merged Cells with VBA, and I made some slightly range change.

I'd like to make all my available/active sheets to run this same VB script. So, I added the following SlectedSheets code to the AutofixMergedCellHeigh VB script, but it didn't work.

Does anyone could help out? Thanks a lot!

AutofixMergedCellHeight:

对工作簿中的所有活动工作表

Sub FixMerged()
    Dim mw As Single
    Dim cM As Range
    Dim rng As Range
    Dim cw As Double
    Dim rwht As Double
    Dim ar As Variant
    Dim i As Integer
    Application.ScreenUpdating = False
    'Cell Ranges below, change to suit.
    ar = Array("B32", "B33")
    For i = 1 To UBound(ar)
    On Error Resume Next
    Set rng = Range(Range(ar(i)).MergeArea.Address)
        With rng
           .MergeCells = False
            cw = .Cells(1).ColumnWidth
            mw = 0
        For Each cM In rng
            cM.WrapText = True
            mw = cM.ColumnWidth + mw
        Next
        mw = mw + rng.Cells.Count * 0.66
        .Cells(1).ColumnWidth = mw
        .EntireRow.AutoFit
        rwht = .RowHeight
        .Cells(1).ColumnWidth = cw
        .MergeCells = True
        .RowHeight = rwht
        End With
    Next i
    Application.ScreenUpdating = True
End Sub

对工作簿中的所有活动工作表

I also add a new script to loop through all of the worksheets in the workbook by using a 'For Each' loop, and inserted calls for SelectSheets and FixMerged. But this way only works on the current single sheet too.

Sub WorksheetLoop()
    Dim Current As Worksheet
    For Each Current In Worksheets
        SelectSheets
        FixMerged
    Next
End Sub

答案1

得分: 2

将工作表对象传递给另一个过程

调用过程

Sub FixMergedAll()

    Dim wb As Workbook: Set wb = ThisWorkbook ' 包含此代码的工作簿

    Application.ScreenUpdating = False

    Dim ws As Worksheet

    For Each ws In wb.Worksheets
        FixMergedCells ws
    Next ws

    Application.ScreenUpdating = True

End Sub

被调用的过程

Sub FixMergedCells(ByVal ws As Worksheet) ' ***

    Dim mw As Single
    Dim cM As Range
    Dim rng As Range
    Dim cw As Double
    Dim rwht As Double
    Dim ar As Variant
    Dim i As Long ' ***

    ' 下面是单元格范围,请根据需要进行更改。
    ar = Array("B32", "B33")

    For i = LBound(ar) To UBound(ar) ' ***

        On Error Resume Next

        Set rng = ws.Range(ar(i)).MergeArea ' ***

        With rng

           .MergeCells = False
            cw = .Cells(1).ColumnWidth
            mw = 0

            For Each cM In rng
                cM.WrapText = True
                mw = cM.ColumnWidth + mw
            Next

            mw = mw + rng.Cells.Count * 0.66
            .Cells(1).ColumnWidth = mw
            .EntireRow.AutoFit
            rwht = .RowHeight
            .Cells(1).ColumnWidth = cw
            .MergeCells = True
            .RowHeight = rwht

        End With

    Next i

End Sub
英文:

Passing a Worksheet Object to Another Procedure

The Calling Procedure

<!-- language: lang-vb -->

Sub FixMergedAll()
    
    Dim wb As Workbook: Set wb = ThisWorkbook &#39; workbook containing this code
    
    Application.ScreenUpdating = False
    
    Dim ws As Worksheet
    
    For Each ws In wb.Worksheets
        FixMergedCells ws
    Next ws

    Application.ScreenUpdating = True

End Sub

The Called Procedure

  • &#39; *** designates the locations of the introduced changes. For the rest, you're on your own.

<!-- language: lang-vb -->

Sub FixMergedCells(ByVal ws As Worksheet) &#39; ***
    
    Dim mw As Single
    Dim cM As Range
    Dim rng As Range
    Dim cw As Double
    Dim rwht As Double
    Dim ar As Variant
    Dim i As Long &#39; ***
    
    &#39;Cell Ranges below, change to suit.
    ar = Array(&quot;B32&quot;, &quot;B33&quot;)
    
    For i = LBound(ar) To UBound(ar) &#39; ***
        
        On Error Resume Next
        
        Set rng = ws.Range(ar(i)).MergeArea &#39; ***
        
        With rng
           
           .MergeCells = False
            cw = .Cells(1).ColumnWidth
            mw = 0
            
            For Each cM In rng
                cM.WrapText = True
                mw = cM.ColumnWidth + mw
            Next
            
            mw = mw + rng.Cells.Count * 0.66
            .Cells(1).ColumnWidth = mw
            .EntireRow.AutoFit
            rwht = .RowHeight
            .Cells(1).ColumnWidth = cw
            .MergeCells = True
            .RowHeight = rwht
        
        End With
    
    Next i

End Sub

huangapple
  • 本文由 发表于 2023年3月7日 09:02:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定