我对VBA很新,我尝试在Excel中创建代码,根据范围内的值(例如0' s)选择行,并使用该组将它们组合在一起在Excel中的功能。在下面的链接捕获中,您可以看到我从哪里开始(捕获1)到我想去的地方(捕获2)。
下面的代码是我所选择的所有行选择0的行。不幸的是,当选择多个不在一起的行时,我不能使用Group函数。
我不太确定要添加到代码中的内容以使其分组第一组0,然后移动到下一组0并执行相同操作直到达到范围结束。我也希望它跳过空白单元格,例如附加屏幕截图的第13行中的空白单元格。
Sub SelectGroupRows0()
Dim c As Range
Dim rng0 As Range
Dim block As Range
Set block = Range("B4:B23")
For Each c In block
If c = 0 Then
If rng0 Is Nothing Then Set rng0 = c.EntireRow
Set rng0 = Union(rng0, c.EntireRow)
Else: End If
Next c
rng0.Select
Selection.Rows.Group
End Sub
对此的任何帮助将不胜感激。
提前致谢,
Q
答案 0 :(得分:1)
You had a few Else If
not in the right place.
Also, you need to loop through the Union
Range Areas
to group each one of them.
Note: You don't need to use rng0.Select
and later Selection.Rows.Group
.
Code
Option Explicit
Sub SelectGroupRows0()
Dim c As Range
Dim rng0 As Range
Dim block As Range
Set block = Range("B4:B23")
For Each c In block
If Not IsEmpty(c) And c.Value = 0 Then
If rng0 Is Nothing Then
Set rng0 = c
Else
Set rng0 = Union(rng0, c)
End If
End If
Next c
' loop through your Range's Areas and group each one of them
For Each c In rng0.Areas
c.EntireRow.Group
Next c
End Sub