Dynamic Graph in excel 2007 - excel

I want to graph the result of my table in excel. here is the code that I have :
Sub bow()
'
' bow Macro
'
'
Worksheets("Sheet3").Select
tahX = Cells(3, 3).Value + 2
Xval = Offset(A1, 1, 2, 1, 1)
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Sheet3'!$12:$12,'Sheet3'!$10:$10")
ActiveChart.ChartType = xlXYScatter
ActiveChart.SeriesCollection(1).Name = "=""bowe"""
ActiveChart.SeriesCollection(1).XValues = "='Sheet3'!$12:$12"
ActiveChart.SeriesCollection(1).Values = "='Sheet3'!$10:$10"
End Sub
now I want to limit the X-values to the value that is defined in the cell C3. So for example if C3 = 20, I want it to only use the first 20 values in the row number 12. How can I do that ?

Sina ,
I'm not sure what exactly your problem is, but in this line:
ActiveChart.SeriesCollection(1).XValues = "='Sheet3'!$12:$12"
you are specifying row 12 to be plotted.Instead of !$12:$12 you can have !$C$12:$L$12 which will set range from C3 to L12 .If you want to set the row dynamically you can use :
Set mcStartX = Worksheets(1).Cells(12, 3)
startColX = mcStartX.Address()
//we assume that in cell C3 we have specified number of cells to be plotted
toAdd = Cells(3, 3).Value
Set mcEndX = Worksheets().Cells(12, 2 + toAdd)
endColX = mcEndX.Address()
Using this code, and assuming that you have 10 in C3, you will get $C$12 from startColX and $L$12 from endColX.The rest is straight forward you can use following code:
ActiveChart.SeriesCollection(1).XValues = "='Sheet3'!" + startColX + ":" + endColX
I hope that this could be helpful.
Cheers,

Related

Unable to plot values on scatterplot using vba

I am relatively new to VBA. I am attempting to automate the generation of a graph containing Beta values using the following steps:
Step 1 - Generate a list of random numbers in Column A
Step 2 - Based on values in Column A, apply the Beta.Dist formula in Column B
Step 3 - Generate a scatterplot based on values in Column B, the scatterplot should look like a U-shaped distribution curve
I've tried using the following code but I am only seeing 1 value in the scatter plot (i.e., the scatterplot only has one dot).
Here's the code:
Option Explicit
Option Base 1
Sub Macro1()
Application.ScreenUpdating = False
Dim i%, j%
Range("A1").Select
Selection = "0.99"
Selection.Offset(0, 1) = "Beta(" & Selection & ", " & Selection & ") Density"
For i = 1 To 301
Selection.Offset(i, 0).Formula = "= rand()"
Selection.Offset(i, 1).Formula = "=BETA.DIST(A2:A302,0.99,0.99,False)"
Next i
j = ActiveSheet.ChartObjects.Count
ActiveSheet.Shapes.AddChart2(-1, xlXYScatter, 98 + j * 10, 16.5 + j * 10, 319, 296.5).Select
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("B2:B302")
ActiveSheet.ChartObjects(j + 1).Activate
ActiveChart.SetElement (msoElementChartTitleAboveChart)
Selection.Caption = "="
ActiveChart.Axes(xlCategory).MaximumScale = 1
ActiveChart.Axes(xlCategory).Format.Line.Visible = msoFalse
ActiveChart.Axes(xlCategory).MajorGridlines.Delete
ActiveChart.Axes(xlValue).MaximumScale = 1.1
ActiveChart.Axes(xlValue).TickLabels.NumberFormat = "0.000#"
ActiveChart.Axes(xlValue).Format.Line.Visible = msoFalse
ActiveChart.Axes(xlValue).MajorGridlines.Delete
Range("E1") = ""
Range("I1") = ""
Range("B1").Select
Application.ScreenUpdating = True
End Sub
I'm hoping to get a U-shaped beta distribution curve in the scatter plot. Appreciate any form of help and thank you in advance!
You're not setting the chart data correctly:
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A2:B302")

Excel - Record cell values, transpose them into columns and copy dynamically changing cells

