Checking one box unchecks the adjacent box in Excel - excel

I am currently trying work on a code that can loop through all the check boxes and uncheck a box if the box next to it is checked.
Currently, I have something written out, and I know it's no where near what I need it to be. I know how to do it individually by box:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
CheckBox2.Value = False
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
CheckBox1.Value = False
End If
End Sub
However, I have several hundred lines of check boxes that I'd have to write this for so if I could just loop through the check boxes it would be a life saver!So I started out with:
Dim i As Integer
i = 1
a = 2
If CheckBoxi.Value = True Then
CheckBoxa.Value = False
End If
If CheckBoxa.Value = True Then
CheckBoxi.Value = False
End If
i = i + 2
a = a + 2
End
However this doesn't seem to work and I have no idea where to go from here. Any help would be greatly appreciated!

Related

Grey out optionbuttons when a specific option button is selected

I asked a similar question this morning and i got a great answer, but then i pushed a little bit further and this is where i'm kinda stuck.
This is my initial post : Unselect an entire optionbutton group if another optionbutton outside the group is selected
But now, I would like to have this:
If OptionButton1.Value = True or OptionButton2.Value = True then Grey out OptionButton4 to OptionButton11 (Also gave it a group name : "Category").
But, If OptionButton3.Value = True, then it ungreys the OptionButton4 to OptionButton11 (Group Category).
EDIT:
I did something and it worked but once I run my userform and generate a number (userform purpose), the optionbuttons stay greyed out. Here's the code i used :
Private Sub OptionButton1_Click()
OptionButton4.Enabled = False
OptionButton5.Enabled = False
OptionButton6.Enabled = False
OptionButton7.Enabled = False
OptionButton8.Enabled = False
OptionButton9.Enabled = False
OptionButton10.Enabled = False
OptionButton11.Enabled = False
End Sub
Private Sub OptionButton2_Click()
OptionButton4.Enabled = False
OptionButton5.Enabled = False
OptionButton6.Enabled = False
OptionButton7.Enabled = False
OptionButton8.Enabled = False
OptionButton9.Enabled = False
OptionButton10.Enabled = False
OptionButton11.Enabled = False
End Sub
EDIT 2:
My solution for now is to add
Unload Me
UserForm1.Show
so it reset my userform and remove the greyed optionbuttons
That looks like you solved your own issue. Another would be to create a validation sub that would actually check everything every time an option is selected. Also there is a way to access all the forms controls something like Forms('myform').controls('mycontrol') I admit I am rusty but if you iterate through you can disable enable as wanted and call that on load
Instead of unloading and reloading the form, handle the enabling/disabling.
Create a common procedure to handle the enabling/disabling so that you do not have to duplicate the code.
You can loop through all the controls which will not only reduce the lines of the code but will be easier to maintain.
Is this what you are trying?
Option Explicit
Private Sub OptionButton1_Click()
EnableOptBtns False
End Sub
Private Sub OptionButton2_Click()
EnableOptBtns False
End Sub
Private Sub OptionButton3_Click()
EnableOptBtns True
End Sub
Private Sub EnableOptBtns(enable As Boolean)
Dim ctl As Control
Dim i As Long
For i = 4 To 11
Me.Controls("OptionButton" & i).Enabled = enable
Next i
End Sub

Excel: Making different rows read only using multiple check box

