Creating pie chart with array - arrays

I am trying to create a Pie chart based on an array (rather than a range). The array is [11,10,1] (I have other code that populates the array).
Dim type_chart As Chart
Dim type_array(2) As Integer
Set type_chart = Charts.Add
type_chart.ChartType = xlPie
type_chart.SeriesCollection(1).Values = type_array
On the last line of the code above, I receive an 'Invalid Parameter' error.
Also, it doesn't have to use an array, but it cannot use a Range.

Your chart needs to work from a range. Find a blank area you can use. Try something like the following:
my_temp_range = "A10:C10"
ActiveSheet.Range(my_temp_range) = type_array
type_chart.SeriesCollection(1).Values = ActiveSheet.Range(my_temp_range)
Once you're working with a temporary range you may not even need the last line (so long as the size of the array doesn't change). You could just set up the chart in advance instead.

Related

Visio VBA trying to get a list of containers and member shapes

Background: I have some code that runs through a Visio page and returns all the shapes. Many of these shapes are in containers, so I would like to know what container a shape belong to.
Original approach: I was hoping to retrieve the "parent" container of each shape (I only need one level of container, there are no containers within containers) using the Shape.ContainingShape property but that was only returning '0' for every shape.
If anyone has a solution for how I was originally trying to get the container, that would be the most elegant. But since I can't get that to work, I am trying the following alternative, which is also presenting a roadblock.
Current approach: I was able to get a list of all the containers on the page, and now I would like to pull the member shapes for each container. It's not as clean, but it would allow me to cross-reference the shapes and get the containers they belong to.
Issue: I am getting "Error 13 Type Mismatch" when trying to create an array with column 0 being the container name, and column 1 being the member shapes.
' Create array of containers and member shapes
Dim arr() As Long
Dim vsoMemberShape As Shape
Dim vsoContainerShape As Shape
Dim containerArr() As Long
Dim rows As Integer
Dim i As Integer
For Each ContainerID In vsoPage.GetContainers(visContainerIncludeNested)
Set vsoContainerShape = vsoPage.Shapes.ItemFromID(ContainerID)
arr = vsoContainerShape.ContainerProperties.GetMemberShapes(1)
rows = UBound(arr)
ReDim containerArr(0 To rows, 0 To 1)
For i = 0 To UBound(arr)
Set memberShape = vsoPage.Shapes.ItemFromID(arr(i))
containerArr(i, 0) = vsoContainerShape.NameU
containerArr(i, 1) = vsoMemberShape.NameU
Next
Next
' The following code is in a For loop, not shown
' shapeToName is what I want to compare to the member shapes in the container
' array defined above, and then retrieve the corresponding container
' This is where the error is popping up
shapeToName = CStr(vsoShapeTo.Name)
Dim x As Integer
x = Application.Match(shapeToName, Application.Index(containerArr, 0, 1), 0)
shapeContainer = containerArr(x, 1)
I think what you're looking for is Shape.MemberOfContainers, which returns an array of containers that a shape is a member of.
You can also have a look at this post which covers the same issue:
Visio: How to get the shapes that are contained in one shape?
I'll also throw in a link to a post by David Parker that covers containers in the context of cross-functional flowchart, which makes good use of Containers and Lists:
https://bvisual.net/2009/09/07/visio-2010-containment-and-cross-functional-flowcharts/

Visio VBA - How can I distribute shapes with a known, fixed distance

