I need my existing macro to put cursor focus on a certain worksheet tab

Hi,

I have a macro that I need to tweak in two ways.  it currently creates a new tab and adds a graph.  

1.  I simply need it to place the cursor focus on the worksheet tab titled 'compare plans' after it adds the new tab and graph.   See attached excel with macro...I've also copied the macro below.   (Note that right now the cursor focus ends up in the newly created worksheet instead of the tab titled 'compare plans'.)

2.  I need it to place inside the graph a subtitle = "cell F6" as found in the tab titled 'compare plans'

Here is the current macro:

Sub CopySheetToRecord()
    Dim ws As Worksheet, shName As String, i As Integer
   
    shName = WorksheetFunction.Text(Sheets("COMPARE PLANS").Range("F14"), "#,###")
    i = 0
    For Each ws In Worksheets
        If ws.Name = shName Then
            i = 1
        End If
    Next ws
    If i = 1 Then
        MsgBox "Sheet " + shName + " exists, copy not performed, program stop", vbCritical
        End
    Else
        Sheets("COMPARE PLANS").Copy After:=Sheets(Worksheets.Count)
        Cells.Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
        Range("A1").Select
        ActiveSheet.Name = shName
        MakeChart (shName)
    End If
End Sub

Sub MakeChart(ByVal shName As String)
    With Sheets(shName)
        .Shapes.AddChart(227, xlLine).Select
        ActiveChart.SetSourceData Source:=.Range("D22:H" & .Range("E1048576").End(xlUp).Row)
        SizeChart (.Shapes("Chart 1"))
    End With
End Sub

Sub SizeChart(ByVal chart As Shape)
    With chart
        .Top = 12
        .Left = 612
        .Height = 350
        .Width = 920
    End With
End Sub

Thanks in advance.
financials-with-graph.xlsm
Cam RabenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FamousMortimerCommented:
I think this will do what you are after.  Just replace the MakeChart sub with this:

Sub MakeChart(ByVal shName As String)
    With Sheets(shName)
        .Shapes.AddChart(227, xlLine).Select
        ActiveChart.SetSourceData Source:=.Range("D22:H" & .Range("E1048576").End(xlUp).Row)
        ActiveChart.HasTitle = True
        ActiveChart.ChartTitle.Caption = Sheets("COMPARE PLANS").Range("F6").Text
        SizeChart (.Shapes("Chart 1"))
        Sheets("COMPARE PLANS").Select
    End With
End Sub

Open in new window

0
Cam RabenAuthor Commented:
I get a runtime error "this object has no title", referring to the active.chart.chartTitle line below:

Sub MakeChart(ByVal shName As String)
    With Sheets(shName)
        .Shapes.AddChart(227, xlLine).Select
        ActiveChart.SetSourceData Source:=.Range("D22:H" & .Range("E1048576").End(xlUp).Row)
        ActiveChart.HasTitle = True
        ActiveChart.ChartTitle.Caption = Sheets("COMPARE PLANS").Range("F6").Text
        SizeChart (.Shapes("Chart 1"))
        Sheets("COMPARE PLANS").Select
    End With
End Sub
0
FamousMortimerCommented:
OK, that was very strange... It worked for me when I stepped through the code with F8 but running it, it would not create the title.  Apparently ActiveChart.HasTitle is for 2007 and below.  Try the code below.

Sub MakeChart(ByVal shName As String)
    With Sheets(shName)
        .Shapes.AddChart(227, xlLine).Select
        ActiveChart.SetSourceData Source:=.Range("D22:H" & .Range("E1048576").End(xlUp).Row)
        ActiveChart.SetElement (msoElementChartTitleAboveChart)
        ActiveChart.ChartTitle.Caption = Sheets("COMPARE PLANS").Range("F6").Text
        SizeChart (.Shapes("Chart 1"))
        Sheets("COMPARE PLANS").Select
    End With
End Sub

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Cam RabenAuthor Commented:
It works beautifully, Famous.  I'm increasing the points to 500 in case you might want to help me do one more tweak.  i realize now that I am using it that I really want to see the newest change when its made, and only THEN return the focus to the 'compare plans' tab....which would require a "button" on the newly created graph called 'return to compare charts'.  If I don't hear back I'll award the points anyway.  Thanks very much!
0
FamousMortimerCommented:
Great, glad it works.  I have to run but here is the entirel module code that adds a button that will return you to the Compare Plans sheet and I also disabled selecting the sheet after creation

Option Explicit


Sub CopySheetToRecord()
    Dim ws As Worksheet, shName As String, i As Integer
    
    shName = WorksheetFunction.Text(Sheets("COMPARE PLANS").Range("F14"), "#,###")
    i = 0
    For Each ws In Worksheets
        If ws.Name = shName Then
            i = 1
        End If
    Next ws
    If i = 1 Then
        MsgBox "Sheet " + shName + " exists, copy not performed, program stop", vbCritical
        End
    Else
        Sheets("COMPARE PLANS").Copy After:=Sheets(Worksheets.Count)
        Cells.Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
        Range("A1").Select
        ActiveSheet.Name = shName
        MakeChart (shName)
    End If
End Sub

Sub MakeChart(ByVal shName As String)
    With Sheets(shName)
        .Shapes.AddChart(227, xlLine).Select
        ActiveChart.SetSourceData Source:=.Range("D22:H" & .Range("E1048576").End(xlUp).Row)
        ActiveChart.SetElement (msoElementChartTitleAboveChart)
        ActiveChart.ChartTitle.Caption = Sheets("COMPARE PLANS").Range("F6").Text
        SizeChart (.Shapes("Chart 1"))
        
        With .Buttons.Add(25.5, 328.5, 96, 39.25)
            .OnAction = "ReturnToComparePlans"
            .Characters.Text = "Return To Compare Plans"
        End With
    End With
End Sub

Sub SizeChart(ByVal chart As Shape)
    With chart
        .Top = 12
        .Left = 612
        .Height = 350
        .Width = 920
    End With
End Sub

Public Sub ReturnToComparePlans()
    Sheets("COMPARE PLANS").Select
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cam RabenAuthor Commented:
Great - all good.  Thanks much, Famous.
0
FamousMortimerCommented:
Quite welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.