Optimizing VBA including AND statement within IF - vba

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}.

Related

Word VBA macro working on some computers but not other

I've written a pretty simple Word macro to hide different parts of a form based on a checkbox made at the beginning of the form. It's only working on some people's computers but not others - it's uploaded to our document server and then users download it out.
Specifically, affected users are able to click a checkbox and the macro will disable the other checkboxes, but the bookmarks remain visible. No error shows up, it just doesn't happen.
The file is downloaded correctly (.docm) and when I poke around in affected users' VBA code, nothing seems to be amiss. I haven't been able to replicate the error myself.
Below is the macro. Any help would be appreciated, as this supports a fairly important business process.
'Plan
Private Sub CheckBox1_Click()
If CheckBox2.Enabled = True Then
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
CheckBox5.Enabled = False
Else:
CheckBox2.Enabled = True
CheckBox3.Enabled = True
CheckBox4.Enabled = True
CheckBox5.Enabled = True
End If
Bookmarks("CAPA_Plan_And_Add").Range.Font.Hidden = CheckBox1.Value
End Sub
'Plan Addendum
Private Sub CheckBox2_Click()
If CheckBox1.Enabled = True Then
CheckBox1.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
CheckBox5.Enabled = False
Else:
CheckBox1.Enabled = True
CheckBox3.Enabled = True
CheckBox4.Enabled = True
CheckBox5.Enabled = True
End If
Bookmarks("CAPA_Plan_And_Add").Range.Font.Hidden = CheckBox2.Value
End Sub
'Execution
Private Sub CheckBox3_Click()
If CheckBox2.Enabled = True Then
CheckBox1.Enabled = False
CheckBox2.Enabled = False
CheckBox4.Enabled = False
CheckBox5.Enabled = False
Else:
CheckBox1.Enabled = True
CheckBox2.Enabled = True
CheckBox4.Enabled = True
CheckBox5.Enabled = True
End If
Bookmarks("CAPA_Execution").Range.Font.Hidden = CheckBox3.Value
End Sub
'Extension
Private Sub CheckBox4_Click()
If CheckBox3.Enabled = True Then
CheckBox1.Enabled = False
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox5.Enabled = False
Else:
CheckBox1.Enabled = True
CheckBox2.Enabled = True
CheckBox3.Enabled = True
CheckBox5.Enabled = True
End If
Bookmarks("CAPA_Extension").Range.Font.Hidden = CheckBox4.Value
Bookmarks("CAPA_Extension_2").Range.Font.Hidden = CheckBox4.Value
End Sub
'Cancellation
Private Sub CheckBox5_Click()
If CheckBox4.Enabled = True Then
CheckBox1.Enabled = False
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
Else:
CheckBox1.Enabled = True
CheckBox2.Enabled = True
CheckBox3.Enabled = True
CheckBox4.Enabled = True
End If
Bookmarks("CAPA_Cancellation").Range.Font.Hidden = CheckBox5.Value
Bookmarks("CAPA_Cancellation_2").Range.Font.Hidden = CheckBox5.Value
End Sub
'Effectiveness Check Yes
Private Sub CheckBox6_Click()
If CheckBox7.Enabled = True Then
CheckBox7.Enabled = False
Else:
CheckBox7.Enabled = True
End If
End Sub
'Effectiveness Check No
Private Sub CheckBox7_Click()
If CheckBox6.Enabled = True Then
CheckBox6.Enabled = False
Else:
CheckBox6.Enabled = True
End If
Bookmarks("Effectiveness_Check").Range.Font.Hidden = CheckBox7.Value
End Sub
Private Sub CheckBox9_Click()
End Sub
There are a few things wrong with this code logical & syntax wise.
Logical
Private Sub CheckBox1_Click()
If CheckBox2.Enabled = True Then
It doesn't make any sense to me why you are basing what checkboxes are Enabled based on a different checkbox than the one which was clicked; specifically without regard to the state of the checkbox that was clicked. This looks like an ugly work around for not understanding how to initialize and properly control event clicks.
Syntactically
An If Statement looks like this:
If condition Then
' Do something if true
End If
An If-Else statement is formatted like this:
If condition Then
' Do something if true
Else
' Do something else
End if
Labels, used in {On Error} Goto Label statements are formatted with a colon after a name
On Error GoTo ErrorHandler
' Some code that might produce an error
ErrorHandler:
' More code (to deal with errors)
So what you have is a normal If-Statement (without an Else component, since it has a colon after it Else:) which should enable all checkboxes.
If CheckBox2.Enabled = True Then
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
CheckBox5.Enabled = False
Else:
CheckBox2.Enabled = True
CheckBox3.Enabled = True
CheckBox4.Enabled = True
CheckBox5.Enabled = True
End If
[TL;DR]
Just remove the colon after the Else statements

Add a second bookmark to the same yes no button

I've created a yes no button that hides a bookmark, how can i add a second bookmark to the same yes no button. My Bookmarks name is TextToShow. i just want to add another bookmark to the same field.
Private Sub CheckBoxNo_Change()
Call ShowHideBookmark
End Sub
Sub ShowHideBookmark()
Dim Sterilisation As Range
Set Sterilisation = ActiveDocument.Bookmarks("TextToShow").Range
If CheckBoxNo.Value = True Then
With Sterilisation.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With Sterilisation.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = True
End With
End If
End Sub
Sub ShowHideBookmark()
Dim Sterilisation As Range, Sterilisation2 As Range
Set Sterilisation = ActiveDocument.Bookmarks("TextToShow").Range
Set Sterilisation2 = ActiveDocument.Bookmarks("TextToShow2").Range
If CheckBoxNo.value = True Then
Sterilisation.Font.Hidden = True
Sterilisation2.Font.Hidden = True
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
Sterilisation.Font.Hidden = False
Sterilisation2.Font.Hidden = False
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = True
End With
End If
End Sub
Assuming (as per JK's approach) your second bookmarked range is named 'TextToShow2', try:
Private Sub CheckBoxNo_Change()
Application.ScreenUpdating = False
Dim bVal As Boolean: bVal = CheckBoxNo.Value
ActiveDocument.Bookmarks("TextToShow").Range.Font.Hidden = bVal
ActiveDocument.Bookmarks("TextToShow2").Range.Font.Hidden = bVal
ActiveWindow.View.ShowHiddenText = Not bVal
ActiveWindow.View.ShowAll = Not bVal
Application.ScreenUpdating = True
End Sub

Unselect an entire optionbutton group if another optionbutton outside the group is selected

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!

Group Box content - hide based on cell value

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

Looking for ways to simplify my code

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