I am trying to update my sourcedata in a chart. I Don't want a dynamic range because the chart will pull each time from the new range with each button click. If I use a dynamic or named range, I am locked into that range.
I have tried:
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("A1:C3").Offset(0,3)
This only works once, though. To be clear, I have a button to update a chart on a spreadsheet. When the button is clicked, the macro will look at the current data in the active chart and shift the source over 3 columns. This will happen each time the button is clicked and the data must stay on the sheet. I've exhausted everything I know and can find online the Goracle search.
Any help?
Thanks in advance,
Lilith
The SeriesCollection(j) of the ActiveChart has always a Formula property that looks like this:
=SERIES(,,Sheet1!$A$1:$C$3,1)
So, you can add a custom function to your project that parses the string Split by $ and returns the corresponding range shift of 3 columns:
Function ShiftBy3(ByVal formulaString As String) As String
cRange = Split(formulaString, "$")(1) + Split(formulaString, "$")(2) + Split(formulaString, "$")(3) + Split(Split(formulaString, "$")(4), ",")(0)
newAddress = Range(cRange).Offset(0,3).Address
ShiftBy3 = Split(formulaString, "$")(0) + newAddress + "," + Split(Split(formulaString, "$")(4), ",")(1)
End Function
and then shifting by just resetting the formula each time:
ActiveChart.SeriesCollection(1).Formula = ShiftBy3(ActiveChart.SeriesCollection(1).Formula)
One approach is to update the Formula property for each series on the chart:
Sub Tester()
Const SER As String = "=SERIES("
Dim s As Series, f As String, arr, rng1, rng2, sn
Dim cht As Chart
Set cht = ActiveSheet.ChartObjects("Chart 2").Chart
sn = ActiveSheet.Name
For Each s In cht.SeriesCollection
f = s.Formula
Debug.Print f '<<<added
f = Replace(f, SER, "")
f = Left(f, Len(f) - 1)
arr = Split(f, ",")
Set rng1 = Range(arr(1))
Set rng2 = Range(arr(2))
s.Formula = SER & Join(Array(arr(0), _
rng1.Parent.Name & "!" & rng1.Offset(0, 3).Address(), _
rng2.Parent.Name & "!" & rng2.Offset(0, 3).Address(), _
arr(3)), ",") & ")"
Next s
End Sub
Here is the working code my boss came up with.
Dim var
var = Cells(4, 4)
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range(Cells(1, 1 + var), Cells(3, 3 + var))
Cells(4, 4).Value = Cells(4, 4).Value + 3
Thank Tim and Matteo for all of you time and help. I really do appreciate it!
Related
Hi I'm trying to create a VBA code that fills the entire column G values with Column E - Column F (so E2-F2 = G2) but I keep getting mismatch error. The values start from the second row and I have created a loop to run down the columns.
This is the code I have so far.
Sub RemainingHours()
Dim i As Integer
i = 2
With Sheets("Opt")
While Not IsEmpty(Cells(5, i).Value)
Cells(7, i).Value = Cells(6, i).Value - Cells(5, i).Value
i = i + 1
Wend
End With
End Sub
Thank you!
Rather than looping you could try using Evaluate.
Sub RemainingHours()
Dim rng As Range
Dim Res As Variant
With Sheets("Opt")
Set rng = .Range("E2", .Range("E" & Rows.Count).End(xlUp))
End With
Res = Evaluate(rng.Offset(, 1).Address & "-" & rng.Address)
rng.Offset(, 2).Value = Res
End Sub
I try to create dynamic chart with VBA , for example I have 5 students I need to create 5 chart
for each students .
Sub Macro4()
Rows("2:2").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Range("Sheet1!$2:$2")
Rows("3:3").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Range("Sheet1!$3:$3")
Rows("4:4").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Range("Sheet1!$4:$4")
Range("D12").Select
ActiveWorkbook.Save
End Sub
I create this using Macro only for test but I try do that as dynamically because if I have more then 100 students it will be difficult , etc..
I hope you guys help me
Thanks
Try the next way, please. No need of any selection. Selection is useless, it only consumes Excel resources:
Sub AddCharts()
Dim sh As Worksheet, ch As Shape, chartNo As Long
Dim prevWith As Long, i As Long
Set sh = ActiveSheet 'the sheet where the charts to be created
'chartNo = 5
chartNo = = Worksheets("Sheet1").Range("A" & rows.count).End(xlUp).row - 1
For i = 1 To chartNo
Set ch = sh.Shapes.AddChart
ch.Chart.ChartType = xlColumnClustered
ch.Chart.SetSourceData Source:=Range("Sheet1!$" & i + 1 & ":$" & i + 1)
ch.Chart.Parent.left = sh.Range("A1").left + prevWith 'here the first left chart position to be set
prevWith = ch.Chart.Parent.width 'the chart width, the next one will be added to its right side
Next i
ActiveWorkbook.Save
End Sub
Change your macro like this:
Sub Macro4(startRow As String, stud As Long)
For k = 1 To stud
Dim constr() As String
Dim cn As String
cn = ""
constr = Split(strtRow,":")
For Each c in constr
cn = cn & "$" & c & ":"
Next
cn = Left(cn, Len(cn) - 1)
Rows(strtRow).Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Range("Sheet1!" & cn)
Dim str As String
str = ""
For each d in constr
str = str & (CInt(d)+1) & ":"
Next
str = Left(str, Len(str) - 1)
strtRow = str
Next
Now call like
Macro4("2:2",3)
Will do on 2:2 to 4:4
I have a macro which imports weekly data into a new column and then runs a number of operations on it. I am having great difficulty with trying to sum the past ten weeks' data. Obviously formulae do not work since every week when I insert a new column, the formulae would not move to include the new column and drop the eleventh column.
The code I wrote to take this is:
Dim h As Range
Dim preCol As Long
With wsBOS.Rows(7)
Set h = .Find("Total", LookIn:=xlValues)
If Not h Is Nothing Then
preCol = h.Column - 1
End If
End With
For jCombo = 1 To 175
Dim siteCombo As String
siteCombo = ThisWorkbook.Sheets("Results Sheet").Cells(jCombo, 3)
If ((siteCombo = "Bone & Connective Tissue") Or (siteCombo = "Brain/CNS") Or (siteCombo = "Breast") Or (siteCombo = "GI") Or (siteCombo = "Gland/Lymphatic") Or (siteCombo = "GYN") _
Or (siteCombo = "Head & Neck") Or (siteCombo = "Leukemia Lymphoma") Or (siteCombo = "Lung") Or (siteCombo = "Gu") Or (siteCombo = "GU") Or (siteCombo = "Male") _
Or (siteCombo = "Metastasis Genital Organ") Or (siteCombo = "Other") Or (siteCombo = "Skin")) Then
ThisWorkbook.Sheets("Results Sheet").Cells(jCombo, preCol - 2).Value = Application.Sum(Range(Cells(jCombo, preCol - 11), (Cells(jCombo, preCol - 3))))
End If
Next jCombo
where jCombo increments rows downwards and preCol refers to the newly created column.
For whatever reason, this snippet is simply doing nothing when run. It does not throw any errors, it just leaves all 175 rows of preCol untouched. I am stumped and am turning to you for help.
If anyone has any ideas and wants to share them, I will be beyond appreciative. Thank you!!
preCol refers to Column OL.
I am trying to get the value to populate in column OJ.
Put this in Row 8 and copy down:
=SUM(INDEX(8:8,COLUMN()-1):INDEX(8:8,COLUMN()-10))
Since there are no column references it will always look at the last 10 columns in row 8. The 8:8 will change to the next row as it is dragged down.
This seems to be working for me
Option Explicit
Sub Dynamic_Duo()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim LC As Long, LR As Long, SumRange As String
LC = ws.Cells(6, ws.Columns.Count).End(xlToLeft).Offset.Column
LR = ws.Range("B" & ws.Rows.Count).End(xlUp).Offset(-2).Row
ws.Cells(6, LC + 1) = "10 Week"
ws.Cells(7, LC + 1) = "Total"
SumRange = ws.Range(ws.Cells(8, LC - 9), ws.Cells(8, LC)).Address(False, False)
ws.Range(ws.Cells(8, LC + 1), ws.Cells(LR, LC + 1)).Formula = "=Sum(" & SumRange & ")"
MsgBox "#Scott Craner's solution is better", vbCritical
End Sub
Can anyone help me?
Actually i am looking for vba script in excel:
i want to change cell location from $c$3 to $d$3 in each iteration
i am trying to use chr method but not working.
Dashboard is my sheet name
ActiveChart.SeriesCollection(2).Name = "=DashBoard!$c$8" 'changes,in next iteration it should change to $d$8 and so on till j
ActiveChart.SeriesCollection(2).Values = "=DashBoard!$c$9:$c$22" 'changes,in next iteration it should change to $d$9:$d$22 and so on till j
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim t As Integer
Dim associatedChar
t = 67
For i = 1 To 2
For j = 1 To 8
For k = 1 To 8
If (Check(i, j, k) = True) Then
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartArea.Select
ActiveChart.SeriesCollection(1).Name = "=DashBoard!$C$8"
ActiveChart.SeriesCollection(1).Values = "=DashBoard!$C$9:$C$22"
associatedChar = Chr(67)
'this is i am trying to use my associate value is intially $c$8 in next
'iterattion i want it to change to $d$8 then to $e$8 and so on.`enter code here`
ActiveChart.SeriesCollection(2).Name = "=DashBoard!$associatedChar$8" 'changes
ActiveChart.SeriesCollection(2).Values = "=DashBoard!$associatedChar$9:$associatedChar$22" 'changes
ActiveChart.SeriesCollection(1).XValues = "=DashBoard!$B$9:$B$22"
ActiveChart.SeriesCollection(2).XValues = "=DashBoard!$B$9:$B$22"
End If
Next k
Next j
Next i
End Sub
Does replacing associatedChar with " & associatedChar& " within the code help at all?
You would want to use .Cells references instead of .Range. Cells(row, col) can use numbers instead of a string reference like Range.
For example:
Dim LastRow As Long
LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).row
For row = 2 to LastRow
Sheets("Sheet1").Cells(row, 2) = whatever.
Next row
I am new to VBA and to this forum. I have a table with dates as the first column (x column) and 12 columns of data pertaining to the data (y values). I am trying to plot the data in a simple xlLine chart. Only few selected columns are to be plotted for y values. The columns are selected using a combo box at the top of the column. The number of rows are variable.
I am using this code but this is not working. Can someone kindly let me know what is wrong and fix it? Appreciate any help. Thanks in advance.
Sub drawchart1()
'
' drawchart1 Macro
'
'
Dim i As Integer
Dim j As Integer
Dim n As Integer
' finding the number of rows
j = Range("Charts!A1").Offset(Sheet1.Rows.Count - 1, 0).End(xlUp).Row
' selecting some range and adding a chart which is then modified.(not sure this is the correct method.)
Range("A10:C15").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
i = 2
n = 2
' Cells (9,1) contains the value "Date". Defining the X Axis values
ActiveChart.SeriesCollection(1).Name = Sheets("Charts").Cells(9, 1).Value
ActiveChart.SeriesCollection(1).XValues = "=Charts!R10C1:R" & j & "C1"
Do While i < 14
' Cells(8,i) contain the results of combo box - true or false.
' Cells(9,i) contain the names of the series
If Cells(8, i).Value = True Then
ActiveChart.SeriesCollection(n).Name = Sheets("Charts").Cells(9, i).Value
ActiveChart.SeriesCollection(n).Values = "=Charts!R10C" & i & ":R" & j & "C" & i
n = n + 1
i = i + 1
Else
i = i + 1
End If
Loop
End Sub
Hi Again,
Since my columns would not exceed 14 (i.e. not large), I used the following "brute force" technique and it worked fine. I would still love to learn how to do it without using the "brute force" technique. Thanks in advance.
Sub drawchart()
Dim j As Integer
Dim Chartstring As String
j = Range("Charts!A1").Offset(Sheet1.Rows.Count - 1, 0).End(xlUp).Row
Chartstring = "A9:A" & j
If Cells(8, 2).Value = True Then
Chartstring = Chartstring & ", B9:B" & j
Else
Chartstring = Chartstring
End If
If Cells(8, 3).Value = True Then
Chartstring = Chartstring & ", C9:C" & j
Else
Chartstring = Chartstring
End If
' And similarly added code for each of the 14 columns
' And finally fed the chartstring into the "Source"
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Range(Chartstring)
End Sub
Probably you're not watching any more. Here's an alternative approach.
Sub DrawChart1()
Dim i As Long
Dim j As Long
Dim ws As Worksheet
Dim rCht As Range, rYVals As Range
Dim cht As Chart
' finding the number of rows
Set ws = Worksheets("Charts")
j = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
' start with X values (row 10 to j), include header row (row 9)
Set rCht = ws.Range(ws.Cells(9, 1), ws.Cells(j, 1))
' add column of Y values if row 8 of column is TRUE
For i = 2 To 14
If ws.Cells(8, i).Value Then
Set rYVals = ws.Range(ws.Cells(9, i), ws.Cells(j, i))
Set rCht = Union(rCht, rYVals)
End If
Next
' if we've had any Y values, insert chart, using range we've built up
If Not rYVals Is Nothing Then
Set cht = ws.Shapes.AddChart(xlLine).Chart
cht.SetSourceData Source:=rCht, PlotBy:=xlColumns
End If
End Sub