Solved

Improving ‘Worksheet_Activate’ macro

Posted on 2011-09-19
2
770 Views
Last Modified: 2012-05-12
Improving ‘Worksheet_Activate’ macro

The following macro is used to merge multiple worksheets in the same ‘Master’ sheet.

Current process:
-      A ‘Master’ worksheet is created manually in an existing ‘Data’ workbook with multiple worksheets.
-      Macro ‘Worksheet_Activate’ is run with ‘Master’ worksheet active.
-      The result: the content of all worksheets is gathered in ‘Master’ worksheet.
-      Each month, some or all worksheets of the ‘Data’ workbook are updated individually.
-      Again, Macro ‘Worksheet_Activate’ is run with ‘Master’ worksheet active.

Result: the content of all worksheets is gathered in ‘Master’ worksheet. Former content is automatically replaced  in ‘Master’ worksheet.

 I would like to be able to:
-      Choose which worksheets need to be copied.
The choice  could be set directly in the code or via some buttons to be clicked not in the different worksheets of the workbook.

(e.g.: worksheet1 ; Worksheet2; Worksheet6  selected but not Worksheet4 and Worksheet7)

-      Keep original cells format.
Currently the formatting of the cells is lost in the ‘Master’ worksheet after the macro is run:; Date, percentage, etc.

------------------------------------------------------------------------------------------------------
  Macro
------------------------------------------------------------------------------------------------------
Sub Worksheet_Activate()

  Dim ws As Worksheet
  Dim r As Long
 
  'Turn screen updates off to make things look nicer
  Application.ScreenUpdating = False
 
  'Delete everything in Master sheet, from row 2 downwards
  ActiveSheet.UsedRange.Offset(1).Clear
 
  'Loop through all worksheets
  For Each ws In ActiveWorkbook.Worksheets
    'Do this for all sheets except the Master sheet
    If ws.Name <> ActiveSheet.Name Then
      'Copy from 2nd row and downwards
      ws.UsedRange.Offset(1, 0).Copy
      'Paste values to Master sheet, below last used row
      Cells(ActiveSheet.UsedRange.Rows.Count + 1, 1).PasteSpecial xlPasteValues
    End If
  Next ws
 
  'Remove completely empty rows
  For r = ActiveSheet.UsedRange.Rows.Count To 2 Step -1
    If Application.WorksheetFunction.CountA(Rows(r)) = 0 Then
      Rows(r).Delete
    End If
  Next r
 
  'Select cell A1
  Range("A1").Select
 
  'Turn after-copy-blinking off
  Application.CutCopyMode = False
 
  'Turn screen updates back on
  Application.ScreenUpdating = True
End Sub

------------------------------------------------------------------------------------------------------

Thanks in advance for the time you take to review this question.

Bye
0
Comment
Question by:fredericgilbert
  • 2
2 Comments
 
LVL 2

Accepted Solution

by:
Hernan_Cano earned 500 total points
ID: 36563020
A simple way to define which sheets are to be copied is:

 
(...)
If ws.Name <> ActiveSheet.Name _
and ws.Name = "[WorkSheet Name]" _
and ws.Name = "Sheet 2" _
and ws.Name = "Some other WS name" Then
(...)

Open in new window

0
 
LVL 2

Expert Comment

by:Hernan_Cano
ID: 36563027
Sorry, that should be "or" instead of "and". A worksheet can't have ALL values. It must be "one of those" names.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel VBA Populate Column with value from Date Picker 16 35
Excel vba question 7 39
Excel VBA Multiple Word Column Headings with Data Validation 2 22
Determine dependencies 3 21
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

762 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