Set combo box to doc variable on userform vba word - vba

How can I update my code below so instead of setting the combo box to "bmextension", I would set it to a Variable with the same name. I'd imagine something like Set ComboBox2 = ActiveDocument.Variables("bmextension").Range
Dim ComboBox2 As Range
Set ComboBox2 = ActiveDocument.Bookmarks("bmextension").Range
ComboBox2.Text = Me.ComboBox2.Value
If Me.ComboBox2.Value = "Yes" Then
ComboBox2.Text = "Please enter your code"
End If
If Me.ComboBox2.Value = "No" Then
ComboBox2.Text = ""
End If

See Word MVP Graham Mayor's page on UserForms which shows use of bookmarks, document variables and Content Controls.
Set ComboBox2 = ActiveDocument.Variables("bmextension").Value

Related

Referencing Text Content Controls

I am trying to reference and paste a specified string into a specific Text Content Controls and have been unable to do this properly.
Basically I have gone through and tried a few different things, first being this;
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim ccs As ContentControls, cc As ContentControl
Set ccs = ActiveDocument.ContentControls
For Each cc In ccs
If cc.Title = "ComboBox1" And cc.Range.Text = "Choose an item." Then
SelectContentControlsByTitle("TextBox1").Add.SetPlaceholderText , , "Please make a drop down selection or manually fill out if not applicable"
The above does not work, as every time I exit my content controller combo box it actually recreates the "placeholdertext" multiple times. I need this to only fill the "TextBox1" Content control.
I have also tried doing something like this,
Dim ccs As ContentControls, cc As ContentControl
Set ccs = ActiveDocument.ContentControls
Set CB1 = SelectContentControlsByTitle("TextBox1")
For Each cc In ccs
If cc.Title = "ComboBox1" And cc.Range.Text = "Choose an item." Then
CB1.Value = "Please make a drop down selection or manually fill out if not applicable"
Due to the type, value is not able to be used like this. This does not work either.
Below is the original way I was doing what I wanted with the Active-X TextBox which does work;
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim ccs As ContentControls, cc As ContentControl
Set ccs = ActiveDocument.ContentControls
For Each cc In ccs
If cc.Title = "ComboBox2" And cc.Range.Text = "Choose an item." Then
TextBox2.Value = "Please make a drop down selection or manually fill out if not applicable"
ElseIf cc.Title = "ComboBox2" And cc.Range.Text = "TMS backup" Then
TextBox2.Value = "The TMS installation directory, settings directory and the database was backed up before the update was performed"
ElseIf cc.Title = "ComboBox2" And cc.Range.Text = "TMS installation" Then
TextBox2.Value = "Installation of version 1.17.X.19XXX performed"
ElseIf cc.Title = "ComboBox2" And cc.Range.Text = "TMS update" Then
TextBox2.Value = "Update from version 1.16.X.XXXXX to version 1.17.X.19XXX performed"
ElseIf cc.Title = "ComboBox2" And cc.Range.Text = "Tool presetter update" Then
TextBox2.Value = "Update from version 1.16.X.XXXXX to version 1.17.X.19XXX performed"
ElseIf cc.Title = "ComboBox2" And cc.Range.Text = "Database generated" Then
TextBox2.Value = "Database structure created with version 1.17.0"
How can I do the above, while using the Text Content Controls?
A better approach would be:
Private Sub Document_ContentControlOnExit(ByVal Ctrl As ContentControl, Cancel As Boolean)
Dim i As Long, StrDetails As String
With Ctrl
If .Title = "ComboBox1" Then
If ShowingPlaceholderText = True Then
StrDetails = ""
Else
For i = 1 To .DropdownListEntries.Count
If .DropdownListEntries(i).Text = .Range.Text Then
StrDetails = .DropdownListEntries(i).Value
Exit For
End If
Next
End If
ActiveDocument.SelectContentControlsByTitle("TextBox1")(1).Range.Text = StrDetails
End If
End With
End Sub
To make this work, simply add your 'conditional' text to each entry's 'Value' property, and make the "TextBox1" content control's placeholder text whatever you want its prompt to be. This way, you don't have to hard-code either the dropdown option or the 'conditional' text in your VBA code. For a practical demonstration, see: https://www.msofficeforums.com/word-vba/16498-multiple-entries-dropdown-lists.html#post46903
PS: You really should get away from the default ActiveX naming conventions and give your content controls meaningful titles.
Try this for setting the text box text:
Sub SetContentcontrolText()
Dim oRange As Range
Dim cc As ContentControl
For Each cc In ActiveDocument.ContentControls
If cc.Title = "ComboBox1" And cc.Range.Text = "Choose an item." Then
Set oRange = ActiveDocument.SelectContentControlsByTitle("TextBox1")(1).Range
oRange.Text = "Please make a drop down selection or manually fill out if not applicable"
End If
Next
End Sub

