I would like to have a word document that has 2 comboboxes. Combobox1 will have a selection and the second will be populated dependent on Combobox1's selection.
The following code executes the legacy controls style, but I want active x combo boxes. I also have a constraint that comments need to be able to be entered in the word doc, and restricting form fields disables that (why the legacy style won't work).
Sub Dropdowns()
Dim xDirection As FormField
Dim xState As FormField
On Error Resume Next
Set xDirection = ActiveDocument.FormFields("QSystem")
Set xState = ActiveDocument.FormFields("FType")
If ((xDirection Is Nothing) Or (xState Is Nothing)) Then Exit Sub
With xState.DropDown.ListEntries
.Clear
Select Case xDirection.Result
Case "Type1"
.Add "SubType1"
.Add "SubType2"
Case "Production and In-Process Controls"
.Add "SubType3"
.Add "SubType4"
End Select
End With
End Sub
Related
I am a bit of a novice when it comes VBA (and coding generally) and apologize in advance if this isn't possible. I have created a userform for users to input different pieces of information (name, address, etc.). When the user clicks the "OK" command button, the values of the differing text boxes appear at various bookmarks within the Word document. For instance, TextBox 1 is dedicated to name, TextBox2 is for the address, etc.
Additionally on the userform, I have added a ListBox that lists various types of data. Ideally, I'd like to have the multiple selections from that ListBox appear at the bookmark in Word on the same line together after clicking the "OK" command button on the userform. It transfers the values to the document (where the cursor is blinking), but I can't figure out how to assign it to the bookmark.
You can see below that I'm assigning the values of the textboxes to the bookmarks and essentially want to do the same with the ListBox.
The ListBox is filled at initialization with the following code.
With ListBox1
.AddItem "name"
.AddItem "address"
.AddItem "secondary address"
.AddItem "Social Security Number"
.AddItem "financial account number"
.AddItem "date of birth"
.AddItem "email address"
End With
End Sub
Private Sub CommandButton1_Click()
Dim DataSub1 As Range
Set DataSub1 = ActiveDocument.Bookmarks("DataSub1").Range
DataSub1 = Me.DataSub1.Value
Dim dAddress As Range
Set dAddress = ActiveDocument.Bookmarks("dAddress").Range
dAddress.Text = Me.dAddress.Value
Dim ii As Integer
For ii = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(ii) Then
Selection.Text = ListBox1.List(ii)
Selection.MoveRight
Selection.TypeParagraph
End If
Next ii
Application.ScreenRefresh
Me.Repaint
Me.Hide
End Sub
For example:
Dim ii As Integer, listText As String
For ii = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(ii) Then
listText = listText & ListBox1.List(ii) & vbCr
End If
Next ii
ActiveDocument.Bookmarks(bmkName).Range.Text = listText
I am trying to set up a dropdown that is dependent on the selection of a previous dropdown in Word using VBA-Code. I watched a bunch of videos and read through forums, but I cannot make it work. I used the Word Legacy dropdowns and labelled them correctly, then I wrote the following code in VBA:
Dim xDirection As FormField
Dim xState As FormField
On Error Resume Next
Set xDirection = ActiveDocument.FormFields("ddType")
Set xState = ActiveDocument.FormFields("ddSelection")
If ((xDirection Is Nothing) Or (xState Is Nothing)) Then Exit Sub
With xState.DropDown.ListEntries
.Clear
Select Case xDirection.Result
Case "Numbers"
.Add "1"
.Add "2"
.Add "3"
.Add "4"
.Add "5"
.Add "6"
Case "Letters"
.Add "A"
.Add "B"
.Add "C"
Case "None"
.Add "Not applicable"
End Select
End With
End Sub
The problem is that this solution only works sometimes and not consistently. It feels like the possible selections are not updated quick enough and I can choose a letter even if only numbers should be available (sometimes I am not able to select anything at all).
I do this in Office 365.
Feedback will be highly appreciated
Thank you in advance!
Assumption: you have a word document with two dropdown content controls.
For both the tag name is set: ccType and ccSelection.
In the class module of ThisDocument you put the following code:
Option Explicit
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
If ContentControl.Tag = "ccType" Then
fillccSelection ContentControl.Range.Text
End If
End Sub
Private Sub fillccSelection(valueType As String)
Dim cc As ContentControl
Set cc = ThisDocument.SelectContentControlsByTag("ccSelection")(1)
If cc.Title <> valueType Then
With cc
.Title = valueType 'set title to current type so that we now if coming here next time
.Range.Text = vbNullString 'clear content as it has to change with new values
With .DropdownListEntries
.Clear
Select Case valueType
Case "Numbers"
cc.SetPlaceholderText Text:="Please select a number"
.Add "1"
.Add "2"
.Add "3"
Case "Letters"
cc.SetPlaceholderText Text:="Please select a letter"
.Add "A"
.Add "B"
.Add "C"
End Select
End With
End With
End If
End Sub
Whenever you change the value of the first content control (ccType) and exit it the ContentControlOnExit is fired.
If you "left" ccType (and not ccSelection) fillccSelection is called by passing the value selected in ccType.
If this type is not yet set for ccSelection, the dropdown entries are set according to the selected type.
I am creating a VBA macro to select all checkboxes in a sheet and it works fine but now i want to adjust my code to select and unselect just the checkboxes in a particular range.
Here is my code.
Sub Select_all()
Dim Cbox As CheckBox
Dim Rng As Range
Set Rng = ActiveWorkbook.Sheets("Sheet4").Range("B7, B104")
For Each Cbox In ActiveSheet.CheckBoxes
If Not Intersect(Cbox.TopLeftCell, Rng) Is Nothing Then
If Cbox.name <> ActiveSheet.CheckBoxes("Check Box 104").name Then
Cbox.Value = ActiveSheet.CheckBoxes("Check Box 104").Value
End If
End If
Next Cbox
End Sub
I added
If Not Intersect(Cbox.TopLeftCell, Rng) Is Nothing
but it doesn't change anything, and when i remove this part then it selects all the checkboxes in the sheet and i need only for the Range("B7:B104")
Any suggestions please ? Thank you very much.
This works for me. But I changed the cb# to "1" because I don't have 104 of them on my test sheet. Any cb's in range B7:B104 will be changed to the value of cb1, which you can change to cb104 in your script.
Sub Link_Checkboxes_To_Cells()
Dim cBox As CheckBox
For Each cBox In Sheet1.CheckBoxes
If Not Intersect(cBox.TopLeftCell, Range("B7:B104")) Is Nothing Then
If cBox.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name Then
cBox.Value = ActiveSheet.CheckBoxes("Check Box 1").Value
End If
End If
Next cBox
End Sub
#JuniorDev
There are a few reasons why the above code doesn't work for you. I'll try to list them.
The above code works for "Form Control" checkboxes - Not ActiveX type checkboxes. You would need a different code for ActiveX checkboxes. Or you may not have a form checkbox named "Check Box 1". But I think that would give you an error. Or your other checkboxes may not be in the Range("B7:B104"). You can try changing that to Range("A1:Z10000") to insure your checkboxes are in the range.
If you are not sure what type checkboxes you have then you can run the following code to find their names and what type of control they are, either form or activex. It will print the info in your immediate window of the VB editor.
Sub CheckboxLoop()
'PARTIAL SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
Dim cb As Shape
'Loop through Form Checkboxes
For Each cb In ActiveSheet.Shapes
If cb.Type = msoFormControl Then
If cb.FormControlType = xlCheckBox Then
Debug.Print "Form Control: " & cb.Name
End If
End If
Next cb
'Loop through ActiveX Checkboxes
Dim ChkBx As OLEObject
For Each ChkBx In ActiveSheet.OLEObjects
If TypeName(ChkBx.Object) = "CheckBox" Then
Debug.Print "ActiveX Control: " & ChkBx.Name
End If
Next ChkBx
End Sub
Sorry to resurrect an old post, but I hate it when a solution has not been provided and I know the answer, which is seldom. Thank you, this helped me on a project I am currently working on.
If you're like me, I change all my Sheet Names for sake of clarity.
For Each cBox In Sheet1.CheckBoxes
This line in John Muggins code limits you to the Sheet Name, "Sheet1".
I was able to change the line to...
For Each cBox In ActiveSheet.CheckBoxes
...and it worked perfectly.
I'm trying to put a combobox inside active worksheet (but not activeX combobox), choose a list to fill and linked cell. It is an easy task, for example:
Sub make_combobox()
ActiveSheet.DropDowns.Add(69.75, 1.5, 79.5, 40.5).Select
Selection.Name = "combo"
ActiveSheet.Shapes("combo").Select
With Selection
.ListFillRange = "$A$1:$A$3"
.LinkedCell = "$D$1"
.DropDownLines = 8
.Display3DShading = False
End With
End Sub
I tried to put macro in worksheet containing this combobox, which will show msgbox whenever chosen linked cell is changed according to the chosen option in combobox. I wrote this in Worksheet section:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D1")) Is Nothing Then
MsgBox "It works!"
End If
End Sub
Unfortunately, it doesn't work (Actually, it works when I change a value in D1 manually, but not work as a result of change in combobox).
Just assign a macro to the control using the OnAction property. It will run after every change made to the Combobox's value.
I have the next excel sheet with many checkboxs.
The problem is when I am coding I have to do some functions with Valor1 and Valor2 depending of if the checkbox is activate.
Well I have the code.
Option Explicit
Sub Casilladeverificación1_Haga_clic_en()
Range("c12").Activate
Do
If CheckBox1.Value Then
Call fucntion1
'Works for the first row, but for the second row int shoul be check CheckBox12 ,a next CheckBox23 ...
If CheckBox2.Value Then
Call fucntion1
If CheckBox2.Value Then
Call fucntion3
....
ActiveCell.Offset(1, 0).Activate
While Not IsEmpty(ActiveCell.Value2)
End Sub
But you can notice I dont want to made all the case with all the checkbox, there is a solve for this like checkbox[i]
I would put all of your functions into one big function and the functionality would separated by a Select Case block.
Private Sub functionRouter(checkAction as integer)
Select Case checkAction
Case 1
'Code for function one
Case 2
'Code for function two
''Etc.
End Select
End Sub
You're going to want to loop over all your check boxes. This is going to depend on what checkbox you are using.
Sub test()
Dim chkBox As CheckBox
Dim chkBox2 As OLEObject
'Regular
For Each chkBox In Sheets("Sheet1").CheckBoxes
Debug.Print chkBox.Caption
Next chkBox
'ActiveX
For Each chkBox2 In Sheets("Sheet1").OLEObjects
If TypeName(chkBox2.Object) = "CheckBox" Then
Debug.Print chkBox2.Object.Value
End If
Next chkBox2
You could do a few different things with all of your checkboxes. You could use the tagproperty (you would need to set all of them, but this allows for duplicates). Then call functionRouter(chkBox.tag) Or you could parse something from the name functionRouter Right(chkBox.name, 1)
You can iterate checkboxes on worksheet by using this loop:
For Each chk In ActiveSheet.CheckBoxes
MsgBox chk.Name
Next
It won't work if you use ActiveX controls though.