English isn't my first language so I hope you will understand me
Beacuse I dont know anything about programming I am turning for help to you: Excel pros;)
I have a workbook with two sheets ( datafeed and record )
On "datafeed", column B, cells B2 to B400 I am capturing live prices from internet.
Currently I use the following :
Sub my_onTime()
Application.OnTime Now + TimeValue("00:00:01"), "my_Procedure"
End Sub
Sub my_Procedure()
With Sheets("record")
rw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Range(.Cells(rw, 1), .Cells(rw, 2)).Value = Sheets("datafeed").Range("a2:b2").Value
End With
ThisWorkbook.RefreshAll
my_onTime
End Sub
which currently records prices from "datafeed" to rows in "record".
Another problem is that records only one cell from "datafeed"; cell B2 . I dont know how to set it, so it would record all prices in range from cells B2:B400
My wish is that Excel would record price changes to"record" Sheet into columns not rows.
"Graphic" display what I want:
Sheet1 - "datafeed" ... B2 = 155............Sheet 2 - "record" cells : F2 = 155.....G2 = 150.....H2 - 145.....I2 - 140,....and so on
Sheet1 - "datafeed" ... B3 = 66.............Sheet 2 - "record" cells : F3 = 66.....G3 = 67....H3 - 66.....I3 - 65,....and so on
Sheet1 - "datafeed" ... B4 = 1015.............Sheet 2 - "record" cells : F4 = 1015.....G4 = 1025....H4 - 1035....I4 - 1045,....and so on
Also last recorded price must be put in first column Sheet 2 - "record"; cells: , F2,F3,F4.......(Right now last recorded value is put in last row )
Any help will be greatly appreciated.
Thank you!
p.s
I have added 3 pics for clarification
1. sheet datafeed
2. sheet record
3. what i wish for sheet record
replace this part of your procedure
With Sheets("record")
rw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Range(.Cells(rw, 1), .Cells(rw, 2)).Value = Sheets("datafeed").Range("a2:b2").Value
End With
with this
With Sheets("record")
rw = .Cells(.Rows.Count, 5).End(xlUp).Row + 1
.Cells(rw, 5).Value = Sheets("datafeed").Cells(2, 1).Value 'write name from column A
iColumn = 6 'set you want to start in column F with prices
For iRow = Sheets("datafeed").Cells(Sheets("datafeed").Rows.Count, 1).End(xlUp).Row To 2 Step -1
.Cells(rw, iColumn).Value = Sheets("datafeed").Cells(iRow, 2).Value
iColumn = iColumn + 1
Next iRow
End With
if you want the new line added to first line instead of the last
With Sheets("record")
iColumn = 6 'set you want to start in column F with prices
iRecordRow = 3 'set to what row number to put the current price
.Rows(iRecordRow & ":" & iRecordRow).Insert Shift:=xlDown
For iRow = Sheets("datafeed").Cells(Sheets("datafeed").Rows.Count, 1).End(xlUp).Row To 2 Step -1
.Cells(iRecordRow, iColumn).Value = Sheets("datafeed").Cells(iRow, 2).Value
iColumn = iColumn + 1
Next iRow
.Cells(iRecordRow, 5).Value = Sheets("datafeed").Cells(2, 1).Value 'write name from column A
End With
if you want it added to different row than 3 just change number in iRecordRow = 3

How can I get constant from formula at Excel VBA?

I added a trendline and get its linear equation value as y = ax + b form. In excel VBA how can I get constant a from this formula?
In formulas
The following equations assume that your sheet has two named ranges: x and y. Then:
a = SLOPE(y,x)
b = INTERCEPT(y,x)
Source: Chart Trendline Formulas
Without named ranges, you use for example:
a = SLOPE(B2:B22, A2:A22)
b = INTERCEPT(B2:B22, A2:A22)
In VBA
rangeX = Range(Cells(2, 1), Cells(22, 1))) ' OR: rangeX = Range("A2:A22")
rangeY = Range(Cells(2, 2), Cells(22, 2))) ' OR: rangeY = Range("B2:B22")
a = Application.WorksheetFunction.Slope(rangeY, rangeX)
b = Application.WorksheetFunction.Intercept(rangeY, rangeX)
you can read the actual formula by turning on the label
then read the label itself and parse it
Sub readFormula()
Dim ttt As Trendline
Set ttt = ActiveChart.FullSeriesCollection(1).Trendlines(1)
ttt.DisplayEquation = True
Debug.Print ttt.DataLabel.Caption
Debug.Print ttt.DataLabel.Formula
Debug.Print ttt.DataLabel.FormulaLocal
Debug.Print ttt.DataLabel.FormulaR1C1
Debug.Print ttt.DataLabel.FormulaR1C1Local
Debug.Print ttt.DataLabel.Text
ttt.DisplayEquation = False
end sub

VBA Excel filter specific string content in a whole column and paste it in another column

