Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA宏的行为因所选单元格而异_Vba_Excel - Fatal编程技术网

VBA宏的行为因所选单元格而异

VBA宏的行为因所选单元格而异,vba,excel,Vba,Excel,我有一个AutoFitMergedCellRowHeight子例程,它将合并的单元格作为参数,然后固定其高度,以便所有文本都可见。按下按钮时,FixAll子按钮被激活 问题是它的行为不稳定。选择与合并单元格(第4列)位于同一列中的单元格时,高度为一个大小(较小,但文本100%可见);当在该列外部但在表内部选择单元格时,不会发生任何事情;当在表格外选择单元格时,高度是固定的,但会变得太大 为什么会这样?我在sub中看不到任何与选定单元格相关的内容 Sub FitAll() AutoFitMe

我有一个
AutoFitMergedCellRowHeight
子例程,它将合并的单元格作为参数,然后固定其高度,以便所有文本都可见。按下按钮时,
FixAll
子按钮被激活

问题是它的行为不稳定。选择与合并单元格(第4列)位于同一列中的单元格时,高度为一个大小(较小,但文本100%可见);当在该列外部但在表内部选择单元格时,不会发生任何事情;当在表格外选择单元格时,高度是固定的,但会变得太大

为什么会这样?我在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
编辑:我还将我的结果与不使用参数而是使用选定单元格的同一个子集进行比较。即使应用了建议的更改,结果也会有所不同

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
    MergedCellRgWidth = CurrCell.ColumnWidth + MergedCellRgWidth
Next
比如:

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

我认为你知道这条线索。您可以在自己的代码中探索不同之处。@Variatus,谢谢您,我来看看。虽然我也很想理解代码的问题,但代码不能用于包含多行的选择。我们不知道您的选择,因此很难说代码有什么问题。无论如何,
FitAll
程序将范围指定为D3。因此,正如@CLR在下面的回答中指出的那样,对选择的后续引用必然会引起问题。谢谢!但它与使用基于宏的ob选择(将其添加到问题中)仍有一些不同。选择子例程使高度小于此高度。但是为什么呢?如果两者基本上使用相同的范围-作为参数选择或传递..则解决它。