Excel VBA;宏隐藏/列切换

问题描述 投票:2回答:1

需要一些指导,我在Excel中具有按层次结构排列的数据。 *或0基本上是一些数据,而x基本上是空单元格,但它表示为子数据。我想分配一个按钮,并隐藏所有数据1st并从根到子逐个显示。

类比:假设您必须看看驱动器。因此,第一,我们将访问根文件夹并列出项目。现在,从此列表中,我们将访问第一项,仅列出第二级子项。建立新列表后,我们将返回并访问第二个根项,并列出其子项(仅第二层)。我们可以继续到最后一个根文件夹。完成此操作后,我们将在3 ... 4th ... 5th..nth级别执行相同的操作。

Input:
    *           
    x   *       
    x   *       
    x   x   *   
    x   x   x   *
    x   *       
    x   *       
    0           
    x   0       
    x   0       
    x   x   0   

Output step0:
Hide all the row + column

Output step0.5:
* (root/1st column - 1st element excluding x Basically Column A)

Output step1:
* (root/1st column - 1st element excluding x)
0 (root/1st column - 2nd element excluding x)

Output step n:
* (root/1st column - 1st element excluding x)
0 (root/1st column - 2nd element excluding x)
:
:
:
:
:
y (root/1st column - n-1 element excluding x)
z (root/1st column - n element excluding x)

Output step1:
*
0

Output step2:
*   
x   *
0   

Output step3:
*   
x   *
x   *
0   

输出步骤4:*X *X *X *0

依图片依次类推:enter image description here

Output step7:
data will keep expending till the same as Input data.

Output Step8:
Data will keep shrinking in reverse order. like step 5 -> 4 -> 3 -> 2 -1.

实验代码:

Sub Hide_Next()

Dim a As Range
Dim b As Range
'Need help with dynamic range. 

    For Each a In Range("A1:A23").Cells
        For Each b In Range("B1:B23").Cells
        If a.Value <> Empty And b.Vaule = Empty Then
                a.EntireRow.Hidden = True
        End If
        Next b
    Next a

End Sub

任何输入将不胜感激。

excel vba excel-vba
1个回答
0
投票

[很抱歉,我尚未获得发表评论的权限,所以我必须将此评论保留为答案(在收到澄清后,我也会作答)。

我不明白展开的逻辑是什么:

  1. 为什么在第一步中显示2行,而不仅仅是行?在第二步,它将显示第二个(也是最后一行)没有孩子。我知道有一个解释,但没有得到解释,因此在写答案之前必须先了解一下

  2. 为什么在第4步中取消隐藏x 0(输入的第9行)而不是第三个x *(输入的第6行)?

© www.soinside.com 2019 - 2024. All rights reserved.