Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3066|回复: 6

[已解决]用鼠标选择单元格事件来实现整行隐藏(类似组及分类显示功能)

[复制链接]
发表于 2013-3-10 10:44 | 显示全部楼层 |阅读模式
话不多说,直接上附件
另附代码如下
Private Sub Worksheet_SelectionChange(ByVal Target As Range) '鼠标选择单元格事件
Dim x As Integer, r As Integer, z As Integer
x = Target.Row
r = Sheets(1).Cells.Find("*", , , , , xlPrevious).Row
z = x + 1
If x > 1 And Cells(x, 3) = "" And Cells(x, 1) <> "" Then
    If Cells(x, 1) = "节" Then
        Do
            z = z + 1
            If Cells(z, 1) <> "" Then
                Exit Do
            End If
        Loop Until z = r
        If z = r Then
            Range(Cells(x + 1, 1), Cells(z, 1)).Select
            If Selection.RowHeight = 0 Then
                Selection.Rows.AutoFit
            Else
                Selection.RowHeight = 0
            End If
        Else
            Range(Cells(x + 1, 1), Cells(z - 1, 1)).Select
            If Selection.RowHeight = 0 Then
                Selection.Rows.AutoFit
            Else
                Selection.RowHeight = 0
            End If
        End If
    ElseIf Cells(x, 1) = "章" Then
        Do
            z = z + 1
            If Cells(z, 1) = "章" Then
                Exit Do
            End If
        Loop Until z = r
        If z = r Then
            Range(Cells(x + 1, 1), Cells(z, 1)).Select
            If Selection.RowHeight = 0 Then
                Selection.Rows.AutoFit
            Else
                Selection.RowHeight = 0
            End If
        Else
            Range(Cells(x + 1, 1), Cells(z - 1, 1)).Select
            If Selection.RowHeight = 0 Then
                Selection.Rows.AutoFit
            Else
                Selection.RowHeight = 0
            End If
        End If
    ElseIf Cells(x, 1) = "册" Then
        Do
            z = z + 1
            If Cells(z, 1) = "册" Then
                Exit Do
            End If
        Loop Until z = r
        If z = r Then
            Range(Cells(x + 1, 1), Cells(z, 1)).Select
            If Selection.RowHeight = 0 Then
                Selection.Rows.AutoFit
            Else
                Selection.RowHeight = 0
            End If
        Else
            Range(Cells(x + 1, 1), Cells(z - 1, 1)).Select
            If Selection.RowHeight = 0 Then
                Selection.Rows.AutoFit
            Else
                Selection.RowHeight = 0
            End If
        End If
    Else
    End If
End If
End Sub


最佳答案
2013-3-10 14:29
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)    '鼠标选择单元格事件
  2.    
  3.     'iEndRow 数据最后行
  4.     'iCurRow 当前所选行
  5.     Dim iCurRow As Integer, iEndRow As Integer, i As Integer
  6.    
  7.     If Target.Count > 1 Then Exit Sub
  8.    
  9.     iEndRow = UsedRange.Rows.Count
  10.     iCurRow = Target.Row
  11.    
  12.     If iCurRow = 1 Then Exit Sub
  13.     If iCurRow >= iEndRow Then Exit Sub
  14.    
  15.     Select Case Cells(iCurRow, 1)
  16.         Case "章"
  17.             i = iCurRow + 1
  18.             Do While Cells(i, 1) <> "章" And i <= iEndRow
  19.                 i = i + 1
  20.             Loop
  21.             Rows(iCurRow + 1 & ":" & i - 1).Hidden = Not Rows(iCurRow + 1).Hidden

  22.         Case "节"
  23.             i = iCurRow + 1
  24.             Do While Len(Cells(i, 1)) = 0 And i <= iEndRow
  25.                 i = i + 1
  26.             Loop
  27.             Rows(iCurRow + 1 & ":" & i - 1).Hidden = Not Rows(iCurRow + 1).Hidden
  28.         End Select
  29. End Sub
复制代码

新建 Microsoft Office Excel 工作表.rar

10.16 KB, 下载次数: 23

例子

 楼主| 发表于 2013-3-10 10:45 | 显示全部楼层
本帖最后由 fgq0 于 2013-3-10 15:49 编辑

根据3楼大大的代码终于实现了我的目的了,传上附件,请大家挑挑毛病,找找BUG,最好是能在优化优化。


回复

使用道具 举报

发表于 2013-3-10 14:29 | 显示全部楼层    本楼为最佳答案   
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)    '鼠标选择单元格事件
  2.    
  3.     'iEndRow 数据最后行
  4.     'iCurRow 当前所选行
  5.     Dim iCurRow As Integer, iEndRow As Integer, i As Integer
  6.    
  7.     If Target.Count > 1 Then Exit Sub
  8.    
  9.     iEndRow = UsedRange.Rows.Count
  10.     iCurRow = Target.Row
  11.    
  12.     If iCurRow = 1 Then Exit Sub
  13.     If iCurRow >= iEndRow Then Exit Sub
  14.    
  15.     Select Case Cells(iCurRow, 1)
  16.         Case "章"
  17.             i = iCurRow + 1
  18.             Do While Cells(i, 1) <> "章" And i <= iEndRow
  19.                 i = i + 1
  20.             Loop
  21.             Rows(iCurRow + 1 & ":" & i - 1).Hidden = Not Rows(iCurRow + 1).Hidden

  22.         Case "节"
  23.             i = iCurRow + 1
  24.             Do While Len(Cells(i, 1)) = 0 And i <= iEndRow
  25.                 i = i + 1
  26.             Loop
  27.             Rows(iCurRow + 1 & ":" & i - 1).Hidden = Not Rows(iCurRow + 1).Hidden
  28.         End Select
  29. End Sub
复制代码
回复

使用道具 举报

发表于 2013-3-10 14:40 | 显示全部楼层
册的我没加了。只需把CASE “章" 代码借用下,把章改为册就成了。
回复

使用道具 举报

 楼主| 发表于 2013-3-10 15:51 | 显示全部楼层
hwc2ycy 发表于 2013-3-10 14:40
册的我没加了。只需把CASE “章" 代码借用下,把章改为册就成了。

请问这一句怎么翻译啊?
Rows(iCurRow + 1 & ":" & i - 1).Hidden = Not Rows(iCurRow + 1).Hidden
意思我懂,要达到什么目的我也知道,但是不明白这句话翻译的话该怎么翻译呢?
回复

使用道具 举报

发表于 2013-3-10 16:19 | 显示全部楼层
fgq0 发表于 2013-3-10 15:51
请问这一句怎么翻译啊?
Rows(iCurRow + 1 & ":" & i - 1).Hidden = Not Rows(iCurRow + 1).Hidden
意思 ...

Range.Hidden 属性

返回或设置一个 Variant 值,它指明是否隐藏行或列。语法
表达式.Hidden
表达式   一个代表 Range 对象的变量。
说明

将此属性设置为 True 以隐藏行或列。指定的区域必须占据整个行或整个列

先取原来的隐藏值,再进行逻辑非操作

NOT TRUE = FALSE
NOT FALSE =TRUE


回复

使用道具 举报

 楼主| 发表于 2013-3-10 19:42 | 显示全部楼层
哦了。。。。才知道有这么个属性。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-24 18:46 , Processed in 0.741582 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表