using a Cmd button's location as a relative reference

gilbertvb3

New Member
Joined
Jun 22, 2008
Messages
15
I want to use a command button that unhides the row below it. However there are going to be 50 buttons. Is there a way to use the button that was pressed as the reference to make the cell beneath it active and change the row height. I am trying to avoid using 50 different macros.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Welcome to the Board!

Take a look at the TopLeftCell Property in the VBA helpfile. You can identify the cell below each button based off of that by using Offset.

Hope that helps,
 
Upvote 0
This is what I got so far. It works on commandbutton1 only. Is there any way I can reuse this code for the rest of the buttons?


Private Sub CommandButton1_Click()

With CommandButton1.TopLeftCell
ActiveSheet.Unprotect
.Offset(1, 0).Resize(1).EntireRow.Hidden = Not .Offset(1, 0).Resize(1).EntireRow.Hidden
ActiveSheet.Protect End With
End Sub
 
Upvote 0
You could do this:
Code:
Option Explicit

Private Sub CommandButton1_Click()
    ToggleHiddenRow CommandButton1
End Sub

Private Sub CommandButton2_Click()
    ToggleHiddenRow CommandButton2
End Sub

Private Sub ToggleHiddenRow(cbo As MSForms.CommandButton)
    ActiveSheet.Unprotect
    With cbo.TopLeftCell
        .Offset(1, 0).Resize(1).EntireRow.Hidden = Not .Offset(1, 0).Resize(1).EntireRow.Hidden
    End With
    ActiveSheet.Protect
End Sub
and call ToggleHiddenRow in each of the 50 command button Click subroutines.

Alternatively, you could create a class to handle multiple command buttons and then you only need 1 command button Click handler. I've written some code based on http://www.ozgrid.com/forum/showthread.php?t=80631 for command buttons on a worksheet - let me know if you want to see it.
 
Upvote 0
Worked like a charm. Thanks a bunch. I am not very proficient in any programming language. Your a life saver.
 
Upvote 0

Forum statistics

Threads
1,215,694
Messages
6,126,255
Members
449,306
Latest member
RealNinetyThree

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