如果使用excel vba包含字符串值,如何删除给定范围内的单元格值

时间:2016-01-19 09:50:56

标签: excel vba excel-vba

如果单元格值是字符串(不是特定的字符串,通常是任何字符串),有人可以告诉我如何删除给定范围内的单元格值 这是我到目前为止所尝试的:

  Dim UsedCell_Total, myRange
  UsedCell_Total = Sheets("Sheet1").Cells(1, 1).SpecialCells(xlLastCell).Row


   For Each cell In Range("A3:A" & UsedCell_Total)
   If (cell.value) = "*" Then  ' I dono't know what condition to give here

   cell.Delete shift:=xlRight
   End If
   Next cell

2 个答案:

答案 0 :(得分:1)

您可以将IsNumeric()Not运算符一起使用来反转逻辑:

Dim UsedCell_Total As Long
Dim myRange        As Excel.Range
Dim cell           As Excel.Range

    UsedCell_Total = Sheets("Sheet1").Cells(1, 1).SpecialCells(xlLastCell).Row

    For Each cell In Range("A3:A" & UsedCell_Total).Cells
        If Not IsNumeric(cell.Value) Then
            cell.Delete shift:=xlRight
        End If
    Next

你可能也想测试日期(日期不是用IsNumeric()捕获的)在这种情况下你会使用

If Not IsNumeric(cell.Value) And Not IsDate(cell.Value) Then

答案 1 :(得分:1)

这是实现您正在寻找的内容的另一种方式,它将捕获其中包含字符串的所有单元格。这使用TypeName函数来测试单元格内容。如果函数求值为“String”,则删除单元格

Dim UsedCell_Total as Long, cell

UsedCell_Total = Sheets("Sheet1").Cells(1, 1).SpecialCells(xlLastCell).Row

For Each cell In Range("A3:A" & UsedCell_Total).Cells
    If TypeName(cell.value) = "String" Then cell.Delete shift:=xlRight
Next cell
相关问题