I have the following code:
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
Select Case Range("B14")
Case "Medium Risk"
Rows("6:12").EntireRow.Hidden = True
Case Else
Rows("6:12").EntireRow.Hidden = False
End Select
Application.EnableEvents = True
End Sub
What I need is: to hide Group Box "content" based on the value in B14.
Group Box 15 surrounds a CheckBox8_Click.
Any ideas?
Thanks
I decided that I will simply settle with hiding a row [based on cell criteria] where the object is:
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
Select Case Range("B14")
Case "Medium Risk"
Rows("6:12").EntireRow.Hidden = True
Rows("24").EntireRow.Hidden = True
Case Else
Rows("6:12").EntireRow.Hidden = False
Rows("24").EntireRow.Hidden = False
End Select
Application.EnableEvents = True
End Sub
Related
I have optionbutton1 and optionbutton2.
I also have a 8 others optionbuttons grouped under the groupname "category".
I want to deselect any optionbutton from the groupname "category" If OptionButton1.Value = True Or Optionbutton2.Value = True.
I tried this, but it's doesn't work and it's way too long. There must be another way
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Or OptionButton2.Value = True Then OptionButton4.Value = False And OptionButton5.Value = False And OptionButton6.Value = False And _
OptionButton7.Value = False And OptionButton8.Value = False And OptionButton9.Value = False And OptionButton10.Value = False And OptionButton11.Value = False
End Sub
First, there's no need for your If statement, since the first condition will always evaluate to True when OptionButton1 is clicked. Secondly, each statement that sets an option button to False should be on a separate line, and without the And operator. Try the following instead...
Private Sub OptionButton1_Click()
OptionButton4.Value = False
OptionButton5.Value = False
OptionButton6.Value = False
OptionButton7.Value = False
OptionButton8.Value = False
OptionButton9.Value = False
OptionButton10.Value = False
OptionButton11.Value = False
End Sub
However, here's an alternative...
Private Sub OptionButton1_Click()
Dim ctrl As MSForms.Control
For Each ctrl In Me.Controls
If TypeName(ctrl) = "OptionButton" Then
If ctrl.GroupName = "Category" Then
ctrl.Value = False
End If
End If
Next ctrl
End Sub
Actually, since you want to reset the "Category" option buttons when OptionButton1 or OptionButton2 is clicked, try the following instead...
Private Sub OptionButton1_Click()
Call ResetCategoryOptionButtons
End Sub
Private Sub OptionButton2_Click()
Call ResetCategoryOptionButtons
End Sub
Private Sub ResetCategoryOptionButtons()
Dim ctrl As MSForms.Control
For Each ctrl In Me.Controls
If TypeName(ctrl) = "OptionButton" Then
If ctrl.GroupName = "Category" Then
ctrl.Value = False
End If
End If
Next ctrl
End Sub
Hope this helps!
My problem is that my code does not pick up the "Medium" variable
If Range("B24").Value = "Medium" Then
Sheets("Sheet2").Visible = True
Can you please advise what's wrong with it ("Standard" should uncover rows, "Medium" should keep them hidden but unhide the Sheet2, High again keeps the rows hidden but unhides Sheet2, if (B24) is empty it should keep rows and Sheet2 hidden:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("B24").Value = "Standard" Then
Sheets("Sheet2").Visible = False
End If
If Range("B24").Value = "Medium" Then
Sheets("Sheet2").Visible = True
End If
If Range("B24").Value = "High" Then
Sheets("Sheet2").Visible = True
End If
Else
Sheets("Sheet2").Visible = False
End If
If Range("B24").Value = "Standard" Then
Rows("29:47").EntireRow.Hidden = False
Else
Rows("29:47").EntireRow.Hidden = True
End If
End Sub
You are obviously missing an If, and your code should not be working. This is how it looks like, when it is formatted:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("B24").Value = "Standard" Then
Sheets("Sheet2").Visible = False
End If
If Range("B24").Value = "Medium" Then
Sheets("Sheet2").Visible = True
End If
If Range("B24").Value = "High" Then
Sheets("Sheet2").Visible = True
End If
'Is this Else an IF?
Else
Sheets("Sheet2").Visible = False
End If
If Range("B24").Value = "Standard" Then
Rows("29:47").EntireRow.Hidden = False
Else
Rows("29:47").EntireRow.Hidden = True
End If
End Sub
If you omit the If-Else, the code would look a bit better with a Select-Case statement:
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Range("B24")
Case "Standard"
Sheets("Sheet2").Visible = False
Rows("29:47").EntireRow.Hidden = False
Case "Medium"
Sheets("Sheet2").Visible = True
Rows("29:47").EntireRow.Hidden = True
Case "High"
Sheets("Sheet2").Visible = True
Rows("29:47").EntireRow.Hidden = True
Case Else
Sheets("Sheet2").Visible = False
Rows("29:47").EntireRow.Hidden = True
End Select
End Sub
You can probably go one step further, by writing Rows("29:47").EntireRow.Hidden = True before the select case and remove this line from cases, thus following the Do-Not-Repeat-Yourself principle. But in this case it is probably not needed.
If you want to add a second range, something like this is possible:
Select Case True
Case Range("B24") = "Standard"
Case Range("B26") = "Medium"
End Select
However, it would evaluate just once, thus if both are true, B26 is not going to happen.
I'm having trouble adding another function in that macro I used to activate or deactivate columns in a Excel Workbook we are using at work. I'm getting the Out of stack space error when running it just adding one of the following function.
Mostly, I've used an If( ;1;0) to manage the activation part and an If(;TRUE;FALSE) for the locked/unlocked part. The function I want to had is based on the same idea using a verification code to Clearcontents of a cell and locked it. If the verification code is false, then, I want the cell to be unlocked so the user can write the value. Here is the code line I want to had times 15 as already done for the locked, unlocked function.
If Range("AS16") = "Vrai" Then
Range("AA16").ClearContents
Range("AA16").Locked = True
Else:
Range("AA16").Locked = False
End If
Here is the code I'm using right now.
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect ("Francis")
Dim Cell As Range
Set Cell = ActiveCell
Application.ScreenUpdating = False
For Each cellule In Range("S50:X50")
If cellule.Value = "1" Then cellule.EntireColumn.Hidden = False
Next cellule
For Each cellule In Range("S50:X50")
If cellule.Value = "0" Then cellule.EntireColumn.Hidden = True
Next cellule
For Each cellule In Range("I50:J50")
If cellule.Value = "1" Then cellule.EntireColumn.Hidden = False
Next cellule
For Each cellule In Range("I50:J50")
If cellule.Value = "0" Then cellule.EntireColumn.Hidden = True
Next cellule
If Range("AR16") = "Vrai" Then
Range("K16").Locked = False
Range("O16").Locked = False
Else:
Range("K16").Locked = True
Range("O16").Locked = True
End If
If Range("AR18") = "Vrai" Then
Range("K18").Locked = False
Range("O18").Locked = False
Else:
Range("K18").Locked = True
Range("O18").Locked = True
End If
If Range("AR20") = "Vrai" Then
Range("K20").Locked = False
Range("O20").Locked = False
Else:
Range("K20").Locked = True
Range("O20").Locked = True
End If
If Range("AR22") = "Vrai" Then
Range("K22").Locked = False
Range("O22").Locked = False
Else:
Range("K22").Locked = True
Range("O22").Locked = True
End If
If Range("AR24") = "Vrai" Then
Range("K24").Locked = False
Range("O24").Locked = False
Else:
Range("K24").Locked = True
Range("O24").Locked = True
End If
If Range("AR26") = "Vrai" Then
Range("K26").Locked = False
Range("O26").Locked = False
Else:
Range("K26").Locked = True
Range("O26").Locked = True
End If
If Range("AR28") = "Vrai" Then
Range("K28").Locked = False
Range("O28").Locked = False
Else:
Range("K28").Locked = True
Range("O28").Locked = True
End If
If Range("AR30") = "Vrai" Then
Range("K30").Locked = False
Range("O30").Locked = False
Else:
Range("K30").Locked = True
Range("O30").Locked = True
End If
If Range("AR32") = "Vrai" Then
Range("K32").Locked = False
Range("O32").Locked = False
Else:
Range("K32").Locked = True
Range("O32").Locked = True
End If
If Range("AR34") = "Vrai" Then
Range("K34").Locked = False
Range("O34").Locked = False
Else:
Range("K34").Locked = True
Range("O34").Locked = True
End If
If Range("AR36") = "Vrai" Then
Range("K36").Locked = False
Range("O36").Locked = False
Else:
Range("K36").Locked = True
Range("O36").Locked = True
End If
If Range("AR38") = "Vrai" Then
Range("K38").Locked = False
Range("O38").Locked = False
Else:
Range("K38").Locked = True
Range("O38").Locked = True
End If
If Range("AR40") = "Vrai" Then
Range("K40").Locked = False
Range("O40").Locked = False
Else:
Range("K40").Locked = True
Range("O40").Locked = True
End If
If Range("AR42") = "Vrai" Then
Range("K42").Locked = False
Range("O42").Locked = False
Else:
Range("K42").Locked = True
Range("O42").Locked = True
End If
If Range("AR44") = "Vrai" Then
Range("K44").Locked = False
Range("O44").Locked = False
Else:
Range("K44").Locked = True
Range("O44").Locked = True
End If
Application.ScreenUpdating = True
Application.Goto Cell
'ActiveSheet.Protect Password:="Francis"
End Sub
Thanks a lot for your help.
Have a nice day!
You typically don't want to have performance-expensive code running in that specific handler. Worksheet_Change gets invoked every time a cell changes... and that includes changing a cell's Locked property value.
So that's how you run out of stack space: your handler is modifying cells' Locked state, which triggers the Worksheet_Change event, which modifies cells' Locked state, which triggers the Worksheet_Change event, which modifies cells' Locked state, which triggers the Worksheet_Change event, which... which eventually blows the call stack.
So prevent this accidental recursion, you need to prevent Excel from firing worksheet events when you're handling one:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo CleanFail
Application.EnableEvents = False
'do stuff
CleanExit:
Application.EnableEvents = True
Exit Sub
CleanFail:
'handle errors here...
Resume CleanExit
End Sub
As for simplifying the code, that's more of a mandate for Code Review Stack Exchange, once your code works as intended.
If any C# dev is reading this, this particular situation now has an up-for-grabs issue on Rubberduck's GitHub repository: #3109 Prevent accidental recursion in Worksheet_Change and Workbook_SheetChange handlers; once that inspection is implemented, Rubberduck will be able to warn you when you handle Worksheet_Change without disabling application events.
The Out of stack error is caused by the Change event, as noted by #Mat
Try this version which also turns the events off and on
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cellule As Range, r As Long, isVrai As Boolean
ActiveSheet.Unprotect "Francis"
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
For Each cellule In Union(Range("I50:J50"), Range("S50:X50"))
With cellule
Select Case .Value2
Case "1": .EntireColumn.Hidden = False
Case "0": .EntireColumn.Hidden = True
End Select
End With
Next
For r = 16 To 44 Step 2
isVrai = (Range("AR" & r).Value2 = "Vrai")
Range("K" & r).Locked = Not isVrai
Range("O" & r).Locked = Not isVrai
If isVrai Then Range("AR" & r).ClearContents
Next
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
'ActiveSheet.Protect Password:="Francis"
End Sub
This section can be simplified:
For Each cellule In Range("S50:X50")
If cellule.Value = "1" Then cellule.EntireColumn.Hidden = False
Next cellule
For Each cellule In Range("S50:X50")
If cellule.Value = "0" Then cellule.EntireColumn.Hidden = True
Next cellule
For Each cellule In Range("I50:J50")
If cellule.Value = "1" Then cellule.EntireColumn.Hidden = False
Next cellule
For Each cellule In Range("I50:J50")
If cellule.Value = "0" Then cellule.EntireColumn.Hidden = True
Next cellule
To the following (however, note that this will unhide any non-zero values).
For Each cellule in Range("S50:X50")
cellule.EntireColumn.Hidden = (cellule.Value = "0")
Next
For Each cellule in Range("I50:J50")
cellule.EntireColumn.Hidden = (cellule.Value = "0")
Next
And this section:
If Range("AR16") = "Vrai" Then
Range("K16").Locked = False
Range("O16").Locked = False
Else:
Range("K16").Locked = True
Range("O16").Locked = True
End If
If Range("AR18") = "Vrai" Then
Range("K18").Locked = False
Range("O18").Locked = False
Else:
Range("K18").Locked = True
Range("O18").Locked = True
End If
....
Can be simplified using a loop over Range("AR16:AR44")
For Each cellule in Range("AR16:AR44") Step 2
cellule.Offset(,-33).Locked = (cellule.Value = "Vrai")
cellule.Offset(,-29).Locked = (cellule.Value = "Vrai")
Next
I was wondering if there was a way to optimize this set of code
Sub BBG_Transmital()
'Hides Sections Not Used in Expanded Form
Dim CollapseRange1 As Range
Dim CollapseRange2 As Range
'Set Which Range/Cell to Associate with Marked CheckBox
With ActiveDocument.Tables(3)
Set CollapseRange1 = .Rows(10).Range
CollapseRange1.End = .Rows(13).Range.End
End With
With ActiveDocument.Tables(3)
Set CollapseRange2 = .Rows(16).Range
CollapseRange2.End = .Rows(21).Range.End
End With
'If Box is Checked then CollapseRanges
If CheckBox1.Value = True Then
CollapseRange1.Font.Hidden = True
'If Box is Not Checked then UncollapseRange
Else
CollapseRange1.Font.Hidden = False
End If
'If Box is Checked then CollapseRanges
If CheckBox1.Value = True Then
CollapseRange2.Font.Hidden = True
'If Box is Not Checked then UncollapseRange
Else
CollapseRange2.Font.Hidden = False
End If
End Sub
I tried using
If CheckBox1.Value = True Then
CollapseRange1.Font.Hidden = True AND CollapseRange2.Font.Hidden = True
Else
CollapseRange1.Font.Hidden = False And CollapseRange2.Font.Hidden = False
But it didn't work. I was just wondering if I can shorten my IF statement into one rather then two.
Thanks!
Here is your same code... just condensed to remove unnecessary checks
Sub BBG_Transmital()
'Hides Sections Not Used in Expanded Form
Dim CollapseRange1 As Range
Dim CollapseRange2 As Range
'Set Which Range/Cell to Associate with Marked CheckBox
With ActiveDocument.Tables(3)
Set CollapseRange1 = .rows(10).Range
CollapseRange1.End = .rows(13).Range.End
Set CollapseRange2 = .rows(16).Range
CollapseRange2.End = .rows(21).Range.End
End With
'If Box is Checked then CollapseRanges
If CheckBox1.Value = True Then
CollapseRange1.Font.Hidden = True
CollapseRange2.Font.Hidden = True
Else 'If Box is Not Checked then UncollapseRange
CollapseRange1.Font.Hidden = False
CollapseRange2.Font.Hidden = False
End If
End Sub
Further condensing Wayne's answer:
This part:
'If Box is Checked then CollapseRanges
If CheckBox1.Value = True Then
CollapseRange1.Font.Hidden = True
CollapseRange2.Font.Hidden = True
Else 'If Box is Not Checked then UncollapseRange
CollapseRange1.Font.Hidden = False
CollapseRange2.Font.Hidden = False
End If
Can be written as:
CollapseRange1.Font.Hidden = CheckBox1.Value
CollapseRange2.Font.Hidden = CheckBox1.Value
Also note, If {boolean-expression} = True can always be rewritten as If {boolean-expression}, and If {boolean-expression} = False can always be rewritten as If Not {boolean-expression}.
I have everything setup with different options for forecasts. I have done this in the past at a different job and I can't figure out where I am going wrong.
Private Sub ComboBox1_Change()
If ComboBox1.Value = "2 Weeks" Then
Columns("J:L").Select
Selection.EntireColumn.Hidden = False
Columns("M:R").Select
Selection.EntireColumn.Hidden = True
End If
If ComboBox1.Value = "6 Weeks" Then
Columns("M:O").Select
Selection.EntireColumn.Hidden = False
Columns("J:L").Select
Selection.EntireColumn.Hidden = True
Columns("P:R").Select
Selection.EntireColumn.Hidden = True
End If
If ComboBox1.Value = "12 Weeks" Then
Columns("P:R").Select
Selection.EntireColumn.Hidden = False
Columns("J:O").Select
Selection.EntireColumn.Hidden = True
End If
End Sub
It seems like your code should work just fine. I rewrote it to be more concise and it worked perfectly (with an Active-X control).
Private Sub ComboBox1_Change()
Select Case ComboBox1.Value
Case "2 Weeks"
Columns("J:L").Hidden = False
Columns("M:R").Hidden = True
Case "6 Weeks"
Columns("J:L").Hidden = True
Columns("M:O").Hidden = False
Columns("P:R").Hidden = True
Case "12 Weeks"
Columns("J:O").Hidden = True
Columns("P:R").Hidden = False
End Select
End Sub
In the title I included it was a form control. – phentrus 8 mins ago
The reason why I was confused and asked you in the comments above whether you are using a form control or an ActiveX control is because your quesiton title says "Form" but the code is for ActiveX.
For Form Control, paste this code in a module.
Option Explicit
Sub DropDown1_Change()
Dim DDown As Shape
Set DDown = ActiveSheet.Shapes(Application.Caller)
Select Case DDown.ControlFormat.List(DDown.ControlFormat.ListIndex)
Case "2 Weeks"
Columns("J:L").Hidden = False
Columns("M:R").Hidden = True
Case "6 Weeks"
Columns("J:L").Hidden = True
Columns("M:O").Hidden = False
Columns("P:R").Hidden = True
Case "12 Weeks"
Columns("J:O").Hidden = True
Columns("P:R").Hidden = False
End Select
End Sub
Next right click your Form Combobox and assign the above macro to it :)