I'd like to place all currently selected shapes into an array. I'd then like to sort that array so I can find either the top most or left most shape in the array. I'd then like to use that shape as my starting point, and then from there align the other shapes a fixed, known distance apart. I've tried to place the shapes into an array like so:
Dim numShapes As Integer, i As Integer
Dim arrShapes As Visio.Selection
numShapes = Visio.ActiveWindow.Selection.Count
For i = 1 To numShapes
arrShapes(i) = Visio.ActiveWindow.Selection(i)
Next i
I have tried to create the array with no type specification, specifying as variant, and as in this example as selection. I don't know if I can put them into a list of some kind either? Obviously I can't get to the point of sorting the array and then distributing my shapes until I can get the array to populate. I'm placing a break point in the code and I have the "Locals" window open and I can see that the array is not being populated.
Update:
Why does this work,
Dim Sel As Visio.Selection
Dim Shp As Visio.Shape
Set Sel = Visio.ActiveWindow.Selection
For Each Shp in Sel
Debug.Print Shp.Name
Next
And this does not?
Dim i As Integer
Dim Shp As Visio.Shape
For i = 1 To Visio.ActiveWindow.Selection.Count
Set Shp = Visio.ActiveWindow.Selection(i)
Debug.Print Shp.Name
Next i
Regards,
Scott
There was a couple of problems in your code - fixing only one would not have got you any further in understanding if you had actually fixed anything.
Your arrShapes is declared as a general object - the Selection
Object is one of those objects that is the Jack of all trades, and
master of none.
You didn't "Set" when assigning to the array.
I don't have Visio on this machine, so cannot directly test the code below. I am also assuming that all items selected are shapes (usually a safe assumption in Visio).
Dim numShapes As Integer, i As Integer
Dim arrShapes() As Shape ' Set this up as an array of shape
If Visio.ActiveWindow.Selection.Count > 0 then ' don't want to cause a problem by setting the array to 0!
ReDim arrShapes(Visio.ActiveWindow.Selection.Count)
numShapes = Visio.ActiveWindow.Selection.Count ' while not really necessary it does help explain the code.
For i = 1 To numShapes
' must Set as we want the reference to the shape, not the default value of the shape.
Set arrShapes(i) = Visio.ActiveWindow.Selection(i)
Next i
Else
MsgBox "No shapes selected. Nothing done." ' soft fail
End If

Find Array index that contains string

I have file with tags and targets, this is example:
TAG1|TARGET1,TARGET2
TAG2|TARGET3,TARGET4
I start by creating String Array using File.ReadAllLines
Dim MAIN As String() = File.ReadAllLines("")
At some point I have one of targets and I need to know what was the tag index (which array line is it), so for example if I have TARGET3 I want to know it's in second line so it's in MAIN(1) and then I can grab TAG = TAG2.
I can't get it working, I tried few methods:
Array.IndexOf(MAIN,"TARGET3")
always returned -1, it worked with full string tho,
Array.IndexOf(MAIN,"TAG2|TARGET3,TARGET4")
returned 1. I tried with Array.FindIndex, was the same.
So my question is: how to get index of partial array item. Thank you for any help.
You can use Linq to search your array in this way
Dim search = "TARGET3"
Dim line = MAIN.FirstOrDefault(Function(x) x.Contains(search))
This will return directly the line with the matching word

Reading array from Spreadsheet Data VBA

I'm sending a range into an array in VBA. But, when I try to reference parts of the array, I get a "Subscript Out of Range" error. I know that the range is successfully being transferred, because I can then send the array back into a different range.
Dim LastClmn() As Variant 'The last column of brake data
Set RangeSet = ws.Range("RJ2:RJ" & ii)
LastClmn() = RangeSet
Msgbox LastClmn(4)
Referencing a piece of the array is what causes the error
Try LastClmn(4,1)
I found it by using the Locals Window under View in the menu bar.

Matlab: Unable to store information into array

Im trying to store values from radialspeed (a function from the phased array toolbox) into an array, but im getting errors:
Conversion to cell from double is not possible.
Error in modelCar (line 40)
Cell(1,T)= Rspeed;
^^Error Message
Cell = cell(1,12)
for T = 1:11
[POS,v] = step(H,T);
Rspeed = radialspeed(POS,v,[25; 25; 70],[0; 0; 0]);
typecast(Rspeed,'uint16');
Cell(1,T)= Rspeed;
%%Rspeed = Vel.Radspeed(:,T);
disp(Rspeed);
end
^^^Excerpt of the code im using.
Another question any tips to plot a graph continuously while in the loop, the draw now function doesn't seem to work
Thank you.
You should not use Cell as a variable since cell is reserved keyword in MATLAB. Though using Cell will pose no problems, but a simple typing mistake can inject errors into your code. You may use myCell, R_cell etc.
By writing, Cell(1,T)= Rspeed you are trying to assign Rspeed of type double to a cell datatype. You should write Cell{1,T}=Rspeed or Cell{T}=Rspeed. You can also visualize your output for each iteration as follows:
Replace disp(Rspeed) by:
hold on;scatter(T,Rspeed,'ro');
pause(0.001);
1st question: Cell(1,T) will return a cell so you need to change your code to Cell{T}= Rspeed;.
2nd question: recall plot is a possible solution if speed is not a main concern.

Resources