Word VBA Renaming Drop-Down Form Fields? - excel

I have a set of 6 tables in a password-protected Word document. Each table has a header row, followed by at least one data row. The 6th column of the second (and subsequent) rows has a drop-down form field in it. There is a button above each table that allows the user to add a row to the table. When they do so, the 6th column contains another drop-down.
After the user selects a value in the drop-down and leaves the field, I need to color that cell, based on the selected value. I can set the OnExit value to run a macro I call "HighlightStatus". To get the reference to the cell to change, I am using
Dim ff As Word.FormField
ThisDocument.Unprotect strSheetPassword
Set ff = ThisDocument.FormFields(Selection.Bookmarks(1).Name)
How do I reference the cell that contains this form field?
The other problem is that when I add a row to the table, I need to set the Name of the form field to a unique one, so that I can reference the field that was just updated. But the Name property seems to be read-only.
I took another approach, that basically runs into the same wall. After updating the field, I had the code loop through all the tables and all of the rows, and update the 6th column of each row. The problem is that the screen scrolls down to the end of the document. To get back to where I was, I still need to uniquely reference the correct form field, which I can't do if the field doesn't have a unique name.

Found it. I was chasing my tail for quite a while, but I found that this works.
ThisDocument.Unprotect strSheetPassword
Selection.Shading.BackgroundPatternColor = wdColorLightGreen
ThisDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=strSheetPassword

Related

VBA How to determine which row was selected in a Listbox?

How to determine, which row was selected in a ListBox? I'm asking here because, all I have found so far, requires to loop through all rows to check if was selected or not, but I was wondering if something simple exists, as I'm using mode MultiSelect 0-fmMultiSelectSingle only an integer is expected...
If you filled the list box using the ListFillRange property, or otherwise used a contiguous worksheet range, the 3rd item in the listbox will be from the row of the 3rd cell in the range.
R = Range(ListFillRange).Rows([Listindex]).Row
where [Listindex] is the ListIndex of the selected list box item.
If this easy way isn't available, f.i. because the list was created using a filter, the thing to do is to create a multicolumn list box with an extra, hidden, column and record the row number there at the time of list creation.
You would need to loop only if the items in the list box were not extracted from the worksheet in the first place, and this is the reason why doing so wouldn't be the best solution for creating the list box's list.

Want to transfer information from a listbox that into separate rows in the main table

I have created a form with a text box (date), combobox (course), combobox (name of attendee).
The name combobox feeds into a list box, so that all the people who attended the course are able to be captured. The listbox is editable incase the wrong name was selected.
Now I want a button to save all the details.
So for the each person who attended the course, the date, person's name and course name will be captured in a separate line in the table.
But when I try to extract the data from the listbox to the cell it states is a type mismatch.
How can I enter ALL the information into the table?
I have a loop for 0 to listcount
I am entering the value into .Range("A" & Lastrow)...
Currently I have indicated: Listbox1(x,0).value
I do not know how to extract the Listbox value to the cell. This doesn't work. Listbox1(x) doesn't work. I need every row in the listbox to populate into the table.
Any help is appreciated.
Tammy

Refer to value selected in combo box with two columns on user form

I have a user form with a combo box with two columns. How do I refer to the values that are active in the combo box within VBA script on other parts of the user form (buttons, etc)?
Simply using MyComboBox.Value yields the value in the first column. I can't figure out how to refer to the second column.
EDIT: Based on advice below, I used this:
MyComboBox.List(MyComboBox.ListIndex, 1)
You don't.
The Value of a multi-column ComboBox will be the "key" value. You've presumably populated the dropdown from some list or array: use that same source to lookup the value corresponding to the selected key. Note the key/value wording: having the values in a Dictionary makes it very easy to retrieve the value of the second column.
Or, look it up from the control's List (which is essentially a copy of your items source), using the ListIndex, which gives you the index of the selected item in the source list.
You can have a Property Get procedure responsible for this lookup - then other parts of the code can easily consume it as needed.
Private Property Get SelectedItemDisplayText() As String
SelectedItemDisplayText = MyComboBox.List(MyComboBox.ListIndex, 1)
End Property
Normally you only care for the selected "key" (/"ID"), not the "display value" though.

Where does Excel store the parameters you pass into Range.Group

This has to do with Pivot Tables.
I am trying use VBA (or C#) to get to some data that's stored in the spreadsheet, but I don't know where it is in the object model. I know the data is in the spreadsheet because when I unzip the spreadsheet, I see it in the pivotcache. The following documentation for OpenXML talks about the field in particular:
https://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.rangeproperties%28v=office.14%29.aspx
In the RangeProperties class, see the groupInterval property, in particular.
For instance, the XML shows:
<fieldGroup base="0">
<rangePr startNum="19" endNum="947" groupInterval="656"/>
<groupItems count="4">
<s v="<19"/>
<s v="19-684"/>
<s v="685-1350"/>
<s v=">1351"/>
</groupItems>
</fieldGroup>
But I don't know how to get to this from the Excel object-model at runtime.
The above data represents the entries the user makes in order to group a set of rows in a pivot table. Below are more details on how to get to it.
When you click on Rows of a Pivot Table, you can Group the rows. For instance, let's say you have a thousand rows, but the values range from 1-100. You don't want to see all 1000 rows, just in 10 rows (groups of 100). First group will be a row that aggregates all the rows where the value is between 1-100, the next group for values 100-200, etc.
When you right-click any cell the Row Labels section, and click on the Group item from the context menu, you'll get a small dialog box with "Grouping" in the title bar, and fields:
- Starting at:
- Ending at:
- By:
And there will be a checkbox next to the starting at and ending end fields.
I'll take the defaults, and increment by 100
The equivalent to doing this in VBA (or C# VSTS) is:
Select the cell:
Call the Group() method on the current single-cell selection, eg:
Selection.Range.Group true, true, 100
At this point, the PivotItems associated with the PivotField end up with captions such as "1-100".
Now, if you right-click on the field again and select the Group item, you will get the same dialog box pop-up, with the fields of the dialog box filled in with the previous values you selected for that PivotTable.
However, the information that's used to fill that dialog box doesn't seem to exist anywhere in the object model. I suspect I just don't know where to look.
Does anyone know?
So the basic question in is, when I call the Group method on a single-cell Range object, and pass in various parameters, where are those parameters retained in the object model? I know it's there because Excel itself is able to load it up and it's stored in the spreadsheet.

Dynamic Filters in Excel

I'm using Office 2013, and working on a worksheet in Excel.
My question is, is there a way to create a dynamic filter in Excel?
To explain in more detail, I have a dynamic worksheet, where upon opening the user will get a few drop-down options. After the user has selected one option from each of the drop-downs, the worksheet will display a table of data based on the user's inputs. The user can change their selections from the drop-downs after the table has been displayed, and can also clear their selections. If they clear their selection, the table will disappear.
Now, the first column in the result Table will contain Text values, but can also contain blanks. These values or blanks are all decided based on the user's selections in the previously mentioned drop-downs, which are displayed permanently to the left of the table. I want to add a filter to this first column of the result table (and to the rest of the table with it) such that only the non blank cells are displayed in the table every time the drop-downs are changed.
As I understand your need correct, I can give you this solution:
For getting a better result make your range to Table.
Select Power View from Insert items;
A sheet Power View 1 will added to your workbook;
From right pane select Table1 and its fields;
From Filter pane select as you want for filtering.

Resources