<<...myformula1 and myformula2 correspond to column A of sheet1...>>
Where are "myformula1" and "myformula2" in your code?
Also, what to you mean by "to correspond to" in your post?
I made a few slight revisions in your code (mostly to use With statements).
The statement to set Range("F6") to a Sum was missing a period in front of
Range. You still need to take "Sheet3" out somehow, but I don't have your
data, so that I can run the macro to see what it does exactly.
I notice that you delete all of the embedded charts, then basically rebuild
them (or at least rebuild 1 of them). I wonder if that's really necessary.
'----------------------------------------------------------------------
Public Sub command2()
Dim wsActive As Worksheet
Dim shp As Shape
Dim i As Double
Dim j As Double
Set wsActive = ActiveSheet
For Each shp In wsActive.Shapes
If shp.Type = msoChart Then
shp.Delete
End If
Next shp
Charts.Add
With ActiveChart
.ChartType = xlColumnClustered
.SetSourceData Source:=wsActive.Range("H4"), _
PlotBy:=xlColumns
.SeriesCollection.NewSeries
End With 'ActiveChart
With wsActive
i = .Range("F4").Value
j = i + 3
ActiveChart.SeriesCollection(1).XValues = _
.Range(.Cells(2, 1), .Cells(j, 1))
ActiveChart.SeriesCollection(1).Values = _
.Range(.Cells(2, 2), .Cells(j, 2))
.Range("F6").Value = Application.WorksheetFunction _
.Sum(.Range(.Cells(2, 2), .Cells(j, 2)))
End With 'wsActive
With ActiveChart
.SeriesCollection(1).Name = wsActive.Range("A1").Value
.Legend.Delete
.Location Where:=xlLocationAsObject, Name:="Sheet3"
End With 'ActiveChart
'must be made generic later
For Each shp In wsActive.Shapes
If shp.Type = msoChart _
Then
With shp
.IncrementLeft -47.25
.IncrementTop -1.5
.ScaleWidth 1.6, msoFalse, msoScaleFromTopLeft
.ScaleHeight 1.46, msoFalse, msoScaleFromTopLeft
ActiveChart.Axes(xlCategory).TickLabels.NumberFormat = "0.00"
End With 'shp
End If
Next shp
End Sub
Bill, Thank you for that revision. I gave you the code for subroutine
command2 in my previous reply. And you are right, it does not have
myformula1 and myformula2. Given below is the code for command1 which
has myformula1 and myformula2. Basically, this code is very similar to
the one in command2. It has just few additional lines. I have
incorporated you revision in this code.
To answer your "correspond to" question, I mean that if the sheet name
is sheet3 then
myformula1 = "=min(Sheet1!A:A)"
myformula2 = "=max(Sheet1!A:A)"
If the sheet name is sheet4 then
myformula1 = "=min(Sheet1!B:B)"
myformula2 = "=max(Sheet1!B:B)"
If the sheet name is sheet5 then
myformula1 = "=min(Sheet1!C:C)"
myformula2 = "=max(Sheet1!C:C)"
etc... How to make this generic as you made the rest of the code
generic using wsactive?
Also, is it possible to replace
ActiveChart.Location Where:=xlLocationAsObject, name:="Sheet3"
with something like
ActiveChart.Location Where:=xlLocationAsObject, wsactive
I hope I have answered your questions.
***************************************
Public Sub command1()
Dim myformula1 As String
Dim myformula2 As String
Dim myformula3 As String
Dim wsActive As Worksheet
Set wsActive = ActiveSheet
myformula1 = "=min(Sheet1!A:A)"
myformula2 = "=max(Sheet1!A:A)"
Range("F2").Value = myformula1
Range("F3").Value = myformula2
Range("F4").Value = 20
Dim shp As Shape
For Each shp In wsActive.Shapes
If shp.Type = msoChart Then
shp.Delete
End If
Next shp
Dim i As Double
Dim j As Double
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=wsActive.Range("H4"), PlotBy:= _
xlColumns
ActiveChart.SeriesCollection.NewSeries
With wsActive
i = .Range("F4").Value
j = i + 3
ActiveChart.SeriesCollection(1).XValues = _
.Range(.Cells(2, 1), .Cells(j, 1))
ActiveChart.SeriesCollection(1).Values = _
.Range(.Cells(2, 2), .Cells(j, 2))
.Range("F6").Value = Application.WorksheetFunction _
.Sum(.Range(.Cells(2, 2), .Cells(j, 2)))
End With 'wsActive
ActiveChart.SeriesCollection(1).name = wsActive.Range("A1").Value
ActiveChart.Legend.Select
Selection.Delete
ActiveChart.Location Where:=xlLocationAsObject, name:="Sheet3"
'must be made generic later
'Dim shp As Shape
For Each shp In wsActive.Shapes
If shp.Type = msoChart Then
shp.IncrementLeft -47.25
shp.IncrementTop -1.5
shp.ScaleWidth 1.6, msoFalse, msoScaleFromTopLeft
shp.ScaleHeight 1.46, msoFalse, msoScaleFromTopLeft
ActiveChart.Axes(xlCategory).Select
Selection.TickLabels.NumberFormat = "0.00"
End If
Next shp
End Sub