OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 439
- Office Version
- 2019
- Platform
- Windows
Hello and thanks in advance for any assistance provided. I have written a function to group rows, but how can I modify it to avoid the error:
"Run-time error '13': mismatch." on the line :
within the function:
I am attempting to take a specific set of rows which have been grouped as one already:
to get this final output.
I tried to modify the following code which was pretty slick, but it would not work for me: Quick VBA/Macro to group columns based on cell values in a row header
Code is as follows:
"Run-time error '13': mismatch." on the line :
VBA Code:
Set RngGrpLast = RngGrp.Find(What:=GrpValue, After:=RngGrp(i - 1), SearchDirection:=xlPrevious)
within the function:
VBA Code:
Function GroupRowsF(ShtName As String, ClmRngLet As String, GrpRowStart As Long, GrpRowEnd As Long) As String
I am attempting to take a specific set of rows which have been grouped as one already:
to get this final output.
I tried to modify the following code which was pretty slick, but it would not work for me: Quick VBA/Macro to group columns based on cell values in a row header
Code is as follows:
VBA Code:
Sub TestGroupRowsFunction()
Dim iLoop As Long
Dim LR As Long
Dim GrpRowStart As Long
Dim GrpRowEnd As Long
Dim ShtName As String
Dim GroupRows As String
Dim ClmRngLet As String
GrpRowStart = 14
GrpRowEnd = 20
ClmRngLet = "C"
ShtName = ActiveSheet.name
GroupRows = GroupRowsF(ShtName, ClmRngLet, GrpRowStart, GrpRowEnd)
End Sub
'****************************************************************************************************
'This function groups rows
Function GroupRowsF(ShtName As String, ClmRngLet As String, GrpRowStart As Long, GrpRowEnd As Long) As String
'Function variable
'ShtName as String - the Sheet where the grouping needs to take place
'ClmRngLet As String - the Column Letter of the Range where thr grouping is focused
'GrpRowStart As Long - starting row of the rows to be grouped
'GrpRowEnd As Long - end row of the rows to be grouped. If this is 0, it will find the _
value within this function by use of the last row function - LastRowF
'Dimensioning
Dim i As Long
Dim LR As Long
Dim FRGrp As Long 'first row of the group, but will not be grouped
Dim SRGrp As Long 'starting row to group
Dim LRGrp As Long 'ending row to group
Dim ShtNameActv As String
Dim GrpValue As String
Dim RngGrp As Range
Dim RngGrpLast As Range
'Code for the grouping
'Set the Range
Set RngGrp = Range(ClmRngLet & GrpRowStart & ":" & ClmRngLet & GrpRowEnd)
'Group settings
With Sheets(ShtName).Outline
.AutomaticStyles = False
.SummaryRow = xlAbove
.SummaryColumn = xlLeft
End With
'Loop to perform grouping
With Sheets(ShtName)
For i = GrpRowStart To GrpRowEnd
FRGrp = i
SRGrp = FRGrp + 1
GrpValue = Range(ClmRngLet & i)
Set RngGrpLast = RngGrp.Find(What:=GrpValue, After:=RngGrp(i - 1), _
SearchDirection:=xlPrevious)
LRGrp = RngGrpLast.Row
If FRGrp <> LRGrp Then Rows(SRGrp & ":" & LRGrp).Group
SRGrp = LRGrp + 1
i = LRGrp
Next i
End With
GroupRowsF = "Done"
End Function