+ Reply to Thread
Results 1 to 6 of 6

Location method with ActiveSheet

Hybrid View

  1. #1
    Registered User
    Join Date
    10-18-2009
    Location
    Coventry
    MS-Off Ver
    Excel 2003
    Posts
    9

    Location method with ActiveSheet

    Hello everyone,
    I am struggling with a small problem, where I believe that I don't exactly understand the activeSheet property.

    I have created a stacked columns chart in vba which works perfectly. It takes data from the sheet I am in and adds a chart below the table. I locate the newly created chart using this code:

    Set chtChart  = chtChart.Location (Where:=xlLocationAsObject, Name:="Waterfall")
    The problem with this is that it refers to a named sheet. If I want to change the name of the sheet, I would have to change the code. I just want the code to refer to the name of the active sheet I am in.

    Having failed to use ActiveSheet.Name for the parameter Name:=, I do not know where to go from here. Any help would be appreciated. Thank you!

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Location method with ActiveSheet

    You may be having trouble keeping track of which sheet is actually active. If you are activating various sheets, then the active sheet changes. I suggest that you set a worksheet variable to the activesheet early in the procedure and use that variable to provide the name.
    For example, the code below will move the chart to Sheet1 if it is active when the procedure is run and will move it to Sheet2 is it is active when the procedure is run.
    Sub doit()
    Dim a As Chart
    Dim b As Worksheet
    Set b = ActiveSheet
    Set a = Charts.Add
    Set a = a.Location(xlLocationAsObject, b.Name)
    End Sub
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  3. #3
    Registered User
    Join Date
    10-18-2009
    Location
    Coventry
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Location method with ActiveSheet

    Quote Originally Posted by blane245 View Post
    You may be having trouble keeping track of which sheet is actually active. If you are activating various sheets, then the active sheet changes. I suggest that you set a worksheet variable to the activesheet early in the procedure and use that variable to provide the name.
    For example, the code below will move the chart to Sheet1 if it is active when the procedure is run and will move it to Sheet2 is it is active when the procedure is run.
    Sub doit()
    Dim a As Chart
    Dim b As Worksheet
    Set b = ActiveSheet
    Set a = Charts.Add
    Set a = a.Location(xlLocationAsObject, b.Name)
    End Sub
    Thank you for your help. Here is what I did to solve the problem:
    - I set a new variable as my ActiveSheet as shown above and I was then able to use the code for the location of the file above.
    - I was using "Where:=" to name the location, but not "Name:=" for the name. This would not work:
    Set a = a.Location(Where:=xlLocationAsObject, b.Name)
    - I then just got rid of the definition of the parameter and it worked just fine:
    Set a = a.Location(xlLocationAsObject, b.Name)
    I did not try the codename solution, which could be useful in other cases, but my code used the ActiveSheet extensively, so I just went for the first suggestion. Thanks again!

  4. #4
    Forum Contributor FRIEL's Avatar
    Join Date
    03-07-2008
    Location
    Coventry
    MS-Off Ver
    2003 and 2007
    Posts
    266

    Re: Location method with ActiveSheet

    you could use the Code name

    activesheet.codename

    and you can change this in the properites of the sheet in VBA
    I dont like to use code i dont understand
    it makes it hard to use in other situations
    so please try to be as clear and patent as possible with me

    Criticism is welcomed

  5. #5
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Location method with ActiveSheet

    Great. Please mark the thread as solved.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Location method with ActiveSheet

    FYI, the codename version would not work, since it is the tab name that is required by the Location method.
    Everyone who confuses correlation and causation ends up dead.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1