+ Reply to Thread
Results 1 to 7 of 7

Too much .activate

  1. #1
    Registered User
    Join Date
    06-18-2013
    Location
    Copenhagen
    MS-Off Ver
    Excel 2010
    Posts
    15

    Too much .activate

    Hello

    My goal is to create a code that "exports" 8 different pivottables from 4 different worksheets to another workbook called rev*.
    The filename of the rev* workbook changes name each week but the first 3 characters are always the same.
    The code below is a partial code to do the first 2 tables from worksheet 1.
    When updating, the code needs to clear the old data because the pivot tables can vary in size each week.

    Please Login or Register  to view this content.
    My questions is this, is there a better way to do the select worksheet and paste? I think that my current code is quite messy. And why do I have to activate both the workbook and the worksheet like this?
    swb.Activate
    With Worksheets("1")
    .Activate
    .Range("B4").Select
    End With
    I tried to just do dwb.worksheets("1").activate but get an error.

    The Code works. Would appreciate some input though if there is something smarter than my current code as I didn't know what VBA was prior to january, so I'm still pretty new at this. Let me know if you have additional questions.
    Last edited by nteil; 06-18-2013 at 08:36 AM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Too much .activate

    Firstly, could you please edit your post to add code tags - makes it a lot easier to read/copy.

    Don't use Activate or Select.

    If you want to copy 'PivotTable2' from worksheet '1' in the workbook referenced by dwb and paste it to worksheet '1' in workbook swb at B4.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    06-18-2013
    Location
    Copenhagen
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Too much .activate

    Woop woop! Thanks Norie. Saved me 20 lines of code for each of the 4 sheets! Lovely!

    How would you do the clear old data without selection, sadly this doesn't work ?
    Please Login or Register  to view this content.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Too much .activate

    Not sure how copy and paste would clear cells.

    If you wanted to clear cells you would use Clear (or ClearContents), or even just set them all = "".

  5. #5
    Registered User
    Join Date
    06-18-2013
    Location
    Copenhagen
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Too much .activate

    The pivottables vary in size each week, so I have to clear the area before pasting the new tables in.
    Instead of using a macro to delete all sorts of borders and colors etc. such as the one deleteformat code on bottom. I just copy A1 (empty cell) and paste it to the area, to overwrite all borders, colors etc.

    But how come
    Please Login or Register  to view this content.
    -Doesn't work, when
    Please Login or Register  to view this content.
    works?

    Please Login or Register  to view this content.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Too much .activate

    Paste and PasteSpecial aren't the same.

    If you want to clear all formats from a range use ClearFormats instead of resetting individual format properties like Interior.

  7. #7
    Registered User
    Join Date
    06-18-2013
    Location
    Copenhagen
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Too much .activate

    Thanks so much for your input Norie! This is the revised code, must say it looks so much smoother :D

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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