Userform runtime error 380- could not set the rowsource property - fix?

Code has been working perfectly for past year. Made some edits and extended some of the data - ensured it was all lined up correct at the time. I came to use it but now I am getting the following error
runtime error 380: could not set the rowsource property"
I have narrowed it down to .RowSource = "VOLNAME2" but I haven't changed anything about this data.
The error initially comes up when you press the button to bring this UserForm up, so debug starts on newlog.show (separate module). However, I used F8 to highlight the error line as above.
I tried redefining the name manager and renaming it. Deleted and re-entered it. I had a look at some other answers but couldn't see anything that stood out as the answer.
Sub UserForm_Initialize()
Dim R As Range
Me.TextBox2.Locked = True
Me.TextBox3.Locked = True
Me.CommandButton1.Enabled = False
With Me.ComboBox4
.RowSource = "VOLNAME2"
.MatchEntry = fmMatchEntryComplete
.Style = fmStyleDropDownList
End With
With Me.ComboBox1
.RowSource = ""
.MatchEntry = fmMatchEntryComplete
.Style = fmStyleDropDownList
For Each R In Range("'Database'!F4:F23")
.AddItem R.Text
Next
End With
With Me.ComboBox3
.RowSource = ""
.MatchEntry = fmMatchEntryComplete
.Style = fmStyleDropDownList
For Each R In Range("'Database'!F26:F51")
.AddItem R.Text
Next
End With
With Me.ComboBox2
.RowSource = ""
.MatchEntry = fmMatchEntryComplete
.Style = fmStyleDropDownList
For Each R In Range("'Database'!H3:H32")
.AddItem R.Text
Next
End With
End Sub
I am expecting the defined name "volname2" to populate combobox 4
You can't update the rowSource for these to straight values, they require a range or some other lookup.
Change:
With Me.ComboBox4
.RowSource = "VOLNAME2"
.MatchEntry = fmMatchEntryComplete
.Style = fmStyleDropDownList
End With
To one of these options:
Option - 1 Clears the list adds the requested text and sets the index to display that value
ComboBox1.Clear
ComboBox1.AddItem ("volname2")
ComboBox1.ListIndex = 0
Option 2 - Clears the list and sets the text value to requested value. Personally this is a bad choice, because you'll have to forcibly clear the text value with .Text = "" if you want to re-use it.
ComboBox1.Clear
ComboBox1.Text = "volname2"

Rowsource behavior not updating/resetting the listbox without reloading the userform // getting column headers with list = ranges

I have the following code that works, BUT only on me unloading(x) the userform and reloading it. The listbox(being the DatabaseViewer) loads on userform initialize.
The testbutton will take the string input on a TextBox and fuzzy search the original database and copy out the database to a temporary sheet which allows for the rowsource formula to work.
I would have preferred to use list = Ranges instead of RowSource but from my google searches it appears that that is the only way to get headers reliably, if there is an alternative to getting headers please do share!!
Private Sub testButton_Click()
Dim dataworksheet As String
dataworksheet = ActiveSheet.Name
MsgBox (dataworksheet)
Range(Cells(getFirstDataAreaRow, getApplicableColumn("Original Name")), Cells(getLastDataAreaRow, getApplicableColumn("Original Name"))).AutoFilter Field:=1, Criteria1:="*" & searchByName.Value & "*"
Range(Cells(getFirstDataAreaRow, getApplicableColumn("Number")), Cells(getLastDataAreaRow, getlastDataAreaColumn)).SpecialCells(xlCellTypeVisible).Copy
Sheets("Temp").Activate
Range("A1").Select
ActiveSheet.Paste
Sheets(dataworksheet).Activate
MsgBox (dataworksheet)
Dim TempDataArea As Range
Set TempDataArea = Range(ThisWorkbook.Sheets("Temp").Cells(getFirstDataAreaRow("Temp") + 1, getlastDataAreaColumn("Temp")), ThisWorkbook.Sheets("Temp").Cells(getLastDataAreaRow("Temp"), getlastDataAreaColumn("Temp")))
With databaseViewer
.ColumnCount = getlastDataAreaColumn("Temp")
.ColumnHeads = True 'only works if you are using rowsource
.ColumnWidths = "0;;4 in" 'THIS SETS the number column to HIDDEN!
.RowSource = TempDataArea.Cells.Address 'rowsouce only works with address, .list works with ranges
End With
End Sub

