需要一些指导,我在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
依图片依次类推
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
任何输入将不胜感激。
答案 0 :(得分:1)
在下面的示例代码中,我还添加了注释以供理解。
DoAStep
是您必须链接到按钮的宏。
exp_rng
是包含要扩展的“结构”的范围。您可以使用了解的结构信息编写代码,使VBA知道范围是什么。
当所有行都隐藏时,您知道必须扩展;当所有行都可见时,您知道必须回来将其隐藏;而当您处于中间步骤时,您便知道必须执行的操作只记得之前的步骤在做什么。
然后,我在模块作用域中使用is_expanding
变量:在DoAStepp调用之间它的值保持存储。但是,当您打开excel文件时,is_expanding
的默认设置为False
。如果要存储扩展/缩小方向,则必须将其存储在单元格值中。
Dim exp_rng As Range
Dim is_expanding As Boolean
Sub DoAStep()
'You can set Range one time for all if you want and you can also 'calculate' it without writing 'A1:D11' in the code
Set exp_rng = Range("A1:D11")
'Direction engine: when expanding calls expand-step.
'If the expand step doesn't find anything to expand then reverse the direction
'Same for reduction
If is_expanding Then
If Not ExpandOneStep Then
is_expanding = False
ReduceOneStep
End If
Else
If Not ReduceOneStep Then
is_expanding = True
ExpandOneStep
End If
End If
End Sub
Function ExpandOneStep() As Boolean
Dim i_row As Long, i_col As Long
'Loop through cells, from top to bottom, from left to right
'exp_rng(i_row, i_col) is the cell I'm looking
For i_col = 1 To exp_rng.Columns.Count
For i_row = 1 To exp_rng.Rows.Count
'When we are expanding we search for hidden cell ( = cells in an hidden row) to unhide
If exp_rng(i_row, i_col).EntireRow.Hidden = True Then
'Check if the cell has a value (I'm checking if it's not empty and not "x", you can change with whatever you want
If Not IsEmpty(exp_rng(i_row, i_col).Value) And exp_rng(i_row, i_col).Value <> "x" Then
'Unhide the row and exit the function so that we will not unhide other rows (leaving them for next steps)
exp_rng(i_row, i_col).EntireRow.Hidden = False
'Exit the function returning TRUE in order to say "I found something to unhide".
'When we don't find anything to unhide then we have to change expanding with reducing
ExpandOneStep = True
Exit Function
End If
End If
Next i_row
Next i_col
'If the for loop ends then we have not found any row to unhide so we
'have to switch to 'reducing time' instead of expanding
ExpandOneStep = False
End Function
Function ReduceOneStep() As Boolean
Dim i_row As Long, i_col As Long
'ReduceOneStep is like ExpandOneStep but:
' - Loop from right to left, from bottom to top (reverse loop)
' - Check for unhidden rows to hide
For i_row = exp_rng.Rows.Count To 1 Step -1
For i_col = exp_rng.Columns.Count To 1 Step -1
If exp_rng(i_row, i_col).EntireRow.Hidden = False Then
If Not IsEmpty(exp_rng(i_row, i_col).Value) And exp_rng(i_row, i_col).Value <> "x" Then
exp_rng(i_row, i_col).EntireRow.Hidden = True
ReduceOneStep = True
Exit Function
End If
End If
Next i_col
Next i_row
ReduceOneStep = False
End Function