简体   繁体   English

在 VBA 中合并单元格时遇到问题

[英]Having trouble merging cells in VBA

So I'm writing some VBA code that goes through my document and looks for where a formula returns an error and it merges and centers it with the cell that's underneath it.因此,我正在编写一些 VBA 代码,这些代码遍历我的文档并查找公式返回错误的位置,然后将其与位于其下方的单元格合并并居中。

Private Sub CommandButton22_Click()
     Dim strTemp As String
     Dim ev As Variant
     Dim rng As Range, cell As Range
     Set rng = Range("H87:H89")


For Each cell In rng
    If (cell.HasFormula) Then
        cell.Select

        strTemp = ActiveCell.Formula
        ev = Evaluate(strTemp)

        If (IsError(ev)) Then
            ActiveCell.Clear
            ActiveCell.Merge ([0,1])

        End If
    End If
Next cell
End Sub

This is what I have so far, it clears the cell properly but won't merge.这是我到目前为止所拥有的,它可以正确清除单元格但不会合并。

Try using:尝试使用:

Range(Cells(ActiveCell.Row, ActiveCell.Column), Cells(ActiveCell.Row + 1, ActiveCell.Column)).Merge

Hope it helps.希望能帮助到你。

Something like this.像这样的东西。 Note you rarely need select/activate and should try to avoid it as much as possible.请注意,您很少需要选择/激活,应该尽量避免它。

Private Sub CommandButton22_Click()

    Dim cell As Range

    For Each cell In Range("H87:H89").Cells
        If cell.HasFormula Then
            If IsError(cell.Value) Then
                cell.Clear
                cell.Resize(2, 1).Merge
            End If
        End If
    Next cell

End Sub

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

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