+ Reply to Thread
Results 1 to 7 of 7

Too much .activate

Hybrid View

  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.

    Dim dwb As Workbook
    Dim swb As Workbook
    Set dwb = ActiveWorkbook
    
        For Each wb In Application.Workbooks
            If wb.Name Like "Rev*" Then wb.Activate: Set swb = ActiveWorkbook: GoTo a:
        Next wb
    
        MsgBox "Failed to locate Rev* workbook": GoTo b:
    
    a:
    'CLEAR OLD DATA: A1 is an empty cell, so this overwrites the borders and old values, figured this was faster
        Worksheets("1").Activate
        Range("A1").Select
        Selection.Copy
        Range("B4:Q38").Select 'This is the maximum range of the 1st pivottable
        ActiveSheet.Paste
        Range("B48:Q82").Select 'This is the maximum range of the 2nd pivottable
        ActiveSheet.Paste
    'Copy the 1st pivottable and paste it to the rev* workbook
        dwb.Activate
        With Worksheets("1")
        .Activate
        .PivotTables("PivotTable2").TableRange2.Copy
        End With
        swb.Activate
        With Worksheets("1")
        .Activate
        .Range("B4").Select
        End With
        IP 'Paste function of the pivot table, look below
    'Copy the 2nd pivottable and paste it to the rev* workbook
        dwb.Activate
        With Worksheets("1")
        .Activate
        .PivotTables("PivotTable1").TableRange2.Copy
        End With
        swb.Activate
        With Worksheets("1")
        .Activate
        .Range("B48").Select
        End With
        IP
    b:
    End Sub
    
    IP()
    'Import Pivot function
            With Selection
            .PasteSpecial Paste:=xlPasteAll
            .PasteSpecial Paste:=xlValues
            End With
    End Function
    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,645

    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.
     dwb.Worksheets("1").PivotTables("PivotTable2").TableRange2.Copy 
    
     swb.Worksheets("1").Range("B4").PasteSpecial xlPasteAll
     swb.Worksheets("1").Range("B4").PasteSpecial xlPasteValues
    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 ?
        swb.Worksheets("1").Range("A1").Copy
        swb.Worksheets("1").Range("B4:Q38").Paste

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

    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
    swb.Worksheets("1").Range("B4:Q38").Paste
    -Doesn't work, when
    swb.Worksheets("1").Range("B4").PasteSpecial xlPasteAll
    works?

        
    Sub DeleteFormat()
    Application.ScreenUpdating = False
    'Delete content in cells
        Selection.ClearContents
    'Delete any border
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        Selection.Borders(xlEdgeLeft).LineStyle = xlNone
        Selection.Borders(xlEdgeTop).LineStyle = xlNone
        Selection.Borders(xlEdgeBottom).LineStyle = xlNone
        Selection.Borders(xlEdgeRight).LineStyle = xlNone
        Selection.Borders(xlInsideVertical).LineStyle = xlNone
        Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    'Delete any color inside cells
        With Selection.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    'Delete any fontcolor
        With Selection.Font
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
        End With
        Application.ScreenUpdating = True
    End Sub

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

    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

    'Clear old data
        With swb.Worksheets("1").Range("B3:R38")
            .ClearFormats
            .ClearContents
        End With
        With swb.Worksheets("1").Range("B40:R70")
            .ClearFormats
            .ClearContents
        End With
    
    'Insert new pivottables
        dwb.Worksheets("1").PivotTables("PivotTable2").TableRange2.Copy
        swb.Worksheets("1").Range("B3").PasteSpecial xlPasteAll
        swb.Worksheets("1").Range("B3").PasteSpecial xlPasteValues
        dwb.Worksheets("1").PivotTables("PivotTable1").TableRange2.Copy
        swb.Worksheets("1").Range("B40").PasteSpecial xlPasteAll
        swb.Worksheets("1").Range("B40").PasteSpecial xlPasteValues

+ 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