首页 > 解决方案 > 使用 VBA 查找具有单独 EM 破折号的范围内的所有单元格并删除这些单元格

问题描述

我已经搜索了很长时间,但没有找到简单的方法来做到这一点。

我有一个包含正数和负数的表格以及大量带有单独的 Em 破折号(或等效项)的单元格,就像看起来像破折号的字符一样。

我想选择一个范围并删除所有此类单元格。

我想使用 Len(<2) 但仍然找不到使用 VBA 的简单方法。

嗨,为了替换某些类型的破折号,我使用了下面的代码。但我不知道如何只选择带有单独破折号的单元格。选择一个长度会找到单元格,但它可能不是一个完美的搜索方式。

Dim LastRow As Integer
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Range("B1:K" & LastRow).Select
Cells.Replace What:=ChrW(8722), Replacement:=Chr(45), lookat:=xlPart
Cells.Replace What:=Chr(150), Replacement:="", lookat:=xlPart
End Sub

谢谢

尼尔

标签: excelvba

解决方案


    Sub RemoveDashes()
     Dim m, r, cell As Object
     Dim b As Boolean

     Set m = Sheets("Sheet1").Range("B1")
     Do
      Set m = m.Offset(1, 0)
      Set r = m.Resize(25, 1)
      b = False
      For Each cell In r
       If cell.Value <> "" Then
        b = True
       End If
      Next cell

'covers em, en, and hyphen:
      If m.Value = "—" Or m.Value = "–" Or m.Value = "-" Then m.Value = ""
     Loop Until b = False
    End Sub

推荐阅读