【问题标题】:Loop through cells in range to find cells manually set to blank循环遍历范围内的单元格以查找手动设置为空白的单元格
【发布时间】:2018-10-18 14:26:24
【问题描述】:

我正在尝试使用以下代码循环遍历一系列单元格,搜索包含 ="" 的单元格:

Sub ReplaceEmptyCells()
    Dim i As String
    Dim k As String
    Dim cell As Range

    i = "=" & Chr(34) & Chr(34)
    k = "#N/A"

    For Each cell In ActiveSheet.Range("C1:AA51") 'Desired range to search through
        If cell.Value = i Then
            cell.Replace What:=i, Replacement:=k, MatchCase:=True
        End If
    Next cell

End Sub

此值已在另一张纸上手动设置,并且已将相关单元格复制并粘贴为链接(这样做是为了避免空单元格在粘贴后转换为 0,同时保持原始单元格中的 0数据)。

目的是让新工作表上“空白”单元格的内容为“#N/A”。为什么上面的 Sub 不起作用?

【问题讨论】:

  • If cell.Formula= i Then
  • 然后只是Cell.Value = k
  • 这似乎不起作用。这是因为“空白”单元格已作为链接粘贴吗?有解决办法吗?
  • 调试器实际显示k= ""。奇怪。
  • If cell.Formula = i Then cell.Value = k 为我工作。我刚刚用那一行替换了你的整个 If。

标签: excel vba


【解决方案1】:

当你可以使用时为什么要循环:

ActiveSheet.Range("C1:AA51").Replace  What:=i, Replacement:=k, Lookat:=xlWhole

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2015-01-07
    相关资源
    最近更新 更多