This is my first program in VBA.
I have an excel sheet which contains multiple questions and each question has a check box to make it editable or read only.
Here below an example
How many cars you own?
How many free coupon you have?
So introducing two check boxes, using that I can make them read only or editable.
So I have tried a vba code to do the same(by Googling). Here below is the code snippet.
Private Sub CheckBox13_Click()
If Sheet3.CheckBox13.Value = False Then
Sheet3.Range("B20:CZ20").Interior.ColorIndex = 16
Range("B20:CZ20").Locked = True
ActiveSheet.Protect Contents:=True
Else
ActiveSheet.Protect Contents:=False
Range("B20:CZ20").Locked = False
Sheet3.Range("B20:CZ20").Interior.ColorIndex = 0
End If
End Sub
Private Sub CheckBox14_Click() 'eigth question Hide check box code
If Sheet3.CheckBox14.Value = False Then
Sheet3.Range("B21:CZ21").Interior.ColorIndex = 16
Range("B21:CZ21").Locked = True
ActiveSheet.Protect Contents:=True
Else
ActiveSheet.Protect Contents:=False
Range("B21:CZ21").Locked = False
Sheet3.Range("B21:CZ21").Interior.ColorIndex = 0
End If
End Sub
My problem is:
Default both rows are editable and both check boxes unchecked
Now I check the first check box, so first row color changed and
became read only.
Now I check the second check box. Getting an error.
error 1004, Application defined or object defined error.
Let me know if I missed out any basic information in order to understand the problem.
Since we don't know where the problem happen, I would try this :
Private Sub CheckBox13_Click()
If Sheet3.CheckBox13.Value = False Then
Sheet3.Protect Contents:=False
Sheet3.Range("B21:CZ21").Locked = False
Sheet3.Range("B20:CZ20").Interior.ColorIndex = 16
Sheet3.Range("B20:CZ20").Locked = True
Sheet3.Protect Contents:=True
Else
Sheet3.Protect Contents:=False
Sheet3.Range("B20:CZ20").Locked = False
Sheet3.Range("B20:CZ20").Interior.ColorIndex = 0
End If
End Sub
Private Sub CheckBox14_Click()
If Sheet3.CheckBox14.Value = False Then
Sheet3.Protect Contents:=False
Sheet3.Range("B21:CZ21").Locked = False
Sheet3.Range("B21:CZ21").Interior.ColorIndex = 16
Sheet3.Range("B21:CZ21").Locked = True
Sheet3.Protect Contents:=True
Else
Sheet3.Protect Contents:=False
Sheet3.Range("B21:CZ21").Locked = False
Sheet3.Range("B21:CZ21").Interior.ColorIndex = 0
End If
End Sub
Details :
I've added Sheet3 before your ranges, (and replaced ActiveSheet by Sheet3) to be sur you're not modifying another Sheet.
I've added ActiveSheet.Protect Contents:=False - Sheet3.Range("B21:CZ21").Locked = False before modifying Range colors to be sure that the Range is not protected.

I want to disable a checkbox when another checkbox is clicked in excel

I want to click a check box and have another linked check box be disabled in Excel.
When I uncheck that particular check box, then the disabled check box should become enabled.
I tried so many things but not able to find the solution. Can I do this without a script?
I have 76 check boxes to work upon. So is this possible without a script? TIA
With two ActiveX check boxes the code would be:
Private Sub CheckBox1_Click()
If CheckBox2.Enabled = True Then
CheckBox2.Enabled = False
Else:
CheckBox2.Enabled = True
End If
End Sub
It's simple, but works.
If you want the code to check and disable clicking on the second box, use the below code. If you uncheck the initial box, it will also enable and uncheck the second one.
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
CheckBox2.Value = True
CheckBox2.Enabled = False
Else
CheckBox2.Value = False
CheckBox2.Enabled = True
End If
End Sub
more directly
Sub CheckBox1_Click()
CheckBox2.Enabled = Not CheckBox2.Enabled
End Sub
I belive you could use "OptionButton's". You don't need code for them. But in that way you'll be able to check only one of 76. And what do you mean by "linked check box"?
Option Compare Database
Private Sub Ck1_Click()
If Ck1 = True Then
Ck2.Enabled = False
Ck3.Enabled = False
Else
Ck2.Enabled = True
Ck3.Enabled = True
End If
End Sub
Private Sub Ck2_Click()
If Ck2 = True Then
Ck1.Enabled = False
Ck3.Enabled = False
Else
Ck1.Enabled = True
Ck3.Enabled = True
End If
End Sub
Private Sub Ck3_Click()
If Ck3 = True Then
Ck1.Enabled = False
Ck2.Enabled = False
Else
Ck1.Enabled = True
Ck2.Enabled = True
End If
End Sub

excel vba slowing excel down, causing 10 second egg timer delay when clicking anywhere on sheet

