Solved

Improving ‘Worksheet_Activate’ macro

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

810 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