Collapse single group row VBA

marcop2906

New Member
Joined
Mar 2, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone, I'm new in the forum and also in the VBA world of excel. I am looking for a method to close a specific group row (the group already exists), on two separate sheets. The row number is calculated in the cells of the respective sheets (respectively AU7 and B1). Using these code syntax always gives me error. I would like to ask for your help, thank you

VBA Code:
Sub closeLastRow()

Dim X As String

X = Range("AU7").Value

Rows(X).EntireRow.ShowDetail = False

End Sub

VBA Code:
Sub closeLastRowSintesi()

Dim X As String

X = Range("B1").Value

Rows(X).EntireRow.ShowDetail = False

End Sub
Icona di Verificata con community
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
There may be some syntax error that I'm not seeing, but it works fine for me. It works fine only when the number is a reference to a row that is grouped. As such I would recommend data validation; provided is my data validation version of one of your methods.
VBA Code:
Sub closeLastRowSintesi()

    Dim X As String

    X = Range("B1").Value

    If Not X = "" And Rows(X).EntireRow.OutlineLevel > 1 Then
            Rows(X).EntireRow.ShowDetail = False
    End If
End Sub
 
Upvote 0
Solution
There may be some syntax error that I'm not seeing, but it works fine for me. It works fine only when the number is a reference to a row that is grouped. As such I would recommend data validation; provided is my data validation version of one of your methods.
VBA Code:
Sub closeLastRowSintesi()

    Dim X As String

    X = Range("B1").Value

    If Not X = "" And Rows(X).EntireRow.OutlineLevel > 1 Then
            Rows(X).EntireRow.ShowDetail = False
    End If
End Sub
Thank you mackc557, I found the error was not in the vba code above but in the group. The related code did not create the groups correctly and therefore occasionally coincided with the line with the group and other times it did not. Thanks so much for the answer and for the eye-opening code.
 
Upvote 0

Forum statistics

Threads
1,215,840
Messages
6,127,219
Members
449,370
Latest member
kaiuuu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top