Expand specific headings in Word VBA - O365 ProPlus - text

I have checkboxes for different answers to questions in a document. Based on what is checked, I want to expand a more detailed section of questions for the user. I have the If statement for the checkboxes correct, but I can't figure out how to expand specific headings later in my document either by text or section number.
I have tried this loop to find the specific text in the heading and expand it
If ccCheckBox.Checked Then
Do Until Selection.Find.Found = False
If Selection.Text Like "Licensing Discovery Questions" Then
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
Selection.Find.Execute
Else: Selection.Paragraphs(1).CollapsedState = False
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
Selection.Find.Execute
End If
Loop
End if
I have added continuous sections between each heading in my document and tried this code to unhide the follow up questions
If ccCheckBox.Checked Then
doc.Sections(2).Range.Font.Hidden = False
End if
I have tried assigning the text I'm looking for to a string and expanding the string, but did not work that out either.
I am running Microsoft Office 365 ProPlus

I have found a solution by switching to Active X Check Boxes and using the following code for each check box.
Private Sub CheckBoxNameHere_Click()
If CheckBoxNameHere.Value = True Then
ActiveDocument.Sections(2).Range.Font.Hidden = False
Else: ActiveDocument.Sections(2).Range.Font.Hidden = True
End If
End Sub

Related

Clearing a list box and any selected items in list

I have an Excel VBA application that goes through a sheet which contains product orders on a sheet in a workbook and searches the worksheet for orders that match various criteria which is populates in a search worksheet. The contents from this worksheet are then displayed in list box. There are several user forms that allow the user to select an order and then manipulate the order. After doing this the order manipulated may not meet the search criteria so I want to clear the list box contents and the selected row in the list box. I have tried numerous things but nothing seems to work. My latest is something list this:
Private Sub ClearListBox()
UserForm5.lstOpenO.ListIndex = -1
UserForm5.lstOpenO.RowSource = ""
End Sub
But I have tried setting the UserForm5.lstOpenO.Selected to false for all the rows. I have tried clearing the search worksheet and then displaying that which should only show the headers on the columns but the highlight in the selected row remains.
Any help would be greatly appreciated
Bruce
First of all you should not use the default instance of the userform in your code. This will lead to ambigous behaviour
I'd suggest
Private Sub ClearSelectionInListBox()
Dim varItm as variant
With lstOpen0
varItm = .MultiSelect
.MultiSelect = 0
.MultiSelect = 1
.MultiSelect = varItm
End With
End Sub
This will clear the selection within the listbox.
It is not clear if you really want to clear the contents. Because if you want then it does not make sense to think about clearing the selection.
If you want to clear the listbox then it is not neccessary to clear the selection first.
Private ClearListBox()
With lstOpen0
.RowSource = ""
.Clear
End With
End Sub
But after that you need to fill the listbox again.
Further reading
VBA userform
Userform.Show
ListBox

Multiple IF Statements not working from 3rd statement on

I'm having issues getting the multiple If statement to work after the second statement. I have 2500 rows of data questions sets that are tied to a 217 item drop down list. What I'm trying to code is, that the questions applicable will populate based on the selected operation of the drop down list. Questions applicable per operation vary from 3 - 12, and questions not applicable to the selection should automatically hide the rows. I've tried multiple variations of code, using ElseIf, If, combination If and ElseIf, combination If and Or_ (this is the most recent) as shown below. Can someone please advise on this? I don't know what else to try and I need to add the additional 213 variables to the code.....Thanks
Sub Hide_Rows()
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim TrigerCell As Range
Set Triggercell = Range("$J$14")
If Not Application.Intersect(Triggercell, Target) Is Nothing Then
If Triggercell.Value = "Electrician, farm based business" Then
Rows("19:2500").Hidden = False
Rows("26:2500").Hidden = True
Or_
If Triggercell.Value = "Plumbing and heating contractors, farm based business" Then
Rows("19:2500").Hidden = False
Rows("19:25", "47:2500").Hidden = True
Or_
If Triggercell.Value = "Plumbing contractors (plumber), farm based business" Then
Rows("19:2500").Hidden = False
Rows("19:46", "55:2500").Hidden = True
Or_
If Triggercell.Value = "Assembly and installation of agricultural equipment in farm outbuildings, for others, farm based business" Then
Rows("19:2500").Hidden = False
Rows("19:55", "61:2500").Hidden = True
End If
End If
End Sub
End Sub
End Sub
Or_ is not something that exists in VBA, so it's not surprising that you're getting a compile error when you try to use it. This usage is unusual enough that it's probably confusing the compiler, which may be why you don't get an error message until the second or third of them (or maybe even on an unrelated statement).
You can make this work with a Select Case statement or by using ElseIf between conditions. I think Select Case is a little more natural for this problem. It should look like this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim TriggerCell As Range
Set Triggercell = Range("$J$14")
If Application.Intersect(TriggerCell, Target) Is Nothing Then Exit Sub
Select Case Triggercell.Value
Case "Electrician, farm based business"
Rows("19:2500").Hidden = False
Rows("26:2500").Hidden = True
Case "Plumbing and heating contractors, farm based business"
Rows("19:2500").Hidden = False
Rows("19:25").Hidden = True
Rows("47:2500").Hidden = True
'Etc.
End Select
End Sub
Note that (per comment on this answer) you can't specify two separate ranges to Rows, which was an issue with several of the blocks in the original code. I've updated the answer accordingly.

