Hide / Unhide entire rows based on check box selection - vba

I would like to hide specific rows based on whether a check box is ticked.
I have this code but it does not seem to work.
I right clicked on the check box, and assigned this macro:
Private Sub CheckBox68_Click()
If CheckBox68 = True Then
[24:26].EntireRow.Hidden = False
End If
End Sub
I am not sure why it does not work. Can anyone see something wrong with it?
Many thanks in advance
Ab

There is no point in your code where you hide the rows.
You need to to modify it as below:
Private Sub CheckBox68_Click()
[24:26].EntireRow.Hidden = Not CheckBox68.value
End Sub

Related

How to Delete Text in Word Using Active X Controls

I am currently attempting to create an automated letter on word and know essentially nothing about activex or vba.
I want to create a checkbox that will show text (a subsection of text) only if it is selected. I had originally used a code like this to hide or show text.
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
ActiveDocument.Bookmarks("TextToHide").Range.Font.Hidden = False
Else
ActiveDocument.Bookmarks("TextToHide").Range.Font.Hidden = True
End If
End Sub
However, since my letter had numbered bullets, depending on which checkbox was selected the order of the letter is completely messed up.
Example: I do not want subsections 1,3,5,6 included in the letter (and it is not) but the bullet's order look like this
Subsection...
Subsection...
Subsection...
I figured the only way to get around this is to completely delete the subsection so that the Bullet numbers are in the correct order. I attempted to write the following code but it will not delete the subsection/bookmarked text.
Sub DeleteBookmark()
If CheckBox1.Value = False Then
ActiveDocument.Bookmarks("TextToHide").Delete
End If
End Sub
Could anyone please help me delete the bookmarked text or update the bullet's numbers when hiding a subsection of the letter?
Try to use the Delete method of the Range class instead:
Sub DeleteBookmark()
If CheckBox1.Value = False Then
ActiveDocument.Bookmarks("TextToHide").Range.Delete
End If
End Sub

Can you reference the control name within a sub in vba?

So I'm attempting to filter a large section of data in a spreadsheet with checkboxes. So far around 80 individual checkboxes. I'm wondering if there is anyway to refer to the checkbox (or any other control) name within the sub, as a specific reference, i.e. thiscontrol.name. At present the first checkbox reads:
Private Sub_F1_Click()
StringVariableForLaterUse ="F1"
If F1.Value = True Then
'Display Data Relevant to F1
End If
End
I'm wondering if I can use
StringVariableForLaterUse = ThisControl.Name
and
If ThisControl.Value = True Then
I'd then be able to replicate this a further 78 times. Currently pure laziness factor, however I want my Subs to be self sufficient as possible. Any thoughts folks?
You can never use a string like an object... But, for an ActiveXcheck box, use the next workaround code.
Public chk1 As Shape
Private Sub CheckBox1_Click()
Set chk1 = ActiveSheet.Shapes(CheckBox1.Name)
MsgBox chk1.OLEFormat.Object.Object.value
End Sub
You can use now chk1 in another Sub inside the sheet module.
You can also refer to it from a module Sub, referencing the sheet, too:
Sub testSheetChk()
Debug.Print Worksheets("Sheet Name").chk1.OLEFormat.Object.Object.value
End Sub
But, this will work only after you firstly run (once) the click event, in order to alocate a value to the object variable.

VBA using drop down lists on other worksheets to hide rows automatically

Afternoon all!
I am using the results of various drop down lists to hide the relevant rows on a spreadsheet as the value on the drop down list is changed (so automatically).
With some googling, I have come across the below set up which works nicely, although I have now hit a snag when trying to specify drop down list cell from another worksheet.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim disabled As Boolean
If Range("D2") = "Yes" Then
disabled = True
End If
If disabled Then
Rows("3:8").EntireRow.Hidden = False
Else
Rows("3:8").EntireRow.Hidden = True
End If
End sub
Using the following has not worked, and googling for a solution has lead me up many a dead end:
If Sheets("Topsheet").Range("D27") = "Yes" Then
Am I unable to use values from a neighbouring sheet when declaring a variable due to it being a private sub?
Any help would be much appreciated as I have been stumped on this for a couple of hours!
Your code can be massively simplified. Try using this (You'll have to update the sheet name if it's not the same as yours)
Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("SheetWithRowsToBeHidden").Rows("3:8").EntireRow.Hidden = IIf(Me.Range("D27").Value2 = "Yes", True, False)
End Sub
Also, where have you put this code? Have you put it inside a Module or is it in the Sheet Object? It needs to be in the Sheet Object that has the dropdowns on.

How to hide unchecked checkboxes when printing

I am working on creating a contract specification page in Word 2013 with checkboxes so that my boss can click each box that he wants to include in the final printed contract, and hide the ones he doesn't need. I'm completely new to VBA but I know that I need to use it to achieve this. From searching the internet I've used bookmarks and the code below:
Private Sub CheckBox1_Click()
If CheckBox1.Value = False Then
ActiveDocument.Bookmarks("Work1").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Work1").Range.Font.Hidden = False
End If
End Sub
But this code seems to hide the checkboxes before I can click them. I would like the checkboxes to stay visible until they print, in case my boss needs to make a change. I also tried using another code, but it also didn't work the way I wanted it to:
Private Sub CheckBox1_Click()
If CheckBox1.Value = False Then
ActiveDocument.Bookmarks("Work1").Application.Options.PrintHiddenText = False
Else
ActiveDocument.Bookmarks("Work1").Application.Options.PrintHiddenText = True
End If
End Sub
I would also like to make it so there are no gaps where the unused checkboxes would be. Any help would be greatly appreciated!!
I seem to have overcomplicated the issue yesterday. Your only problem was that your checkboxes turn unvisible along with the text. You just need to bookmark exactly the text you want to hide/show leaving the checkbox (perceived by Word as a part of the text) outside of the bookmark.
Your code will do this fine, or you can replace it with this:
Private Sub CheckBox1_Click()
Bookmarks("Work1").Range.Font.Hidden = Not CheckBox1
End Sub
By the way, if you make Word show paragraph marks (Ctrl + *), you will be able to see hidden text.
I would suggest your macros:
a) save the current document (in the temporary folder if you want)
b) delete all unchecked boxes
c) print the document
d) close the document
e) open the file saved in the point a).
Should take a dozen lines of code or so.

Program Option Button in Excel to run two different macros or actions

Simple question that I can't seem to get right: I have set up a macro that hides a few cells and another one that shows those same cells. When I run them independently they work fine. But i'm trying to set an option button or a check box that will run both macros. For example: if the box is checked, it shows the cells, if it is unchecked, it hides them.
I have only been able to assign one macro on click, but this doesn't work because it doesn't take into consideration the state of the button. I've tried with an IF, like this:
Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
Columns("Q:R").Select
Selection.EntireColumn.Hidden = True
Range("A2").Select
Else
Columns("Q:R").Select
Range("R1").Activate
Selection.EntireColumn.Hidden = False
Range("A2").Select
End If
End Sub
What am I doing wrong? Thanks in advance!
Try this to toggle, starting with an unticked check box and visible columns
Sub CheckBox2_Click()
Columns("Q:R").Select
Selection.EntireColumn.Hidden = not (Selection.EntireColumn.Hidden)
Range("A2").Select
End Sub
I don't think you need to worry about the state of the button. You really just need to reverse the state on click no matter what. You can just negate whatever the current state is.
Range("Q:R").EntireColumn.Hidden = Not Range("Q:R").EntireColumn.Hidden
Range("A2").Select
That being said, the logic you have is solid. What do you mean when you say it's not working? Does it never work, or only the first time you click the checkbox?