Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Improving ‘Worksheet_Activate’ macro

Posted on 2011-09-19
2
768 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
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…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

839 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