VBA - Locking Cells Only Working In Some Cells and Not Others

this project is certainly testing my limits in VBA. I have yet another question. In this workbook, I have a button called "Add Projects" that takes a different sheet "Project Templates" and uses it to create a new sheet called "Add Projects". I'm trying to add some specific cell locking when this sheet is created. The idea is that users can alter certain areas, but not ones that have formulas or are auto-populated by an entry in a different cell.
The problem is, when I add the code, certain ones lock appropriately, one particular cell does a popup asking for a password (when it should just be locked like the others), and some let you still edit them despite them being included in the locked code.
I've checked the cell format, and the box for locked is selected. I've also gone to the Allow Users to Edit Ranges and removed everything from there (although those ranges weren't in there). I've moved the order of the ranges around, thinking maybe they were somehow overwriting each other even though I have a distinct cell set as the end range.
Public Sub Open_AddProjectSheet()
Application.ScreenUpdating = False
ActiveWorkbook.Unprotect Password:="Password"
Worksheets("ProjTemplate").Visible = True
If WorksheetExists("AddProject") = False Then
GetTSResourceInfo
GetTSProjectID
ActiveWorkbook.Unprotect Password:="Password"
Worksheets("ProjTemplate").Copy After:=Worksheets("ProjTemplate")
ActiveSheet.Name = "AddProject"
Worksheets("ProjTemplate").Visible = xlSheetVeryHidden
Sheets("AddProject").Activate
ActiveSheet.Unprotect Password:="Password"
ActiveSheet.Cells.Locked = False
ActiveSheet.Range("E5,C6,L2,L6").Locked = True
ActiveSheet.Range("E11:E190").Locked = True
ActiveSheet.Range("G11:H190").Locked = True
ActiveSheet.Range("K2:K6").Locked = True
ActiveSheet.Range("L11:R190").Locked = True
ActiveSheet.Range("L3:L5").Locked = False
ActiveSheet.Protect Password:="Password",UserInterFaceOnly:=True,Contents:=True
Else
answer = MsgBox("'AddProject' sheet already active. Would you like to pick up where you left? WARNING: Selecting 'No' will close the existing sheet and open a new one, any progress will not be saved.", vbYesNo)
If answer = 6 Then
Worksheets("AddProject").Activate
Else
*Same code as above*
End If
End If
Worksheets("ProjTemplate").Visible = xlSheetVeryHidden
ActiveWorkbook.Protect Password:="Password"
Application.ScreenUpdating = True
End Sub
When running this code, E5,C6,K2-K6,L2,L11-R190 are all locked correctly. L6 brings up a popup asking for a password to change this cell. All other ranges, E11-E190, G11-H190 are editable even though they should be locked.
Any ideas I'm greatly appreciative of. Thanks!
So, even though it didn't work before, we went back and tried to alter the Allow Users to Edit Ranges. Wiped all of them out again and the cell locking started working. I'm not going to question why it worked vs not working the last time it was tried, just glad it's working! That being said, the speed being different is still an issue.

Hide or disable a button if the sheet is protected

For my form I need to hide or disable a textbox with a on-click delete macro attached to it when the sheet is protected. I'm talking about excel's build-in protection system. I've looked at several tutorials but I can't seem to get it to work properly.
I tried multiple things including this:
If ActiveSheet.ProtectContents = True Then
TextBox1.Visible = False
Else
TextBox1.Visible = True
End If
Any idea how to do this?
Change
If ActiveSheet.ProtectContents = True Then
TextBox1.Visible = False
Else
TextBox1.Visible = True
End If
To
If ActiveSheet.ProtectContents = True Then
ActiveSheet.TextBox1.Visible = False
Else
ActiveSheet.TextBox1.Visible = True
End If
You are not declaring where the textbox is. This will fix it
It is not clear whether the textbox is embedded in the worksheet or in a userform.
If in the worksheet is it a text box based on a shape? These are created from the INSERT tab on the Ribbon.
Or is this an ActiveX textbox? These are created from the DEVELOPER tab on the Ribbon.
If on the other hand, you are referring to a textbox on a userform then you can use the following code (assuming the userform code name is UserForm1):
UserForm1.Controls("NameOfYourTextBox").Visible = Not ActiveSheet.ProtectContents

Launch a different sheet by Spin button

want to use a Spin button (ActiveX Control) to show a previous or next sheet. When the button is clicked the event is successfully triggered and the desired sheet is activated but it holds some elements (commands, graphs, etc.,) from the original sheet and shows these as an appending picture.
Sample code for Down button event :
Private Sub SpinButton_JumpToWeek_SpinDown()
Dim sh_num As String
Dim tmp_num As Integer
Application.ScreenUpdating = False
Application.EnableEvents = False
SpinButton_JumpToWeek.Value = Range("B27").Value - 1
tmp_num = SpinButton_JumpToWeek.Value
' Activate desired KTx sheet
sh_num = "KT" & tmp_num
Range("F27").Value = "" 'reset to blank
Sheets(sh_num).Activate
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
To override this effect I have to manually select (activate) another sheet and then again select the desired sheet. I tried also to automatize this workaround with a macro, but unfortunately it does not work.
It is interesting that this problem do not occur if I execute code in Debug mode (using breakpoint and the stepping line by line).
Surprisingly, I do not have such problem if I try to show the previous/next sheet by writing a value (sheet name index) into a defined cell (i.e. using the Worksheet_Change event). The desired page is correctly shown. See photos.
Sample code for this evententer image description here:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim sh_num As String
Application.ScreenUpdating = False
Application.EnableEvents = False
If Range("F27").Value > 0 Then
' Activate desired KTx sheet
sh_num = "KT" & Range("F27").Value
Range("F27").Value = "" 'reset to blank
Sheets(sh_num).Activate
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
I need to use the Spin button because it is faster and allow me to skip some sheets (for example if these do not have data).
The problem occurs in both 32 and 64-bit Excel.
Do somebody an idea why this problem is occurring and a workaround? Do I have to change some settings or system properties in Excel? Any help would be highly appreciated.
#mehow
I append my commenst here due to large number of words.
I followed your suggestion and tried the example of a UserForm with inserted active control “Microsoft Office Spreadsheet”. I found out that this would not be a good solution for my case, because the response of such sheet is relatively slow (even on a fast PC like mine) when user inserts values into cells. Furthermore, this would greatly complicate my fairly simple *.xlsm workbook, which has more than 50 sheets (1 sheet for each week, the contents of these sheets are then linked to a main sheet), and completely meets my basic necessities (with the exception of this spin button of course).
In my opinion there is probably necessary to manipulate some kind of system property (like for e.g. Application.ScreenUpdating trick), but I do not have enough VBA knowledge to find it.
To clearly explain my question I would need to share my sample workbook, but I do not know how to upload it to this forum. Unfortunately, I am not able upload/show images successfully on this forum (due to my low rating), which would also help a lot.
BTW, I cannot see images on other questions of this forum, too. . Could this problem occur due to missing plugins in a web browser?
You can use a simple trick... before the "Application.screenupdating = true" you can insert the two lines:
ActiveWindow.SmallScroll Down:=-100
ActiveWindow.SmallScroll Up:=100

Resources