【问题标题】:String in a formula in excel VBAExcel VBA中公式中的字符串
【发布时间】:2021-10-31 16:31:40
【问题描述】:

我正在为以下代码中的 iferror 函数中的字符串而苦苦挣扎。我希望字符串是可变的,如果出现错误,则会弹出一个输入框,用于指定用户想要的字符串。

Sub test_IFERROR()
Dim frange As Range, xcell As Range
Dim xAddress As String
Dim xStr As String
    On Error Resume Next
    xAddress = Application.ActiveWindow.RangeSelection.Address
    Set frange = Application.InputBox("Please select a range", "Kutools for Excel", xAddress, , , , , 8)
    Set frange = ws.Cells.SpecialCells(xlCellTypeFormulas)
    On Error GoTo 0
    If Not frange Is Nothing Then
    xStr = Application.InputBox("Replace blank cells with what?", "Kutools for Excel")
        xUpdate = Application.ScreenUpdating
        Application.ScreenUpdating = False
        For Each xcell In frange
            c.Formula = "=IFERROR(" & Right(c.Formula, Len(c.Formula) - 1) & ","" & xStr & "")"
        Next xcell
    End If
    Set frange = Nothing
End Sub

【问题讨论】:

  • 试试:c.Formula = "=IFERROR(" & Mid$(c.Formula, 2) & ",""" & xStr & """)"
  • 谢谢你,但它不起作用。
  • 它给出一个错误并弹出调试屏幕
  • c.Formula = "=IFERROR(" & Right(c.Formula, Len(c.Formula) - 1) & ","" & xStr & "")" 问题出在这线。我试过你的,但它不起作用
  • 我的代码有几个错误。你的一个工作得很好。谢谢你帮助我。

标签: excel vba string excel-formula


【解决方案1】:

你的代码有几个问题,都是因为你没有使用Option Explicit。如果你这样做了,你会立即看到你使用了不正确的变量。

ws 变量从未被声明或赋值。所以你应该改变:

Set frange = ws.Cells.SpecialCells(xlCellTypeFormulas)

到:

Set frange = frange.SpecialCells(xlCellTypeFormulas)

这也不是一个好主意,因为如果没有公式,frange 将保留为原始选择(忽略错误)。因此,如果您有 2 个单独的范围,则更好(并且更易于阅读)。一个用于初始选择,一个用于公式。

xUpdate 也永远不会被声明,而且您永远不会使用它来将Application.ScreenUpdating 的状态恢复到原来的状态。

没有c 变量。你的循环迭代器被称为xcell,所以使用c.Formula 没有意义,除非你真的想要xcell.Formula

最后,你应该按照@Rory 的建议(部分)做,将","" & xStr & "")" 替换为",""" & xStr & """)",这样xStr 的实际值就被连接起来了。

此外,您可能希望限制可以将IFERROR 应用于同一单元格的次数,因为如果您在同一选择上多次运行您的过程,您最终会得到类似=IFERROR(IFERROR(IFERROR(... 的内容,这可能不是你想要的。

我还将变量重命名为更有意义的名称。例如,我会将xAddress 重命名为currentSelectionAddress

以上都可以总结为:

Option Explicit

Sub test_IFERROR()
    Dim selectedRange As Range
    Dim formulaRange As Range
    Dim cell As Range
    Dim currentSelectionAddress As String
    Dim blankReplacement As String
    Dim appScreenUpdate As Boolean
    
    On Error Resume Next
    currentSelectionAddress = Application.ActiveWindow.RangeSelection.Address
    Set selectedRange = Application.InputBox("Please select a range", "Kutools for Excel", currentSelectionAddress, Type:=8)
    Set formulaRange = selectedRange.SpecialCells(xlCellTypeFormulas)
    On Error GoTo 0
    If formulaRange Is Nothing Then Exit Sub
    
    blankReplacement = Application.InputBox("Replace blank cells with what?", "Kutools for Excel")
    appScreenUpdate = Application.ScreenUpdating
    Application.ScreenUpdating = False
    For Each cell In formulaRange
        cell.Formula = "=IFERROR(" & Right(cell.Formula, Len(cell.Formula) - 1) & ",""" & blankReplacement & """)"
    Next cell
    Application.ScreenUpdating = appScreenUpdate
End Sub

我不明白您为什么要求用户替换空白单元格,因为您的代码不检查公式是否返回空白值。我在想你是在替换错误而不是空白,所以也许你应该用"Replace error cells with what?" 更新"Replace blank cells with what?"

【讨论】:

  • 非常感谢。我以前从未尝试过 VBA 编码,所以我试图将一些代码组合在一起来制作我需要的模块。这就是为什么它有这么多错误。将空白单元格替换为是由于粘贴了另一个模块的代码。
  • @Malikh1517 祝你的项目好运!一定要通过 VBA 编辑器(Tools\Option\Editor\Require Variable Declaration)打开Option Explicit,以便编译器警告帮助您解决问题。由于您是该网站的新手,请考虑将我的答案标记为已接受的答案,或者甚至可以通过按答案左上角的向上箭头来投票。我这样说并不是为了获得声誉积分,而是因为如果您使用此网站,您应该养成投票和接受答案的习惯,以奖励努力做出回应的人。
  • 另外,很可能会有更多人回答同一个问题,因此每个答案的赞成票数量将使最佳答案浮出水面,它可以帮助除 OP 之外的更多人(原始海报)。
  • 我不能在这个阶段投票,因为我的声誉低于 15,是的!你的回答很有用。我今天注册了帐户,出于上述模块的目的,我无法找出代码中的错误。你的帮助对我帮助很大。
猜你喜欢
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 2016-10-26
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多