X,Y Scatter Plot with Animation - excel

I'm looking for a way to graph multiple point within an (X,Y) plane, and have them appear (then disappear) one at a time. I have a total of about 400 (x,y) points, which are position tracking information gathered from a Inertial Measurement Unit.
Each data point is separated by approx 4 ms (I can change this interval to make it longer), so if possible, I would like to display one dot at a time, each for 4ms, until the next dot appears. The final product should display the object's movement path over the 5-6 second sample time. From my excel file, I have two columns (one for X and one for Y) which are 400 elements long (400 rows).
I'm hoping for a way to plot the X,Y scatter information one row at a time, and looking for some code that will increment through each row and plot the corresponding scatter. I am a semi-fluent programmer, but have never used MS Visual Basic before. All the solutions for Excel that I have found so far (which are similar to my problem) involve writing code for the graph in Visual Basic. I think I may be able to get that working, if someone can help me with the code... but if there is another piece of software which does this (for free! I'm on a student budget!) then I am willing to try multiple solutions.
I found this code already:
Sub Macro1()
Charts.Add
ActiveChart.ChartType = xlXYScatter
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A3:B3"), PlotBy _
:=xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
With ActiveChart.Axes(xlCategory)
.MinimumScale = -30
.MaximumScale = 30
.MinorUnit = 1
.MajorUnit = 5
.Crosses = xlAutomatic
.ReversePlotOrder = False
.ScaleType = xlLinear
.DisplayUnit = xlNone
End With
With ActiveChart.Axes(xlValue)
.MinimumScale = 0
.MaximumScale = 2800
.MinorUnit = 50
.MajorUnit = 100
.Crosses = xlAutomatic
.ReversePlotOrder = False
.ScaleType = xlLinear
.DisplayUnit = xlNone
End With
For I = 3 To Worksheets(1).Range("A65536").End(xlUp).Row
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A3:B" & I), PlotBy _
:=xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
ST = Timer
While Timer < ST + 1
Wend
Next I
End Sub
However, being as there was no comments or documentation, I had difficulty perusing through it and picking it apart. I feel like once I can really understand the format of Visual basic I can modify the program to adapt for my specific needs... but understanding it is the first step.
So again, my information is in the format of 3 columns:
X displacement -- Y displacement -- Timestamp
(position in mm) (position in mm) (seconds, or iteration #, whichever is easier)

Related

Add Boundary Condition for Goal Seek

I am trying to create an automated Goal Seek script for a interlinked cells and workbook. However, perhaps due to the complexity and number of interlinks, somehow under a certain condition the Goal Seek function converges at a very high or low x-value.
Is there a way to improve its accuracy by setting some kind of boundary (a < x < b) similar to that in Solver. The reason I don't want to add solver in VBA is that because some of the other users may not be activating their Solver add-ins.
This is what the Goal Seek value gives me for an initial guess of x =
0.5h = 500
This is what the X-value should be, with a random guess of x = 100
Another alternative that I could think about is to create some sort of manual iteration (e.g. Bisection method) Sub Routine, but again, the equations are pretty complex so this may not be ideal.
What I am doing at the moment is that to preset an initial value for the x if y (another parameter) is negative or positive. I reckon this has eliminated most of the invalid result, but it still gives an error on one or two occasion. Appreciate your input. Thanks.
Sub Guess()
' ------------- For Guessing Initial X-Value -----------
Dim i As Integer, j As Integer
For i = 4 To 11
For j = 18 To 25
If Worksheets("Crack Width").Range("I" & j) < 0 Then
'------------------------Pre-guess X_value to be 0.5X_bal if N<0-------------
Worksheets("Calcs").Range("B" & i) = Worksheets("Calcs").Range("C" & i).Value * 0.5
If Worksheets("Calcs").Range("B" & i) = 0 Then Worksheets("Calcs").Range("B" & i).ClearContent
i = i + 1
ElseIf Worksheets("Crack Width").Range("I" & j) >= 0 Then
'------------------------Pre-guess X_value to be 0.5h if N>0-------------
Worksheets("Calcs").Range("B" & i) = Worksheets("Calcs").Range("E" & i).Value * 0.5
If Worksheets("Calcs").Range("B" & i) = 0 Then Worksheets("Calcs").Range("B" & i).ClearContents
i = i + 1
End If
Next j
Next i
End Sub

Fill the whole container of grouped shapes - Excel

I have create some shapes in Excel, group them together and i can not find how to apply fill color on the whole group. Not on the shapes but on the background.
Any help?
In short, this isn't possible on a shape group.
Detailed explanation:
From https://learn.microsoft.com/en-us/office/vba/api/excel.groupshapes
The GroupedShapes object "Represents the individual shapes within a
grouped shape."
Because the group itself only represents the shapes within it, it doesn't have its own fill colour as such, only the fill colours of the shapes within it.
So, as I guess you've discovered, if you do something like this:
With Sheet1.Shapes(1).GroupItems.Parent
.Fill.ForeColor.RGB = RGB(255, 0, 0)
End With
Then you are telling the group to apply the Forecolor to every shape within the group, and not the group object itself.
If you wish to have a background colour to the group, then a workaround would be to create a rectangle at the back, and set the colour of that instead.
Update: Workaround Example
If you wish to implement my suggested workaround, then the following will get you there. You will need to adjust for colours / workbook / worksheet / shape group name etc. There might be a prettier way, but I have this working...
Const shapeGroupName As String = "ShapeGroup"
Const shapeGroupBGName As String = "ShapeGroupBG"
Const shapeGroupMargin As Single = 5
Dim x As Integer
Dim y As Integer
Dim h As Integer
Dim w As Integer
Dim shapeCount As Integer
Dim shapeCol() As String
With Sheet1.shapes.Range(Array(shapeGroupName))
ReDim Preserve shapeCol(.GroupItems.Count)
For shapeCount = 1 To .GroupItems.Count
shapeCol(shapeCount) = .GroupItems.Item(shapeCount).Name
Next
y = .Top - shapeGroupMargin
x = .Left - shapeGroupMargin
h = .Height + shapeGroupMargin * 2
w = .Width + shapeGroupMargin * 2
.Ungroup
End With
shapeCol(0) = shapeGroupBGName
With Sheet1.shapes.AddShape(msoShapeRectangle, x, y, w, h)
.Name = shapeGroupBGName
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Line.DashStyle = msoLineDashDot
.ZOrder msoSendToBack
End With
With Sheet1.shapes.Range(shapeCol).Group
.Name = shapeGroupName
End With
This works by getting the dimensions of the group, and the names of the shapes within it. Next it ungroups the shapes, adds a rectangle behind the existing shapes, and then regroups accordingly.
A couple of notes:
I have created an arbitrary margin size, as the dimensions of the group are actually the top left and bottom right of the objects within it. In excel, the GUI adds a nice bit extra on to this, so its up to you to set this to what you want.
You can also implement a different shape (i.e. a rounded rectangle instead if desired etc.).
If you have multiple shape groups, then the above could be modified easily to accommodate this.

Problem with comparing negative numbers in vba

I'm still new at VBA and I'm sure that this is a simple problem, but I'm comparing negative numbers who is lesser for example if I inputed textboxMIN to -7 and textboxMAX to -1 the result will be -1 will be less than -7. I don't know how to solve it because for me the condition is correct but the result isn't. Please take a look if there is a problem with my code
'/************PROCESS***************/
For t_int_iteratorI = 0 To txt_NumOperands.Value - 1
MultiPage1.Pages.Add
MultiPage1.Pages(t_int_iteratorI).Caption = "Variable" & t_int_iteratorI + 1
Call sub_LabelPerPage
Set p_var_SetTxtBox = frm_RangeForm.MultiPage1.Pages(t_int_iteratorI).Controls.Add("Forms.TextBox.1", "MinBox")
With p_var_SetTxtBox
.Top = 50
.Left = 100
End With
Set p_var_SetTxtBox = frm_RangeForm.MultiPage1.Pages(t_int_iteratorI).Controls.Add("Forms.TextBox.1", "MaxBox")
With p_var_SetTxtBox
.Top = 50
.Left = 300
End With
Next t_int_iteratorI
p_var_MaxValue = frm_RangeForm.MultiPage1.Pages(t_int_iteratorI).maxbox.Value
p_var_MinValue = frm_RangeForm.MultiPage1.Pages(t_int_iteratorI).MinBox.Value
If p_var_MinValue > p_var_MaxValue Then
MsgBox "MIN value should be lesser than MAX value."
Exit Sub
End If
Thank you in advance!

Select alternate points and move labels above/below

I am fairly new to VBA and trying to select alternating points to place datalabels above and below.
Here is my code that is currently placing a datalabel below point 1 which I want, but then I want the 3rd point's label to be placed below as well, and the other ones above. I have tried many different loops and codes but nothing seems to work and I'm not sure why it seems to copy and paste instead of move the label.
For x = 1 To ActiveChart.SeriesCollection(1).Points.Count
With ActiveChart.SeriesCollection(1).Points(x).DataLabel
.Position = xlLabelPositionBelow
.Orientation = xlHorizontal
End With
x = x + 2
Next x
For x = 2 To ActiveChart.SeriesCollection(1).Points.Count
With ActiveChart.SeriesCollection(1).Points(x).DataLabel
.Position = xlLabelPositionAbove
.Orientation = xlHorizontal
End With
x = x + 2
Next x
This is what my code currently produces:
Here is what I would like it to do:
I feel like it is something simple that I am missing if this is possible. So any help would be greatly appreciated. Is it possible there is an easier way?
Thank you in advance.
The problem appears to be that you are 'over-iterating' x. Where you want x to go up by two, you're actually saying "x = x + 2" and THEN also saying "+ 1 x" (which is what Next does). You could solve this above by changing your For Loops to say "For x = 1 to 3 Step 2". Then when you loop with "Next x", it will add 2 instead of just 1.
However, I recommend you do it like the following, as it is (in my opinion) a little clearer that you want something for an even x, and something for an odd x:
For x = 1 To ActiveChart.SeriesCollection(1).Points.Count
With ActiveChart.SeriesCollection(1).Points(x).DataLabel
If x Mod 2 = 1 Then 'If x is odd, put label below point
.Position = xlLabelPositionBelow
.Orientation = xlHorizontal
Else 'if x is even, put label above point
.Position = xlLabelPositionAbove
.Orientation = xlHorizontal
End If
End With
Next x
ActiveChart.SeriesCollection(1).Points(1).DataLabel.Position = xlLabelPositionBelow
ActiveChart.SeriesCollection(1).Points(2).DataLabel.Position = xlLabelPositionAbove
ActiveChart.SeriesCollection(1).Points(3).DataLabel.Position = xlLabelPositionBelow
For x = 4 to ActiveChart.SeriesCollection(1).Points.Count
ActiveChart.SeriesCollection(1).Points(x).DataLabel.Position = xlLabelPositionAbove
Next

Shape.Top and Shape.Left Issues

I have some code that looks at the position of a line that a user can edit. The purpose of editing this line is to get rough measurements from a plot plan which works so far. I'm working on making it much more interactive by recording measured positions as well.
Here's the issue. When this shape (a connector elbow) is on the cabinet and the access point it records its position in points using shape.top and shape.left (code below). When I resize this line to put the access point end on another access point, the cabinet position changes as well even though it didn't move. I did notice that the scale height and width changed on the drawing but I can't figure out why that would affect the initial point.
It's worth noting that as you rotate the elbow connector the value of width and height rotate. That means sometimes height is up and down and sometimes its the value you'd expect width to be. Still the left position only stays constant when the connector is rotated 180 degrees.
Is there a relation between scale height/width and the top/left value?
Sub Measure()
Set sp = ActiveSheet.Shapes("Measurement")
Msgbox(sp.Top & "//" & sp.Left)
end sub
Edit: So I realize I mentioned a lot about the program, the real problem is why do the top left measurements change despite the top left staying stationary on the screen? And only in the 270/90 rotation (happens automatically depending how you drag the line)
Add Screen Shots (Measurements are Top then left)
This is the first screen shot with a 270 rotation
This is the second, notice the top left stayed stationary but the points changed
This next group is with a 180/0 rotation (shape auto rotates, otherwise I would just lock it and be done).
Rotated 180 first screen shot
Rotated 180 second screen shot
I sympathized with your problem recognition and I did not understand this phenomenon, but after testing, it seems that the original coordinates are preserved even if the figure is rotated. This is because even if the rotation is performed again, the rotation is performed by the original coordinate value. If you artificially align the position, the coordinates appear to move accordingly. I tested it as follows.
Sub Measure()
Dim rngT As Range
Dim sp As Shape
Set sp = ActiveSheet.Shapes("Measurement")
'MsgBox (sp.Top & "//" & sp.Left)
Set rngT = Range("k" & Rows.Count).End(xlUp).Offset(1, 0)
rngT = sp.Top
rngT.Offset(, 1) = sp.Left
End Sub
Sub test()
Dim sp As Shape
Dim Ws As Worksheet
Dim vDB
Dim i As Integer, t As Single, l As Single
Dim r As Integer
vDB = Range("k2", Range("L" & Rows.Count).End(xlUp))
Set Ws = ActiveSheet
r = UBound(vDB, 1)
For i = 1 To r
t = vDB(i, 1)
l = vDB(i, 2)
Set sp = Ws.Shapes.AddShape(msoShapeRectangle, l, t, 10, 10)
Next i
End Sub
I figured it out guys.
So in orientation 0 or 180 the Shape.top and Shape.Left give accurate consistent measurements.
In the 90 or 270 the top and left have to be adjusted to give accurate consistent measurements.
The code is Shape.Top-((Shape.Width-Shape.Height)/2) and Shape.Left+((Shape.Width-Shape.Height)/2)
(NOTE: THIS ADJUSTMENT ONLY APPLIES WHEN OBJECT IS ROTATED 90 or 270//Shape.Rotation)
Also, the shape used here is an elbow connector

Resources