Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2014-08-07
7
Medium Priority
?
469 Views
Last Modified: 2014-08-08
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
0
Comment
Question by:Cam Raben
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:FamousMortimer
ID: 40247157
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
 

Author Comment

by:Cam Raben
ID: 40247175
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
 
LVL 10

Expert Comment

by:FamousMortimer
ID: 40247270
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Cam Raben
ID: 40247302
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
 
LVL 10

Accepted Solution

by:
FamousMortimer earned 2000 total points
ID: 40247328
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
 

Author Closing Comment

by:Cam Raben
ID: 40247347
Great - all good.  Thanks much, Famous.
0
 
LVL 10

Expert Comment

by:FamousMortimer
ID: 40248490
Quite welcome.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question