I am using the following vba codes which im using to hide a set of rows and unhide rows depending on if a cell contains text or not, and they are causing my excel spreadsheet to be slow and unresponsive and causing the egg timer to show for about 10 seconds.
If I take the code out It speeds things up so what can I do to my codes to get them to speed up and not take so long? perhaps there is a better way of structuring the code but im really new to vba so am not sure what I would need to do, would appreciate someone's help thanks.
the reason I am using worksheet change and worksheet selection change is so that whether a user clicks on a cell or not the page still updates
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("K22").Value <> "" Then
Application.ScreenUpdating = False
Rows("25:38").EntireRow.Hidden = False
Rows("40:48").EntireRow.Hidden = True
ElseIf Range("K22").Value = "" Then
Rows("25:38").EntireRow.Hidden = True
Rows("40:48").EntireRow.Hidden = False
End If
Application.ScreenUpdating = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("K22").Value <> "" Then
Application.ScreenUpdating = False
Rows("25:38").EntireRow.Hidden = False
Rows("40:48").EntireRow.Hidden = True
ElseIf Range("K22").Value = "" Then
Rows("25:38").EntireRow.Hidden = True
Rows("40:48").EntireRow.Hidden = False
End If
Application.ScreenUpdating = True
End Sub
The main issue is from the Worksheet_Change event, but it could be applied to any event.
The worksheet change is triggering each time you hide a column, so it's trying several times to hide the same columns, before (eventually) failing with an out of memory error:
Hide these columns... Oh, a worksheet change... Hide these columns... Oh, A worksheet change... Hide th...
To avoid this, you need to use
Application.EnableEvents = False
when you decide you are going to make changes, and
Application.EnableEvents = True
when done.
You may also want to put some error handling that turns the events on again, as if something else occurs that stops the code from running, the triggers will be turned off, and the spreadsheet will no longer update as you expect it to.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
If Range("K22").Value <> "" Then
Rows("25:38").Hidden = False
Rows("40:48").Hidden = True
Else
Rows("25:38").Hidden = True
Rows("40:48").Hidden = False
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
This works instantly for me:
Application.ScreenUpdating = False
Select Case Range("K22")
Case Is <> ""
Rows("25:38").Hidden = False
Rows("40:48").Hidden = True
Case Else
Rows("25:38").Hidden = True
Rows("40:48").Hidden = False
End Select
Application.ScreenUpdating = True

Excel VBA Macros Using Checkboxes To Hide Rows - If Else Statements

I have 60 check boxes on one worksheet. They are form control checkboxes. They all do a similar function which is hide rows. When you click the check box it shows the rows
When unchecked the rows are hidden. Is there an easy if else or case statement I could write for this:
Sub CheckBox1_Click()
If Range("B4").Value = True Then
Rows("5:62").EntireRow.Hidden = False
Else
Rows("5:62").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox2_Click()
If Range("B63").Value = True Then
Rows("64:102").EntireRow.Hidden = False
Else
Rows("64:102").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox3_Click()
If Range("B103").Value = True Then
Rows("104:129").EntireRow.Hidden = False
Else
Rows("104:129").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox4_Click()
If Range("B130").Value = True Then
Rows("131:160").EntireRow.Hidden = False
Else
Rows("131:160").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox5_Click()
If Range("B161").Value = True Then
Rows("162:183").EntireRow.Hidden = False
Else
Rows("162:183").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox6_Click()
If Range("B184").Value = True Then
Rows("185:222").EntireRow.Hidden = False
Else
Rows("185:222").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox7_Click()
If Range("B223").Value = True Then
Rows("224:244").EntireRow.Hidden = False
Else
Rows("224:244").EntireRow.Hidden = True
End If
End Sub
........etc etc
Also, if I need to add more rows in or delete rows, how would I code that in?
I would appreciate a format that I could use to create my own check boxes in the future without having to manually fix and edit them individually
You could use something like this:
Sub CheckBox1_Click()
HideRows "B63", "64:102"
End Sub
Sub HideRows(controllerRange, rowRange)
Rows(rowRange).EntireRow.Hidden = Not Range(controllerRange).Value
End Sub
You could also call HideRows directly from the checkbox.
In CheckBox1 assign macro, you would enter the following:
'HideRows "B63","64:102"'
Note: The single quotes MUST be entered as shown.

Resources