excel A列单元格与B1至B7中,有相同数值时,自动隐藏该行怎么设置,,

如题所述

可以利用VBA实现当A列单元格与B1至B7中,有相同数值时,自动隐藏该行。

软件版本:office2007

举例说明如下:

1.当A列单元格与B1至B7中,有相同数值时,自动隐藏该行

2.Alt+F11,输入代码如下:

3.F5执行代码,结果如下:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-04-16
Sub s()
    Set d = CreateObject("scripting.dictionary")
    For i = 1 To 7
        d(Cells(i, 2).Text) = ""
    Next
    Debug.Print d.Count
    n = Cells(Rows.Count, 1).End(3).Row
    For i = 1 To n
        If d.exists(Cells(i, 1).Text) Then
            Rows(i).Hidden = True
        End If
    Next
End Sub

追问

如果要把A列改成别的列数是在哪个改,,

如果反过来呢,,把没有等于B1:B7的行隐藏,,可以吗。。?

追答Sub s()
    Const co=1'第几列就把1改成几
    Set d = CreateObject("scripting.dictionary")
    For i = 1 To 7
        d(Cells(i, 2).Text) = ""
    Next
    Debug.Print d.Count
    n = Cells(Rows.Count, co).End(3).Row
    For i = 1 To n
        If d.exists(Cells(i, co).Text) Then'隐藏没有的这行改成If Not d.exists(Cells(i, co).Text) Then
            Rows(i).Hidden = True
        End If
    Next
End Sub

本回答被网友采纳
第2个回答  2016-04-16

Sub a()

    Set d = CreateObject("Scripting.dictionary")

    For Each sh In Range("b1:g1")

        d(sh.Value) = ""

    Next

    For Each sh In Range(Cells(1, 1), Cells(Cells(Rows.Count, "a").End(3).Row, "a"))

        If d.exists(sh.Value) Then Rows(sh.Row & ":" & sh.Row).EntireRow.Hidden = True

    Next

End Sub

附件上传失败就不上传了

追问

如果要把A列改成别的列数是在哪个改,,

如果反过来呢,,把没有等于B1:B7的行隐藏,,可以吗。。?

追答

如果要把A列改成别的列数是在哪个改,如果是h列,就这样改

For Each sh In Range(Cells(1, "h"), Cells(Cells(Rows.Count, "h").End(3).Row, "h"))
如果要把A列改成别的列数是在哪个改,如果反过来呢,,把没有等于B1:B7的行隐藏就这样改
Sub a()
Set d = CreateObject("Scripting.dictionary")
For Each sh In Range("b1:g1")
d(sh.Value) = ""
Next
For Each sh In Range(Cells(1, "h"), Cells(Cells(Rows.Count, "h").End(3).Row, "h"))
If not d.exists(sh.Value) Then Rows(sh.Row & ":" & sh.Row).EntireRow.Hidden = True
Next
End Sub

第3个回答  2016-04-16
建议用VBA代码追问

我知道需要VBA代码,,可问题怎么设置。。

追答

可以帮你设置,但需要你将问题描述得细致一些。

追问

比如A列中的任一一行或多行等于B1至G1中的任一个单元格数值,则隐藏该行

追答

【A列中的任一单元格】究竟是与【B1:B7】比较,还是与【B1:G1】比较?!两次的描述自相矛盾啊!!!

追问

B1至B7

追答Sub 隐藏行()
For i = 1 To [a65536].End(3).Row
    If Application.CountIf(Range("b1:b7"), Cells(i, 1)) > 0 Then Rows(i).Hidden = True
Next
End Sub

追问

如果要把A列改成别的列数是在哪个改,,

哦,,知道了,,

如果反过来呢,,把没有等于B1:B7的行隐藏,,可以吗。。?

追答

把没有等于B1:B7的行隐藏,只需将代码中的大于号>改为等号=

追问

如果同时要多列呢,,

比如,,ABC这3列,,

追答Sub 隐藏行()
For Each rng In Range("a:c")
    If Application.CountIf(Range("b1:b7"), rng) > 0 Then Rows(rng.Row).Hidden = True
Next
End Sub

追问

再问一个,,A1单元格为12.34怎么取个位数2呢,,

=RIGHT(A1,3)

这公式可以,,但是如果A1为12.3时就不行了,,

本回答被提问者采纳
相似回答