Reference item in group ( Textbox ) - excel

I was using below line of code to edit word document's textbox. It is working pretty well.
wdDoc.Shapes("T1").TextFrame.TextRange.Text = sh.Range("Q2"). Value
But now I made some changes in my Word document (template file).
I Grouped few items with Shapes("T1").So now Shapes("T1") is inside Group(3).
Now code give error, because it can't find Shapes("T1").
How can I reference Shapes("T1") within Group (3)?
Thank you.
Only property I have found is Shape.GroupItems

Here is the solution:
With wdDoc.Shapes("Group 3")
For x = 1 To .GroupItems.Count
If .GroupItems(x).Name = "T1" Then
With .GroupItems(x)
.TextFrame.TextRange.Text = sh.Range("Q2").Value
End With
End If
Next
End With

Related

VBA skipping code based on previous error

I think I am missing something obvious but have been looking at it for so long I think I may be blind to it.
I have a sheet coded to create a pivot table from a downloaded report and apply specific filters to the pivot table - which I am aware will have some instances where there is only 1 item in the filter list so cannot apply and returns an error.
I have managed to add 'On Error GoTo...' a line past the code I know will not be able to process.
However, I also have a second Pivot Table on the same sheet which applies the same filter but in reverse - i.e. the filter will usually have 2 items, so the 2 pivot tables end up showing the breakdown of the contents of each item.
The problem is 'On Error GoTo...' is not working on the second item.
I have the GoTo locations name differently - the first pivot GoTo = 'NoOKL:' and the second = 'NoOKS:'.
Because an error on the first Pivot will mean an error on the second every time I am trying to get around this by adding 'P = 1' to the error handling of the first Pivot and then added the code below for the second:
If P = 1 Then GoTo NoOKS
With ActiveSheet.PivotTables("PivotTable11").PivotFields("CATEGORY")
.PivotItems("OKL_CONTRACTS").Visible = False
End With
ActiveSheet.PivotTables("PivotTable11").PivotFields("CATEGORY"). _
EnableMultiplePageItems = True
NoOKS:
End If
I have tried moving the GoTo location 'NoOKS' both inside the If statement and outside but get the same result.
Any help would be appreciated.
Here's an example of what your code might look like if you omit all GoTo's.
If P = 1 Then
With ActiveSheet.PivotTables("PivotTable12").PivotFields("CATEGORY")
.PivotItems("OKL_CONTRACTS").Visible = False
.EnableMultiplePageItems = False
End With
Else
With ActiveSheet.PivotTables("PivotTable11").PivotFields("CATEGORY")
.PivotItems("OKL_CONTRACTS").Visible = False
.EnableMultiplePageItems = True
End With
End If
This code presumes that you have 2 pivot tables of which you want to hide one subject to the number of items it will display (presumed to be P) and set the EnableMultiplePageItems property differently. This doesn't make much sense in the above example but the objective is to show the use of If and Else instead of GoTo.
I point out that EnableMultiplePageItems = (P = 1) would also set the property to either True or False depending upon the evaluation of the statement (P = 1). In the above example the property belongs to different objects but if you have to set the same property for the same object to different values in your project depending upon the value of P that method will avoid even the use of If, not to mention GoTo and reduce the amount of code as well.

Having trouble using For/Next with variables

I am using an excel Userform and on the form I have created several label fields that are named in sequential order. For example:
Label1, Label2...LabelX
I want to assign the .caption for them from an array that I have built separately. Using a For/Next loop makes the most sense in my head, but since it is a label, I am having trouble. I cannot even get the code to take in a way that makes sense. Below is what I was trying...
For i = 1 To 100
Labeli.Caption = Array(i)
Next i
If there is a way to keep it inside of the loop, it would make the code a lot less cumbersome. Thank you in advance for your assistance.
Try this:
i = 1
For Each itm In Me.Controls
If itm.Name = "Label" & i Then
itm.Caption = Array(i)
i = i + 1
End If
Next
You cannot form an Object Name like Labeli where i is a Variable. But you can loop through all the controls and check if that Label have a Name like Label1 and so on.
Another Method:
For i = 1 To 100
Me.Controls("Label" & i).Caption = Array(i)
Next
What you can to is "build" the name of the Labels like this:
Userform.Controls("Label" & i).Caption
Userform is a Reference to the Userform, if you write it the codebehind of the Userform you can use Me.
But if you have a hundred Labels, there may be better ways to create your Userform, this looks a bit like a XY-Problem

VBA getelementsbytagname issue

