【问题标题】:Delete contents from cell, IFIsNumeric = False从单元格中删除内容,如果 IsNumber = False
【发布时间】:2017-03-14 15:51:13
【问题描述】:

所以我有这个程序;从远程系统扫描值。有时,如果网络出现故障,它可能会超时,并且会出现 #nan 错误或类似的东西。

当我尝试将该错误消息写入数据库时​​;因为它正在寻找一个数值,所以它会因为“数据类型不匹配”而出错。

为了解决这个问题,我决定尝试清理结果。所以我有下面的代码。它大部分时间都有效;我看到它失败的是,如果中途有一个数字,然后它又会出错。一旦它到达数字,它似乎会停止循环并且不会清理其他任何东西。

有人能给我的任何帮助都会很棒;也许我这样做很糟糕。我试图举例说明我想在下面看到的内容。如果您有任何建议或问题,请告诉我。

Private Sub Clean()
'Select Current Row For where to start cleaning. LoopIndex is a global variable. 
'Just giving the line of where to read from so I do not need to read the whole sheet. 
  Range("B" & LoopIndex).Select

'Start the loop, to go through the entire row.
  Do Until IsEmpty(ActiveCell)

   'Checks if it is a number value; if it is not it will clear out the data.
    If IsNumeric(ActiveCell.Value) = False Then
        ActiveCell.Value = ""
    End If
  ActiveCell.Offset(0, 1).Select
 Loop
End Sub

它看起来像什么。

|A     |B     |C     |D     |
|#error|#Error|3     |#Error|

我希望它看起来像什么。

|A     |B     |C     |D     |
|      |      |3     |      |

它在做什么。

|A     |B     |C     |D     |
|      |      |3     |#Error|

【问题讨论】:

  • 你从哪里得到LoopIndex?另外,最好avoid using .Select/.Activate
  • 这是我分配给它的一个全局变量。我在评论部分有这个。
  • @user2644176 我会小心使用 do until 循环。 IMO 指定要清洁的范围并简单地循环通过单元格来清洁它们要安全得多。你的范围是可变的吗?我的回答对你不起作用有什么原因吗?让我知道,我会帮助你让它工作。至于为什么你的不是,我会尝试除IsEmpty 之外的其他东西 - 可能想改用ActiveCell = ""。此外,它似乎在我的机器上运行良好(尽管只有一行)。

标签: excel vba loops isnumeric


【解决方案1】:

试试这个

Sub RemoveErrors(TheRange As Range)
Dim c
For Each c In TheRange
    If IsError(c) Then c.Value = ""
Next c
End Sub

编辑:没有循环的版本如果是公式错误可能会加快速度

Sub RemoveErrors(TheRange As Range)
On Error Resume Next
TheRange.SpecialCells(xlCellTypeFormulas, xlErrors).Value = vbNullString
On Error GoTo 0
End Sub

像这样使用它:

Sub Test()
RemoveErrors Range("A1:D21")
End Sub

如果您更喜欢使用IsNumeric 函数,请使用此函数:

Sub RemoveNAN(TheRange As Range)
Dim c
For Each c In TheRange
    If IsNumeric(c) = False Then c.Value = ""
Next c
End Sub

【讨论】:

  • 谢谢。这似乎运作良好。范围将是可变的,这不是问题。我喜欢你在这里所做的 For Each 和 IsError。似乎比我的做法优雅得多。我很感激。
【解决方案2】:

这就是我最终这样做的方式,因为变量范围在不同的工作表上。我发布这个只是为了给其他人一个未来的解决方案。

感谢大家对此的帮助。

 Private Sub Clean()

Dim StartRow As String
Dim LastCol As Long
Dim EndRow As String
Dim StartCol As String
Dim MyCol As String

StartCol = "B"


With ActiveSheet
        LastCol = .Cells(LoopIndex, .Columns.Count).End(xlToLeft).Column
End With

MyCol = GetColumnLetter(LastCol)


RemoveErrors Range(StartCol & LoopIndex & ":" & MyCol & LoopIndex)


End Sub

'Does the replacing
Sub RemoveErrors(TheRange As Range)
Dim c
For Each c In TheRange
    If IsError(c) Then c.Value = ""
Next c
End Sub

'Gets the Column Letter
Function GetColumnLetter(colNum As Long) As String
    Dim vArr
    vArr = Split(Cells(1, colNum).Address(True, False), "$")
    GetColumnLetter = vArr(0)
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多