I have a matrix of aprox 5000 rows that looks something as seen on im1 (not the real matrix)
im1
Basically I coded something that inserts 3 empty columns to the right of wire/hose/type with the respective names "Wire, Size and Color" written in the top cell of the column
The column Wire/hose/Type contains 3 types of information
the first would be the 4 character long string that start with a number (like 3FEC or 4LOP). this is a pattern
the second is a number like 2.5 or 0.50
the third is the string that contains one or two colors (GN or GN/BN for example)
What I want to do is Identify the first type of information in every cell of the column and paste it on "Wire", to paste the second type of information on "size" and the third on "color" and finally deleting the column Wire/hose type so finally it looks something as seen on im2:
im2
If anyone could please guide me a little on filling at least the first parameter so i can fill the rest with the same reasoning I would really appreciate it, don't mean to sound cliche but I'm kind of new in this coding environment
my code so far looks like this:
Sheet3.Activate
For i = 1 To 4
celltxt2 = ActiveSheet.Cells(1, i).Text
If InStr(1, celltxt2, "Wire/Hose/Tube type") Then
Columns(i + 1).EntireColumn.Select
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
End If
Next i
o = 1
Do While IsEmpty(Cells(1, o)) = False
o = o + 1
Loop
Set wr = Cells(1, o)
wr.Value = "Wire"
Set sz = Cells(1, o + 1)
sz.Value = "Size"
Set clr = Cells(1, o + 2)
clr.Value = "Color"
Thank you!
This is the way to do it.
Sub ProcessData()
Dim x As Long, y As Long
Dim r As Range
Dim values As Variant
With Sheet3
For y = 1 To 4
If InStr(1, Cells(1, y).value, "Wire/Hose/Tube type") Then
.Columns(y).Resize(, 3).Insert Shift:=xlToRight
Exit For
End If
Next
y = y + 3
For Each r In .Range(.Cells(2, y), .Cells(.Rows.Count, y).End(xlUp))
values = Split(Replace(r.value, ":", "-"), "-")
r.Offset(0, -3).value = values(1)
r.Offset(0, -2).value = values(2)
r.Offset(0, -1).value = values(3)
Next
.Cells(1, y - 3).Resize(1, 3).value = Array("Wire", "Size", "Color")
.Columns(y).Delete
End With
End Sub

Excel VBA, having multiple column inputs as XYscatter chart Series name in dynamically generated chart

I'm trying to automate the generation of a dynamic XYscatter chart using VBA. My data are in sets of 12 individual data per trial and the number of trial will vary. Firstly by scanning the csv file to locate the last set of data, and by removing the first line of the tag, i divided it by 12 to determine the number of sets available and then populate the data into to graph with the corresponding data points and series name. I've got most of the code working but I'm having some syntax issue on including a range data for series name. The series name will only work if I am selecting a column instead of a range of column data.
How can I modify my code to have multiple column input as my Series Name?
Sub PlotSelect()
Dim myChart As Chart
DataRow = 1
SelectRow = 2
With ActiveSheet
'To count the number of rows to determine number of sets of data
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Row = LastRow - 1
N = Row / 12
'Creating a dummy chart before repopulating the data points
Range("A1:B2").Select
Set myChart = ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmooth).Chart
' delete all the dummy series
For i = myChart.SeriesCollection.Count To 1 Step -1
myChart.SeriesCollection(i).Delete
Next
'Populating chart with data
Do While DataRow <= N
If DataRow <> -1 Then
myChart.SeriesCollection.NewSeries
myChart.SeriesCollection(DataRow).Name = Range(ActiveSheet.Cells(SelectRow, 2), ActiveSheet.Cells(SelectRow, 5))
myChart.SeriesCollection(DataRow).Values = Range(ActiveSheet.Cells(SelectRow, 9), ActiveSheet.Cells(SelectRow + 11, 9))
myChart.SeriesCollection(DataRow).XValues = Range(ActiveSheet.Cells(SelectRow, 8), ActiveSheet.Cells(SelectRow + 11, 8))
End If
DataRow = DataRow + 1
SelectRow = SelectRow + 12
Loop
End Sub
Instead of using a range of cells to generate the Name for the data entry, I merge the data in the cells and copy the content into another cell and select the new cell with the concatenated cells. Later i delete the temporary cell.
Do While DataRow <= 2
If DataRow <> -1 Then
myChart.SeriesCollection.NewSeries 'To add new data entries
**Range("B9999").Select
ActiveCell.FormulaR1C1 = ((Cells(SelectRow, 4)) & "_" & (Cells(SelectRow, 5)) & "_" & (Cells(SelectRow, 6)))
myChart.FullSeriesCollection(DataRow).Name = Range("B9999")**
myChart.SeriesCollection(DataRow).Values = Range(Cells(SelectRow, 2), ActiveSheet.Cells(SelectRow + 600, 2))
myChart.SeriesCollection(DataRow).XValues = Range(Cells(SelectRow, 1), ActiveSheet.Cells(SelectRow + 600, 1))
End If
DataRow = DataRow + 1
SelectRow = SelectRow + 601
Loop
Range("B9999").Delete 'to delete temp data
ActiveWindow.ScrollRow = 2 'to reset spreadsheet view
The modifications are located in the ** ** range

Resources