在VBA分组行(Grouping Rows in VBA)

您所在的位置:网站首页 excel按范围分组 在VBA分组行(Grouping Rows in VBA)

在VBA分组行(Grouping Rows in VBA)

#在VBA分组行(Grouping Rows in VBA)| 来源: 网络整理| 查看: 265

我有下面的代码似乎并不奏效。 从本质上讲, rngList是指在Excel中定义的名称范围为约500行和长每n行数有文本(大约有32行指出,具有文本500的)。 我试图去非空白单元格(通过模仿ctrl + down在Excel命令)。

我检查,看看他们是否是空白的,如果他们是我想组细胞。 如果不是空白的,我要检查电池的左,如果是0,我也想组吧。 我现在的代码基本上是试图做到这一点,但我收到以下错误:

Group Method of Range Class Failed

它接着强调以下行:

Selection.Rows.Group

编辑:比方说,而不是分组是空行,我想,在他们有1组行。 这样的CRTL +向下实际上会转到该细胞,而不是最后一排。

非常感谢你的帮助!

代码如下:

rngList.Cells(1).Select i = 0 Do While i < 32 i = i + 1 If Selection.Value = "" Then Selection.Rows.Group Else Selection.End(xlToLeft).Select If Selection.Value 0 Then Selection.Rows.ClearOutline End If End If Selection.End(xlToRight).Select Selection.End(xlDown).Select Loop Answer 1:

尽管这篇文章的时候,我想我会在我的两分钱抛出任何人在其谁可能绊倒。 我希望我正确地理解你的问题。 下面是我收集:

目标 :对于感兴趣的列中的每一行的基础上,标准组行。

标准 :唯一rows in the group是那些要么没有值(空白,null,为空),或具有一个值,并有一个相邻小区(直接向左),其具有为0的唯一的值rows not in the group是那些不是空白和具有不为0的相邻小区。

下面是一些示例数据:

注:在范围B1:B12化妆命名范围rngList ,如OP说,他们有。

数据运行宏之前:

数据运行宏后-分组未收缩:

数据运行宏后-分组折叠:

处理这个的代码:

为了使此代码的工作:在VBE(Visual Basic编辑器),打开包含数据组的工作表(也包含命名范围rngList )并粘贴此代码,然后运行宏。

注意:评论添加进一步详细解释某些部分,但我相信自己的代码写入的方式,可以解释本身(例如变量名是有意义的,逻辑是有道理的)。

Public Sub GroupCells() Dim myRange As Range Dim rowCount As Integer, currentRow As Integer Dim firstBlankRow As Integer, lastBlankRow As Integer Dim currentRowValue As String Dim neighborColumnValue As String 'select range based on given named range Set myRange = Range("rngList") rowCount = Cells(Rows.Count, myRange.Column).End(xlUp).Row firstBlankRow = 0 lastBlankRow = 0 'for every row in the range For currentRow = 1 To rowCount currentRowValue = Cells(currentRow, myRange.Column).Value neighborColumnValue = Cells(currentRow, myRange.Column - 1).Value If (IsEmpty(currentRowValue) Or currentRowValue = "") Then 'if cell is blank and firstBlankRow hasn't been assigned yet If firstBlankRow = 0 Then firstBlankRow = currentRow End If ElseIf Not (IsEmpty(currentRowValue) Or currentRowValue = "") Then 'if the cell is not blank and its neighbor's (to the left) value is 0, 'and firstBlankRow hasn't been assigned, then this is the firstBlankRow 'to consider for grouping If neighborColumnValue = 0 And firstBlankRow = 0 Then firstBlankRow = currentRow ElseIf neighborColumnValue 0 And firstBlankRow 0 Then 'if firstBlankRow is assigned and this row has a value with a neighbor 'who isn't 0, then the cell one row above this one is to be considered 'the lastBlankRow to include in the grouping lastBlankRow = currentRow - 1 End If End If 'if first AND last blank rows have been assigned, then create a group 'then reset the first/lastBlankRow values to 0 and begin searching for next 'grouping If firstBlankRow 0 And lastBlankRow 0 Then Range(Cells(firstBlankRow, myRange.Column), Cells(lastBlankRow, myRange.Column)).EntireRow.Select Selection.Group firstBlankRow = 0 lastBlankRow = 0 End If Next End Sub

Answer 2:

我用萨姆的代码组不使用A列思考别人可能觉得它有用。

Sub Group_Jobs() Dim myRange As Range Dim rowCount As Integer, currentRow As Integer Dim firstBlankRow As Integer, lastBlankRow As Integer Dim currentRowValue As String Dim nextRowValue As String Application.ScreenUpdating = False 'Stop screen updating while grouping 'select range based on given named range Set myRange = Range("A1:A1000") rowCount = Cells(Rows.Count, myRange.Column).End(xlUp).Row firstBlankRow = 0 lastBlankRow = 0 'for every row in the range For currentRow = 1 To rowCount currentRowValue = Cells(currentRow, myRange.Column).Value nextRowValue = Cells(currentRow + 1, myRange.Column).Value 'Assign firstBlankRow & lastBlankRow 'if currentRowValue = NotBlank(Job#) And nextRowValue = NotBlank(Job#) Then Skip 'if currentRowValue = Blank And nextRowValue = Blank Then Skip 'if currentRowValue = NotBlank(Job#) And nextRowValue = Blank Then is firstBlankRow 'if currentRowValue = Blank And nextRowValue = NotBlank(Job#) Then is lastBlankRow If Not (currentRowValue = "" Or currentRowValue = "") Then If (IsEmpty(nextRowValue) Or nextRowValue = "") Then firstBlankRow = currentRow + 1 End If ElseIf (IsEmpty(currentRowValue) Or currentRowValue = "") Then If Not (IsEmpty(nextRowValue) Or nextRowValue = "") Then If firstBlankRow 0 Then lastBlankRow = currentRow End If End If End If Debug.Print "Row " & currentRow; ": firstBlankRow: " & firstBlankRow; ", lastBlankRow: " & lastBlankRow 'Group firstBlankRow & lastBlankRow 'if first & last blank rows have been assigned, create a group If firstBlankRow 0 And lastBlankRow 0 Then 'Debug.Print "Row: " & currentRow; ", Outline Level: " & ActiveSheet.Rows(currentRow).OutlineLevel If Not ActiveSheet.Rows(currentRow).OutlineLevel > 1 Then 'Ignore if last row is already grouped Range(Cells(firstBlankRow, myRange.Column), Cells(lastBlankRow, myRange.Column)).EntireRow.Select Selection.Group End If firstBlankRow = 0: lastBlankRow = 0 'reset the first/lastBlankRow values to 0 End If Next Application.ScreenUpdating = True 'Start screen updating as macro is complete End Sub

文章来源: Grouping Rows in VBA


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3