Forum Discussion
Help creating a toggle switch cycle counter
Hi! I'm developing a spreadsheet to track gauges that are checked out from our quality department. I would like to be able to automatically count the number of times each gauge was checked out...I thought it would be easy to add a Checked In/Checked Out toggle switch to each row and then count the number of times the switch was cycled (imagine each row in the spreadsheet represents one gauge)...however this appears to be more difficult than I initially thought! Can anyone help me develop this functionality? Or, come up with a better idea? It needs to be handled automatically or behind the scenes because it's not something we want the spreadsheet users to manage. Thanks!!!
Hi Richard,
I did some searching on file uploads and found that macro enabled files are understandably blocked for security reasons. It was suggested that a zipped version would be OK.
6 Replies
- Mark FitzgeraldIron Contributor
Hi Richard,
I did some searching on file uploads and found that macro enabled files are understandably blocked for security reasons. It was suggested that a zipped version would be OK.
- Richard OckersCopper Contributor
Awesome, thanks!! I appreciate the help.
- Mark FitzgeraldIron Contributor
I can understand why creating and coding many buttons would seem daunting.
You are lucky I'm having a quiet day and enjoy challenges. Feedback and Likes will be appreciated.
My approach was to use a single ActiveX command button coded to be sensitive to the user's selection. ActiveX controls are programmable while Form controls are not.
If the selection is outside the GaugeUse table or more than one row the button is disabled and an appropriate instruction is displayed.
If the selection is a single cell or row intersecting the table, the button caption changes depending on whether the gauge is In or Out.
I've tried to attach a file but if it hasn't worked, here's a screenshot:
The code behind it should be pasted into the worksheet code sheet which you can reach by right-clicking the sheet tab and selecting View code..
Option Explicit
Private Sub cbtnCheckInOut_Click()
'The button is only enabled if a single cell or row intersecting with the GaugeUse table
'Enabled or disabled is set within the Worksheet_SelectionChange event handler below'Check if the selected Gauge is In or Out
If Cells(ActiveCell.Row, [GaugeUse[In / Out]].Column) = "In" Then
'It's In so mark it out, increment the Out count and change button caption
Cells(ActiveCell.Row, [GaugeUse[In / Out]].Column) = "Out"
Cells(ActiveCell.Row, [GaugeUse[Out count]].Column) = Cells(ActiveCell.Row, [GaugeUse[Out count]].Column) + 1
Me.cbtnCheckInOut.Caption = "Check In"
Else
'It's Out so mark it In and change button caption
Cells(ActiveCell.Row, [GaugeUse[In / Out]].Column) = "In"
Me.cbtnCheckInOut.Caption = "Check Out"
End If
End SubPrivate Sub Worksheet_SelectionChange(ByVal Target As Range)
'Intersect returns a range object if Target (selection) is within the GaugeUse table.
'If the selection is outside the GaugeUse table Intersect will return Nothing.
'If more than one row is selected, the button is disabled.
'Note: [GaugeUse] is shorthand for Range("GaugeUse") or ListObjects("GaugeUse")
If Not Intersect(Target, [GaugeUse]) Is Nothing And Target.Rows.Count = 1 Then
Me.cbtnCheckInOut.Enabled = True
'Hide the Instruct range and toggle the button caption
[Instruct].Font.Color = [Instruct].Interior.Color
If Cells(Target.Row, [GaugeUse[In / Out]].Column) = "In" Then
Me.cbtnCheckInOut.Caption = "Check Out"
Else
Me.cbtnCheckInOut.Caption = "Check In"
End If
Else
Me.cbtnCheckInOut.Enabled = False
[Instruct].Font.ColorIndex = xlAutomatic
End If
'Check if more than one row in GaugeUse table is selected and change Instruct message accordingly.
If Not Intersect(Target, [GaugeUse]) Is Nothing And Target.Rows.Count > 1 Then
[Instruct] = "Select a single ID to enable the button."
Else
[Instruct] = "Select an ID to enable the button."
End If
End Sub- Richard OckersCopper Contributor
Hi Mark! Thank you very much for your help! I can definitely see where you are heading and I think it is the right direction...but I'm having trouble implementing your solution. Perhaps I do not understand well enough...you mentioned a possible issue with your file attachment. Indeed, that appears to be the case. Therefore, if it's not too much trouble, could you implement the button/script in the test file I have attached to this comment? Then, once you have uploaded it back to this thread, I could pick it apart to better understand what you did. If that does not work, let me know and I will give you my direct email address so you can simply forward me the modified test file. Thanks so much!
- Haytham AmairahSilver Contributor
Hi Richard,
I've created a dummy example which is maybe similar to you looking for!
Please find it in the attachments.
- Richard OckersCopper ContributorHi Haytham,
Thanks for the response - your dummy example is close but not exactly what I'm looking for as I'm not looking to count how many of each type of gauge are checked out. Instead, I'm looking to count the number of times each individual gauge has been checked out. Mark Fitzgerald's response is closer (see the forum thread)...but I still need help implementing his code. I will reach out to him...thank you for your help!