搜索选定的单元格,如果它们包含字母则隐藏它们

时间:2016-01-24 10:53:01

标签: excel vba

我想循环遍历细胞并寻找字母。如果它们包含该字母,则使用NumberFormat隐藏单元格。这有效,但我如何使这个循环可以删除,所以我可以隐藏/取消隐藏。

Private Sub CommandButton1_Click()
Dim rng As Range, cell As Range
Set rng = Range("D5:F35,D43:F73,J5:L35,J43:L73,P5:R35,P43:R73,V5:X35,V43:X73,AB5:AD35,AB43:AD73,AH5:AJ35,AH43:AJ73")
For Each cell In rng
        If InStr(1, cell.Value, "A") > 0 Then cell.NumberFormat = ";;;"
        If InStr(1, cell.Value, "B") > 0 Then cell.NumberFormat = ";;;"
        If InStr(1, cell.Value, "C") > 0 Then cell.NumberFormat = ";;;"
        If InStr(1, cell.Value, "D") > 0 Then cell.NumberFormat = ";;;"
        If InStr(1, cell.Value, "F") > 0 Then cell.NumberFormat = ";;;"
Next cell
End Sub

3 个答案:

答案 0 :(得分:1)

我不确定“toggeable”是什么意思 如果你想取消隐藏所有东西,无论它包含什么,那么只需将整个范围的.numberformat属性设置为General。

如果您的意思是当您从单元格中删除其中一个目标字母时,它应该被取消隐藏,那么请尝试以下宏:

编辑编辑通过切换添加我认为您的意思。

========================================

Option Explicit
Option Compare Binary
Private Sub CommandButton1_Click()
Dim rng As Range, cell As Range
Set rng = Range("D5:F35,D43:F73,J5:L35,J43:L73,P5:R35,P43:R73,V5:X35,V43:X73,AB5:AD35,AB43:AD73,AH5:AJ35,AH43:AJ73")

Application.FindFormat.NumberFormat = ";;;"
Set cell = rng.Find(what:="*", searchformat:=True)
    If Not cell Is Nothing Then
        rng.NumberFormat = "General"
        Exit Sub
    End If

For Each cell In rng
       If cell.Value Like "*[ABCDEF]*" Then
            cell.NumberFormat = ";;;"
        Else
            cell.NumberFormat = "General"
        End If
Next cell
End Sub

====================================

答案 1 :(得分:0)

...试

.op {
 float: right;
 margin-top: 100px;
 display: block;
 margin: 0 auto;
}

答案 2 :(得分:0)

数字格式的最后一部分用于文本 - 只需从自定义数字格式中删除该部分即可隐藏/显示文字。

Sub HideText()

    Dim rng As Range
    Set rng = ThisWorkbook.Worksheets("Sheet1") _
        .Range("D5:F35,D43:F73,J5:L35,J43:L73,P5:R35,P43:R73,V5:X35,V43:X73,AB5:AD35,AB43:AD73,AH5:AJ35,AH43:AJ73")

    If rng.NumberFormat <> "#,##0;-#,##0;#,##0;" Then
        rng.NumberFormat = "#,##0;-#,##0;#,##0;"
    Else
        rng.NumberFormat = "#,##0;-#,##0;#,##0;@"
    End If

End Sub