Thanks, I got this working now.
As I wanted the range that provides the chart data in the top right corner
of the screen I made a Sub that does that.
Maybe somebody has a better or shorter code to do the same:
Sub TopRightAlignRange(rngTopRight As Range)
Dim bError As Boolean
Dim lRangeRightCol As Long
Dim lVisibleRangeRightCol As Long
Dim bAdjustWidth As Boolean
Application.ScreenUpdating = False
'top align top row of range
'--------------------------
ActiveWindow.ScrollRow = rngTopRight.Cells(1).Row
lRangeRightCol = rngTopRight.Cells(rngTopRight.Cells(1).Row, _
rngTopRight.Columns.count).Column
lVisibleRangeRightCol = _
ActiveWindow.VisibleRange.Cells(ActiveWindow.VisibleRange.Cells(1).Row,
_
ActiveWindow.VisibleRange.Columns.count).Column
If lRangeRightCol =
ActiveWindow.VisibleRange.Cells(ActiveWindow.VisibleRange.Cells(1).Row, _
ActiveWindow.VisibleRange.Columns.count).Column
Then
Exit Sub
End If
If lRangeRightCol < lVisibleRangeRightCol Then
'first try left scroll to align right side of range to right screen
edge
'-----------------------------------------------------------------------
Do Until lRangeRightCol = _
ActiveWindow.VisibleRange.Cells(ActiveWindow.VisibleRange.Cells(1).Row,
_
ActiveWindow.VisibleRange.Columns.count).Column
Or _
lRangeRightCol > _
ActiveWindow.VisibleRange.Cells(ActiveWindow.VisibleRange.Cells(1).Row,
_
ActiveWindow.VisibleRange.Columns.count).Column
Or _
ActiveWindow.ScrollColumn
= 1
ActiveWindow.SmallScroll ToLeft:=1
If Err.Number <> 0 Then
bError = True
Exit Do
End If
If lRangeRightCol > _
ActiveWindow.VisibleRange.Cells(ActiveWindow.VisibleRange.Cells(1).Row,
_
ActiveWindow.VisibleRange.Columns.count).Column
Then
ActiveWindow.SmallScroll ToRight:=1
bAdjustWidth = True
Exit Do
End If
Loop
Else
'first try right scroll to align right side of range to right screen
edge
'-----------------------------------------------------------------------
Do Until lRangeRightCol = _
ActiveWindow.VisibleRange.Cells(ActiveWindow.VisibleRange.Cells(1).Row,
_
ActiveWindow.VisibleRange.Columns.count).Column
Or _
lRangeRightCol < _
ActiveWindow.VisibleRange.Cells(ActiveWindow.VisibleRange.Cells(1).Row,
_
ActiveWindow.VisibleRange.Columns.count).Column
ActiveWindow.SmallScroll ToRight:=1
If Err.Number <> 0 Then
bError = True
On Error GoTo 0
Exit Do
End If
If lRangeRightCol < _
ActiveWindow.VisibleRange.Cells(ActiveWindow.VisibleRange.Cells(1).Row,
_
ActiveWindow.VisibleRange.Columns.count).Column
Then
bAdjustWidth = True
Exit Do
End If
Loop
End If
If bError Or _
lRangeRightCol <> _
ActiveWindow.VisibleRange.Cells(ActiveWindow.VisibleRange.Cells(1).Row,
_
ActiveWindow.VisibleRange.Columns.count).Column
Then
If lRangeRightCol <
ActiveWindow.VisibleRange.Cells(ActiveWindow.VisibleRange.Cells(1).Row, _
ActiveWindow.VisibleRange.Columns.count).Column
Then
Do Until lRangeRightCol = _
ActiveWindow.VisibleRange.Cells(ActiveWindow.VisibleRange.Cells(1).Row,
_
ActiveWindow.VisibleRange.Columns.count).Column
Columns(1).ColumnWidth = Columns(1).ColumnWidth + 1
Loop
Else
Do Until lRangeRightCol = _
ActiveWindow.VisibleRange.Cells(ActiveWindow.VisibleRange.Cells(1).Row,
_
ActiveWindow.VisibleRange.Columns.count).Column
Columns(1).ColumnWidth = Columns(1).ColumnWidth + -1
Loop
End If
End If
Application.ScreenUpdating = True
End Sub
Sub MakeChartFromRange()
Dim strName As String
Dim oChart As Chart
Dim oChartObject As ChartObject
Dim oSheet As Worksheet
Dim lFirstRow As Long
Dim rngChartRange As Range
Dim rngTopLeft As Range
Dim rngBottomRight As Range
Dim lRowTop As Long
Dim lRowBottom As Long
Dim lColLeft As Long
Dim lColRight As Long
Set rngChartRange = Selection
Set oSheet = ActiveSheet
'put the range for the chart flush with the top right corner of the
visible range
'--------------------------------------------------------------------------------
TopRightAlignRange rngChartRange.Cells(rngChartRange.Columns.count)
lFirstRow = Selection.Cells(1).Row
lRowTop = Selection.Cells(1).Row
lRowBottom =
ActiveWindow.VisibleRange.Cells(ActiveWindow.VisibleRange.Cells.count).Row
lColLeft = ActiveWindow.ScrollColumn
lColRight = Selection.Cells(1).Column
Set rngTopLeft = Cells(lRowTop, lColLeft)
Set rngBottomRight = Cells(lRowBottom, lColRight)
'get patient's name for graph title
'----------------------------------
strName = Cells(lRowTop, 2) & " " & Cells(lRowTop, 3)
Application.ScreenUpdating = False
'clear the old charts
'---------------------
With ActiveSheet
For Each oChartObject In .ChartObjects
oChartObject.Delete
Next
End With
'build the chart
'---------------
Set oChart = oSheet.ChartObjects.Add(rngTopLeft.Left, _
rngTopLeft.Top, _
rngBottomRight.Left -
rngTopLeft.Left, _
rngBottomRight.Top -
rngTopLeft.Top).Chart
With oChart
.SetSourceData _
Source:=rngChartRange, _
PlotBy:=xlColumns
.ChartType = xlLine
.HasTitle = True
.ChartTitle.Characters.Text = strName
.HasLegend = False
.PlotArea.Left = 0
.PlotArea.Top = 0
.PlotArea.Width = .ChartArea.Width
.PlotArea.Height = .ChartArea.Height
.PlotArea.Interior.ColorIndex = 19
.ChartArea.Interior.ColorIndex = 34
.SeriesCollection(1).Border.ColorIndex = 3
.SeriesCollection(1).Border.Weight = xlMedium
End With
Application.ScreenUpdating = True
Exit Sub
ERROROUT:
MsgBox "There was an error making the chart" & _
vbCrLf & vbCrLf & _
Err.Description
On Error GoTo 0
End Sub
RBS