简体   繁体   English

VBA宏的行为取决于所选单元格

[英]VBA Macros behaves differently depending on selected cell

I have an AutoFitMergedCellRowHeight subroutine that takes a merged cell as an argument and then fixes its height so that all the text will be visible. 我有一个AutoFitMergedCellRowHeight子例程,该子例程将合并的单元格作为参数,然后固定其高度,以便所有文本都可见。 The FixAll sub is activated when a button is pressed. 当按下按钮时, FixAllFixAll被激活。

The problem is it's behavior is unstable. 问题在于它的行为不稳定。 When a cell is selected that is in the same column as the merged cell (column 4) the height is one size (smaller, but the text is 100% visible); 如果选择的单元格与合并的单元格在同一列(第4列),则高度为一个大小(较小,但文本为100%可见); when a cell is selected outside that column but inside a table nothing happens; 当在该列外但在表内选择一个单元格时,则不会发生任何事情; when a cell is selected outside the table the height is fixed but get too big. 当在表格外选择单元格时,高度是固定的,但太大。

Why is this happening? 为什么会这样呢? I can't see anything related to a selected cell in the sub. 我在子菜单中看不到与选定单元格相关的任何内容。

Sub FitAll()
   AutoFitMergedCellRowHeight (Cells(3, 4))
End Sub

Sub AutoFitMergedCellRowHeight(cell As Range)
    Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
    Dim CurrCell As Range
    Dim ActiveCellWidth As Single, PossNewRowHeight As Single
    If cell.MergeCells Then
        With cell.MergeArea
            .WrapText = True
            If .Rows.Count = 1 Then
                cell = cell.MergeArea.Cells(1, 1)
                MsgBox (cell.Row & "and" & cell.Column)

                Application.ScreenUpdating = False
                CurrentRowHeight = .RowHeight
                ActiveCellWidth = cell.ColumnWidth
                For Each CurrCell In Selection
                    MergedCellRgWidth = CurrCell.ColumnWidth + 
                        MergedCellRgWidth

                Next
                .MergeCells = False
                .Cells(1).ColumnWidth = MergedCellRgWidth
                .EntireRow.AutoFit
                PossNewRowHeight = .RowHeight
                .Cells(1).ColumnWidth = ActiveCellWidth
                .MergeCells = True
                .RowHeight = IIf(CurrentRowHeight > PossNewRowHeight, _
                CurrentRowHeight, PossNewRowHeight)

            End If
        End With
    End If
End Sub

EDIT: I compare my results also to the same sub that doesn't use an argument but rather a selected cell. 编辑:我也比较我的结果与不使用参数,而是一个选定的单元格的同一子。 The results differ thought even after applying the changes CLR suggested.. 即使应用了CLR建议的更改,结果也会有所不同。

Sub AutoFitMergedActiveCellRowHeight()
    Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
    Dim CurrCell As Range
    Dim ActiveCellWidth As Single, PossNewRowHeight As Single
    If ActiveCell.MergeCells Then
        With ActiveCell.MergeArea
            .WrapText = True
            If .Rows.Count = 1 Then
                Application.ScreenUpdating = False
                CurrentRowHeight = .RowHeight
                ActiveCellWidth = ActiveCell.ColumnWidth
                For Each CurrCell In Selection
                    MergedCellRgWidth = CurrCell.ColumnWidth + MergedCellRgWidth

                Next
                .MergeCells = False
                .Cells(1).ColumnWidth = MergedCellRgWidth
                .EntireRow.AutoFit
                PossNewRowHeight = .RowHeight
                .Cells(1).ColumnWidth = ActiveCellWidth
                .MergeCells = True
                .RowHeight = IIf(CurrentRowHeight > PossNewRowHeight, _
                CurrentRowHeight, PossNewRowHeight)
            End If
        End With
    End If
    'MsgBox ("DONE")
    MsgBox (ActiveCell.Row & "and" & ActiveCell.Column)
End Sub

For Each CurrCell In Selection is looking at selected cell, not cell passed in parameter. For Each CurrCell In Selection它查看的是选定的单元格,而不是传入参数的单元格。

I think you want to replace: 我认为您要替换:

For Each CurrCell In Selection
    MergedCellRgWidth = CurrCell.ColumnWidth + MergedCellRgWidth
Next

with something like: 与类似:

For Each CurrCell In cell.MergeArea
    MergedCellRgWidth = CurrCell.ColumnWidth + MergedCellRgWidth
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM