无法在VBA中合并单元格

所以我写了一些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 

这是我到目前为止,它正确清除单元格,但不会合并。

尝试使用:

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

希望能帮助到你。

像这样的东西。 注意你很less需要select/激活,应尽量避免它。

 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