Hi,
I am trying to show a larger version of a graph and some supporting cells when the user hovers the mouse over a sparkline chart. The idea is to copy an image of the target cells (which includes the graph and supporting cells) and load the image into a user form.
When I call the code from a another macro, everything works well. However if I trigger it from a function the macro returns a blank screenshot because it can't select the cells to take the picture. I modified the function so it only updates a cell (CY57) in the spreadsheet and then use the spreadsheet_change event to kick off the macro. In this case the macro runs without an error, but it still does not appropriately select the cells to capture the image and therefore returns a blank image. If I manually change the target cell (CY57) in the spreadsheet then the spreadsheet_change event runs appropriately takes the image.
Here are some more specifics:
I am using the following formula in the cell with the sparkline to call the function:
=IFERROR(HYPERLINK(Row_Location(ROW()),""),"")
Which successfully triggers and runs this function code to update the value in cell CY57:
Public Function Row_Location(Data_Row As Integer)
If ActiveSheet.Range("CY57") <> Data_Row Then ActiveSheet.Range("CY57") = Data_Row
End Function
Then I use a worksheet_change event to kick off the ExportRange macro. This all works appropriately
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("CY57"), Range(Target.Address)) _
Is Nothing Then
ExportRange
End If
End Sub
The problem occurs in the ExportRange macro. I included the code below, but I am not sure you need the whole block. Without getting into all the tests, I have narrowed down the issue to the fact that it won't allow me to select or active any cells when triggered by a result of a function or directly from the function. Because I can't do that, it exports a blank image instead of the intended cells.
Sub ExportRange() '(rng As Range)
Dim cob, sc
Application.ScreenUpdating = True
Sheets("blank").Range("CX64:Dk85").Select
Set rng = Selection
rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Set cob = rng.Parent.ChartObjects.Add(10, 10, 200, 200)
'remove any series which may have been auto-added...
Set sc = cob.Chart.SeriesCollection
Do While sc.Count > 0
sc(1).Delete
Loop
With cob
.ShapeRange.Line.Visible = msoFalse '<<< remove chart border
.Height = rng.Height
.Width = rng.Width
.Chart.Paste
.Chart.Export Filename:=ThisWorkbook.Path & "\temp_image.jpg", Filtername:="jpg"
.Delete
End With
UserForm1.Show
End Sub
If I manually type a number into cell CY57 then everything runs fine. If I call ExportRange from another macro it runs fine.
Sub Tester()
ExportRange ' ActiveSheet.Range("CX64:Dk85")
End Sub
It is only when the Worksheet_change event is triggered by a result of the function that it fail.
Does anyone have any ideas how to work around this?
Thanks for the help,
Mike
I am trying to show a larger version of a graph and some supporting cells when the user hovers the mouse over a sparkline chart. The idea is to copy an image of the target cells (which includes the graph and supporting cells) and load the image into a user form.
When I call the code from a another macro, everything works well. However if I trigger it from a function the macro returns a blank screenshot because it can't select the cells to take the picture. I modified the function so it only updates a cell (CY57) in the spreadsheet and then use the spreadsheet_change event to kick off the macro. In this case the macro runs without an error, but it still does not appropriately select the cells to capture the image and therefore returns a blank image. If I manually change the target cell (CY57) in the spreadsheet then the spreadsheet_change event runs appropriately takes the image.
Here are some more specifics:
I am using the following formula in the cell with the sparkline to call the function:
=IFERROR(HYPERLINK(Row_Location(ROW()),""),"")
Which successfully triggers and runs this function code to update the value in cell CY57:
Public Function Row_Location(Data_Row As Integer)
If ActiveSheet.Range("CY57") <> Data_Row Then ActiveSheet.Range("CY57") = Data_Row
End Function
Then I use a worksheet_change event to kick off the ExportRange macro. This all works appropriately
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("CY57"), Range(Target.Address)) _
Is Nothing Then
ExportRange
End If
End Sub
The problem occurs in the ExportRange macro. I included the code below, but I am not sure you need the whole block. Without getting into all the tests, I have narrowed down the issue to the fact that it won't allow me to select or active any cells when triggered by a result of a function or directly from the function. Because I can't do that, it exports a blank image instead of the intended cells.
Sub ExportRange() '(rng As Range)
Dim cob, sc
Application.ScreenUpdating = True
Sheets("blank").Range("CX64:Dk85").Select
Set rng = Selection
rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Set cob = rng.Parent.ChartObjects.Add(10, 10, 200, 200)
'remove any series which may have been auto-added...
Set sc = cob.Chart.SeriesCollection
Do While sc.Count > 0
sc(1).Delete
Loop
With cob
.ShapeRange.Line.Visible = msoFalse '<<< remove chart border
.Height = rng.Height
.Width = rng.Width
.Chart.Paste
.Chart.Export Filename:=ThisWorkbook.Path & "\temp_image.jpg", Filtername:="jpg"
.Delete
End With
UserForm1.Show
End Sub
If I manually type a number into cell CY57 then everything runs fine. If I call ExportRange from another macro it runs fine.
Sub Tester()
ExportRange ' ActiveSheet.Range("CX64:Dk85")
End Sub
It is only when the Worksheet_change event is triggered by a result of the function that it fail.
Does anyone have any ideas how to work around this?
Thanks for the help,
Mike