Warning message in Excel macro if combobox null

I create combo box selection using userform in Excel macro.
What I want to do is, to prevent the user to click OK without selecting a value.
Here is my code, I don't know what is wrong, the message box doesn't show.
Private Sub UserForm_Initialize()
ComboBox1.RowSource = "Sheet1!G1:G" & Range("G" & Rows.Count).End(xlUp).Row
ComboBox2.RowSource = "Sheet1!G1:G" & Range("G" & Rows.Count).End(xlUp).Row
End Sub
Private Sub CommandButton1_Click()
If IsNull(ComboBox1) Then
MsgBox ("ComboBox Has Data")
End If
Workbooks("Select Project.xlsm").Sheets("Sheet1").Range("B2").Value = ComboBox1.Value
Workbooks("Select Project.xlsm").Sheets("Sheet1").Range("C2").Value = ComboBox2.Value
End Sub
Can anybody help what is wrong with my code? Sorry, I'm new to VBA.
You're not checking the Text property of your ComboBox. You should process like this.
Private Sub CommandButton1_Click()
If (ComboBox1.Text = "") Then
MsgBox "ComboBox Has No Data"
Exit Sub
End If
Workbooks("Select Project.xlsm").Sheets("Sheet1").Range("B2").Value = ComboBox1.Value
Workbooks("Select Project.xlsm").Sheets("Sheet1").Range("C2").Value = ComboBox2.Value
End Sub
What changed ?
I changed If IsNull(ComboBox1) Then with If (ComboBox1.Text = "") Then so this will check the Text property in your ComboBox.
I also added Exit Sub to leave the function if the ComboBox is empty so it doesn't commit the operation after.
IsNull(ComboBox1) and IsNull(ComboBox1).Value will both never be true. Null is a value returned from a database if a field contains no value. You have to check if the value of the ComboBox is empty. An empty string in VBA is a string with the length 0, so you have to use on of those:
If Me.ComboBox1 = "" then ...
If Me.ComboBox1.Value = "" then ...
If Me.ComboBox1.Text = "" then ...
(For the difference between value and text-property see Distinction between using .text and .value in VBA Access)
Anyhow, I would go for the solution to enable/disable the button (as Rosetta suggested). Put a event-routine to the Combobox:
Private Sub ComboBox1_Change()
Me.CommandButton1.Enabled = Me.ComboBox1.Value <> ""
End Sub

Using variable for sheet name in excel VBA VLookup

Private Sub UpdateBoxes()
Dim wsFunc As WorksheetFunction: Set wsFunc = Application.WorksheetFunction
Dim sheet As String
If Range("C1") = "some string" Then
sheet = "SomeSheet"
End If
Range("B6").Value = Sheets("Spreadsheet Ctrl").Range("B7") 'Sets title of Box 1 based on Spreadsheet Ctrl
Range("G6").Value = Sheets("Spreadsheet Ctrl").Range("C7") 'Sets title of Box 2 based on Spreadsheet Ctrl
Range("B11").Value = Sheets("Spreadsheet Ctrl").Range("D7") 'Sets title of Box 3 based on Spreadsheet Ctrl
Range("G11").Value = Sheets("Spreadsheet Ctrl").Range("E7") 'Sets title of Box 4 based on Spreadsheet Ctrl
Range("B16").Value = Sheets("Spreadsheet Ctrl").Range("F7") 'Sets title of Box 5 based on Spreadsheet Ctrl
Range("G16").Value = Sheets("Spreadsheet Ctrl").Range("G7") 'Sets title of Box 6 based on Spreadsheet Ctrl
Range("C7").Value = wsFunc.VLookup(B6,'" & sheet & '"!A1:G5,3,)" 'Vlookup for "Current Revision
End Sub
The variable "sheet" will eventually change based on a nested if. It should then be passed to the last line of code before End Sub. I am getting a compile error that states "Expected: expression", and it highlights the first tick in '" & sheet & '".
Something like:
Range("C7").Value = wsFunc.VLookup(ActiveSheet.Range("B6"), _
Sheets(sheet).Range("A1:G5"),3,False)