Update Spin Button values before hide or unhide

Justplainj

New Member
Joined
Apr 15, 2021
Messages
47
Office Version
  1. 365
Platform
  1. Windows
Good day All.

I am using the following code
VBA Code:
Private Sub SpinButton1_Change()

x = Sheet7.Range("P75")
y = Sheet7.Range("P76")
Sheet7.SpinButton1.SmallChange = x
Sheet7.SpinButton1.Max = y

If SpinButton1.Value = SpinButton1.Max Then ' this checks if the spinbutton is the last row or not. if it is it will exit the macro keeping all of the lines unhidden
Exit Sub
Else

Rows(SpinButton1.Value & ":" & SpinButton1.Max).Hidden = True
Rows(SpinButton1.Min & ":" & SpinButton1.Value).Hidden = False
Range("S" & SpinButton1.Value + 1 & ":" & "S" & SpinButton1.Max).ClearContents 'this clear the data validation of hidden cells so that when it is unhidden it shows nothing and a new selection can be made.
Range("P75").Select

End If

End Sub

I use it for a spin button to hide or unhide rows from row 77 to a max value entered by the user seen as "y" by the amount of rows "x"
When the value of x = 5 it will hide or unhide 5 rows, but if the value of x changes to 1, the first time you select the spin button 5 rows are hidden or unhidden and only after that initial 5 it then changes to 1.
Is it possible to amend the code to immediately only hide or unhide only 1 row instead of 5 on first click and then 1 on second onward?

I was thinking of added the x and y update to a seperate sub as a change event as per the below but that is not working.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

x = Sheet7.Range("P75")
y = Sheet7.Range("P76")
Sheet7.SpinButton1.SmallChange = x
Sheet7.SpinButton1.Max = y

End Sub

Thank you
J
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Update:

I amended the change event to
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

x = Sheet7.Range("P75")
y = Sheet7.Range("P76")
Sheet7.SpinButton1.SmallChange = x
Sheet7.SpinButton1.Max = y

End Sub

When the value for x or y is entered into the cell and enter is pressed it updates and works as intended.

If anyone has a suggestion for a cleaner code that I can learn from that would be great.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,523
Messages
6,120,033
Members
448,940
Latest member
mdusw

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