Returning focus to last used cell in VBA function

wasabi

New Member
Joined
Apr 12, 2010
Messages
28
I've got a function as follows:
Code:
Function VerificarInfinito(a As Double)
Set c = ActiveCell
ActiveSheet.ChartObjects("Planta").Activate
If (a >= 3) Then
    ActiveChart.SeriesCollection(2).Format.Line.Transparency = 0
    ActiveChart.SeriesCollection(13).Format.Line.Transparency = 0
    ActiveChart.SeriesCollection(14).Format.Line.Transparency = 0
    ActiveChart.SeriesCollection(1).Format.Line.Transparency = 0
    ActiveChart.SeriesCollection(4).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(16).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(3).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(15).Format.Line.Transparency = 1
ElseIf a < 1 / 3 Then
    ActiveChart.SeriesCollection(2).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(13).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(14).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(1).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(4).Format.Line.Transparency = 0
    ActiveChart.SeriesCollection(16).Format.Line.Transparency = 0
    ActiveChart.SeriesCollection(3).Format.Line.Transparency = 0
    ActiveChart.SeriesCollection(15).Format.Line.Transparency = 0
Else
    ActiveChart.SeriesCollection(2).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(13).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(14).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(1).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(4).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(16).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(3).Format.Line.Transparency = 1
    ActiveChart.SeriesCollection(15).Format.Line.Transparency = 1
End If
c.Activate
VerificarInfinito = a
End Function

It does everything I need it to do (in this case, make certain lines in a graph visible or not depending on the input value). However, the function ends with the focus on the chart, not in the last used cell. I tried fixing this by saving the ActiveCell in the beginning and then Activating it again at the end of the function, however this has no effect.

I've used this solution (saving the ActiveCell) in other VBA subroutines (also altering the contents of a graph) and it works like a charm, so I don't entirely understand why it doesn't work here.

Code:
Sub Drop_Esq()
a = Range("S4").Value
Set c = ActiveCell
'... do stuff with the graph
c.Activate
End Sub

Is there a relevant difference due to this being a Function as opposed to a Sub(routine)?
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
You don't need to activate the chart to change it...

Code:
Sub VerificarInfinito(a As Double)

    With ActiveSheet.ChartObjects("Planta")
        If (a >= 3) Then
            .SeriesCollection(2).Format.Line.Transparency = 0
            .SeriesCollection(13).Format.Line.Transparency = 0
            .SeriesCollection(14).Format.Line.Transparency = 0
            .SeriesCollection(1).Format.Line.Transparency = 0
            .SeriesCollection(4).Format.Line.Transparency = 1
            .SeriesCollection(16).Format.Line.Transparency = 1
            .SeriesCollection(3).Format.Line.Transparency = 1
            .SeriesCollection(15).Format.Line.Transparency = 1
        ElseIf a < 1 / 3 Then
            .SeriesCollection(2).Format.Line.Transparency = 1
            .SeriesCollection(13).Format.Line.Transparency = 1
            .SeriesCollection(14).Format.Line.Transparency = 1
            .SeriesCollection(1).Format.Line.Transparency = 1
            .SeriesCollection(4).Format.Line.Transparency = 0
            .SeriesCollection(16).Format.Line.Transparency = 0
            .SeriesCollection(3).Format.Line.Transparency = 0
            .SeriesCollection(15).Format.Line.Transparency = 0
        Else
            .SeriesCollection(2).Format.Line.Transparency = 1
            .SeriesCollection(13).Format.Line.Transparency = 1
            .SeriesCollection(14).Format.Line.Transparency = 1
            .SeriesCollection(1).Format.Line.Transparency = 1
            .SeriesCollection(4).Format.Line.Transparency = 1
            .SeriesCollection(16).Format.Line.Transparency = 1
            .SeriesCollection(3).Format.Line.Transparency = 1
            .SeriesCollection(15).Format.Line.Transparency = 1
        End If
    End With
    
End Sub

Also, this doesn't need to be a Function as best I can tell. It doesn't return a different\new value. Call the above sub with something like...
VerificarInfinito 4
 
Last edited:
Upvote 0
I don't know why, but any time I've ever tried using With ChartObject, it's never worked. In this case, it fails on the very first .SeriesCollection() call.

And I have it as a function because I need it to update whenever the input value is altered. The call to the function is actually "=VerificarInfinito(B1/B2)" so that it will update whenever either B1 or B2 are changed. I need that value to know how to alter the graph but I also want it displayed on the screen, which is why I simply have the function return the same value as in inputted.

As far as I know, there's no way to link a Sub to a cell. The other Subs I have (where my ActiveCell ... Activate code works) are linked to control buttons.
 
Last edited:
Upvote 0
Ah, thanks. The Worksheet_Change method worked like a charm. Much appreciated. However, With ChartObject still didn't work...

However, I simply changed it back to ActiveChart and it worked.
 
Last edited:
Upvote 0
Your welcome. Thanks for the feedback.

The With command should be this...

With ActiveSheet.ChartObjects("Planta").Chart
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top