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 :)
Related
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
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!
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
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 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}.