如果匹配条件,则清除表的单元格值

时间:2014-04-13 18:34:18

标签: excel vba

我希望对特定范围的细胞进行分类,并查看该细胞的LEN(gth)是否等于20.如果是,我想清除所述细胞的内容并移至接下来,直到他们都被评估。

我有这个报告是从一个网站上提取出来并导出到excel而烦恼是0.0%,显示为英镑符号(#x的20)。我已经尝试过其他搜索字符串的宏,它完全忽略了0%,一个常量就是每个出现的单元格长度正好是20个字符。

我应该做的是:

1  Searching C3:U20 
2  for cell.len = 20 
3  if activecell = matches criteria 
4  then clear.contents
5  Goto 1 until all activecells with a len of 20 are cleared

感谢您提供任何帮助。

*的 修改 * 由于我无法做到这一点,我使用了“解决方法”。它作为所有 blarp 效率低下,但由于它仅用于小型表,因此并不重要。我只需要在“三个”单独的脚本中执行此操作。我发现如果我将范围从Percentage格式转换为General,我只能将溢出数字作为字符串查找。然后,一旦它们被“清除”,我只需将这些列重新转换为百分比格式:

Sub sub_ConvertToGeneral()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''  Convert the Percentage Columns to General Format
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Columns("F:F").Select
    'Application.FormulaBarHeight = 2
    Range("F:F,H:H,L:M,O:O,S:S,U:U").Select
    Range("U1").Activate
    Selection.NumberFormat = "General"
    'Selection.NumberFormat = "0.00%"
    Range("A1:B1").Select
End Sub

Sub sub_Overflow()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''  Find Overflow and delete every instance of it
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Dim FindString As String
    Dim rng As Range
    FindString = "2.6965E+308"
50
    If Trim(FindString) <> "" Then
        With Sheets("Main Import").Range("C1:U20")
            Set rng = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not rng Is Nothing Then
                Application.Goto rng, True
                ActiveCell.ClearContents
                Do
                Set rng = .FindNext(rng)
                GoTo 50
                Loop While Not rng Is Nothing
                Else
            End If
        End With
    End If
    Range("A1").Select
End Sub

Sub sub_ConvertToPercentage()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''  Convert the General Columns to Percentage
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Columns("F:F").Select
    'Application.FormulaBarHeight = 2
    Range("F:F,H:H,L:L,O:O,S:S,U:U").Select
    Range("U1").Activate
    'Selection.NumberFormat = "General"
    Selection.NumberFormat = "0.00%"
    Range("A1:B1").Select
End Sub

1 个答案:

答案 0 :(得分:0)

如果要清除的单元格实际上只包含20个字符(So LEN(A1)将显示20个),则选择要检查的单元格并尝试:

Sub dural()
    For Each r In Intersect(Selection, ActiveSheet.UsedRange)
    If Len(r.Text) = 20 Then
        r.Clear
    End If
    Next r
End Sub

修改#1:

This version will clear cells with a value greater than 1.0E+307:

Sub dural()
    For Each r In Intersect(Selection, ActiveSheet.UsedRange)
        If IsNumeric(r.Value) Then
            If r.Value > 1E+307 Then
                r.Clear
            End If
        End If
    Next r
End Sub