Good morning,
I'm attempting to extract HTML table information and collate results on en excel spreadsheet.
I'm using the getelementsbytagname("table")(0) function to extract the HTML table info, which has worked well. Can someone please tell me what is the significance of the (0) after the table?
Also, I have an instance where an opened webpage does not have any table information to process (I don't know this until the page is opened), this leads to an error in my code as I try to initialize my data array to the table dimensions. Is there a way of extracting a result from getelementsbytagname("table")(0), I've tried:-
If (iDom.getelementsbytagname("table")(0) = 0) Then
but this returns a run time error.
Many thanks in advance for your help.
First add reference to Microsoft Internet Controls (SHDocVw) and to Microsoft HTML Object Library:
Then the Object Explorer is your friend:
So getElementsByTagName returns IHTMLElementCollection which has property length. When on the page some elements with specific tag name are found then length is greater then zero. HTH
Dim tables As IHTMLElementCollection
Set tables = doc.getElementsByTagName("table")
If tables.Length > 0 Then
Dim table As HTMLTable
Set table = tables.item(0)
' Because item is the default property of IHTMLElementCollection we can simplyfy
Set table = tables(0) ' this is the same as tables.item(0)
End If
In VBA the appended (0) refers to the first element of an array (assuming an Option Base 0). Here is a short example:
vArr = Array("element 1", "element 2", "element 3")
Debug.Print v(1)
The above code should return element 2 as the second element of a zero-based array.
So, getelementsbytagname("table")(0) refers to the first element of that table. Yet, if the "table" is not found then there is no array to get from that table and getting the first element from that array (by appending (0)) yields an error.
Instead you should test if there is actually a table by that name (before trying to access the array of elements within that table) like so:
If (iDom.getelementsbytagname("table") = 0) Then

Reference SlicerCache by name, not number

I have some code referencing Slicers:
For Each item In wb.SlicerCaches("Segment").SlicerItems
If item.Selected = True Then
If Len(sSegment) > 0 Then sSegment = sSegment & "|"
sSegment = sSegment & item.Caption
End If
Next item
but I get Invalid procedure call or argument. I've seen many examples referencing them by name, but can't get it to work. If I use (1), (3) etc and then add a slicer, it messes up the order, so the code is mucked up.
How can I reference them by name, my end goal is to iterate through selected items.
You may need to reference the slicercache by adding Slicer_ in front of it.
For example, I added an Authors slicer to a table containing information about books and I could reference it by using this code:
Debug.Print ActiveWorkbook.SlicerCaches("Slicer_Author").Slicers("Author").Caption
The reason I knew to add Slicer_ is because I right clicked the slicer and then selected Slicer Settings... and saw this:
And that seems to reference the slicer fine. It was really dumb luck that I happened to see that and thought to try it.
Try:
For Each item In wb.SlicerCaches.Item("Segment").SlicerItems

Filtering a Pivot Table Automatically in Excel

I'm building a DB for school activities in the afternoon. I'm trying to create a search option through a form that controls a pivot table in which the user can filter on the class type and/or the age group and or the school year.
I wrote this code in VBA and it's not working. When i tried to write a code to filter on only one of the above (such as class type) it worked but when I expanded it to 3 filter options it isn't working. It fails when no value is inserted in one of the options.
search_class.Hide
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("class type").ClearAllFilters
If IsNull(Range("type_search").Value) Then
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("class type").CurrentPage = "(All)"
Else: ActiveSheet.PivotTables("PivotSearchClass").PivotFields("class type").CurrentPage = Range("type_search").Value
End If
type_box = "pick a class type"
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("group age").ClearAllFilters
If IsNull(Range("target_search").Value) Then
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("group age").CurrentPage = "(All)"
Else: ActiveSheet.PivotTables("PivotSearchClass").PivotFields("group age").CurrentPage = Range("target_search").Value
End If
target_box = "pick a group age"
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("school year").ClearAllFilters
If IsNull(Range("year_search").Value) Then
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("school year").CurrentPage = "(All)"
Else:
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("school year").CurrentPage = Range("year_search").Value
End If
year_search_box = "pick a school year"
ActiveSheet.PivotTables("PivotSearchClass").PivotCache.Refresh
Does anyone know ehat the problem is and how to fix it?
I think it will work if you change your tests to either:
If IsEmpty(Range("type_search").Value)
or
If Range("type_search").Value = ""
IsNull is used to test whether a variant variable contains a null value, which won't be true with either a blank or filled cell.
I don't have Excel on this computer and pivot tables are my weakness but I'll try to help since you seem alone on this. First make sure your script is indeed running the "True" case of your if statement and then I would edit the value of CurrentPage to see if your issue is there (i.e., change the "(all)" value to something else). Where does the error lie and what is the error description?

Resources