Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel Macro to Update Main Sheet

Posted on 2003-11-18
7
Medium Priority
?
1,137 Views
Last Modified: 2008-03-17
I have an Excel Workbook that has multiple worksheets that I use to track the status of projector bulbs that we use.  There is a separate worksheet for each bulb (For example 0120, 0736, 0754, etc.), and a main sheet called Bulb List that I need to update with the last record from each of the individual bulb worksheets.  I would like the macro to loop through each of the worksheets and update the main sheet with the latest info from the individual worksheets.

I have been written the following code to get started but would like it to loop through all the individual worksheets without me having to name them since the main bulb list already has over 50 bulbs and continues to grow.

Sub Build_Current_Status()
'
' Build_Current_Status Macro
' Macro recorded 11/6/2003 by tmoore
'

'
    Range("C2").Select
   
    Application.Goto Reference:=Worksheets("0120").Range("B1")
    Selection.End(xlDown).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Bulb_list").Select
    ActiveSheet.Paste
   
    Range("C3").Select
    Application.Goto Reference:=Worksheets("0736").Range("B1")
    Selection.End(xlDown).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Bulb_list").Select
    ActiveSheet.Paste
   
    Range("C4").Select
    Application.Goto Reference:=Worksheets("0754").Range("B1")
    Selection.End(xlDown).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Bulb_list").Select
    ActiveSheet.Paste
   
    Range("C5").Select
    Application.Goto Reference:=Worksheets("0792").Range("B1")
    Selection.End(xlDown).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Bulb_list").Select
    ActiveSheet.Paste
End Sub

Thanks for any help,

mbean
0
Comment
Question by:mbean
  • 3
  • 3
7 Comments
 
LVL 22

Expert Comment

by:Dreamboat
ID: 9774864
If it were me....

I would make all my worksheets have Row 1 contain the TOTALS [i.e., =sum(B3:B65536)] and Row 2 contain the titles.

Then, you could easily use formulas on your summary worksheet instead of using code.
0
 

Author Comment

by:mbean
ID: 9774980
Let me explain a little more:

The individual bulb worksheets (0120, 0736, 0744, etc.) have the following columns:

Date, From Location, To Location, Hours, Notes or Actions Taken

Each of the individual bulb worksheets (0120, 0736, 0744, etc.) over time will have several rows of data (records).  

My Summary Worksheet (Bulb List) has the same columns as the individual bulb worksheets and I need to pull in the last row of data from each individual bulb worksheet. I also need to be able to undate the Summary Worksheet on a daily basis so I have the most current information. I need to clear the data on the Summary Worksheet prior to updating it with the most current info from the individual bulb worksheets.

Thanks  
0
 
LVL 81

Expert Comment

by:byundt
ID: 9775103
Hi mbean,
The following macro pulls data from columns B to end of data from the last row on each worksheet. The results are pasted starting in row 2 of worksheet Bulb_list

Sub Build_Current_Status()
Dim ws As Worksheet, wsMaster As Worksheet
Dim rw As Range
Dim i As Long
Set wsMaster = Worksheets("Bulb_list")
i = 1
For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "Bulb_list" Then
        i = i + 1
        Set rw = ws.Cells(1, 2).End(xlDown)
        Set rw = Range(rw, rw.End(xlToRight))
        rw.Copy
        wsMaster.Cells(i, 1).PasteSpecial
    End If
Next ws
End Sub

Cheers!

Brad
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

Author Comment

by:mbean
ID: 9780852
Ok Brad...this works great now one more twist I have two worksheets that I do not want to include data from.  The following is a list of the worksheets in the workbook:

Bulb_List
Projectors
Returns
0120
0736
0744
etc.

I need to skip over Projectors & Returns but include all the other worksheets.

Thanks,

Mary
0
 
LVL 81

Accepted Solution

by:
byundt earned 2000 total points
ID: 9780953
Mary,
To skip over multiple worksheets, it is better to use a Select Case construction. The following sub ignores Bulb_list, Projectors and Returns:

Sub Build_Current_Status()
Dim ws As Worksheet, wsMaster As Worksheet
Dim rw As Range
Dim i As Long
Set wsMaster = Worksheets("Bulb_list")
i = 1
For Each ws In ActiveWorkbook.Worksheets
    Select Case ws.Name
    Case "Bulb_list", "Projectors","Returns"
    Case Else
        i = i + 1
        Set rw = ws.Cells(1, 2).End(xlDown)
        Set rw = Range(rw, rw.End(xlToRight))
        rw.Copy
        wsMaster.Cells(i, 1).PasteSpecial
    End Select
Next ws
End Sub

Brad
0
 

Author Comment

by:mbean
ID: 9781089
Brad,

Thanks...that did the trick.  I had to modify it slightly to only paste values so here it is with the modifications:

Sub BuildCurrentStatus()
Dim ws As Worksheet, wsMaster As Worksheet
Dim rw As Range
Dim i As Long
Set wsMaster = Worksheets("Bulb_list")
i = 1
For Each ws In ActiveWorkbook.Worksheets
    Select Case ws.Name
    Case "Bulb_list", "Projectors", "Returns"
    Case Else
        i = i + 1
        Set rw = ws.Cells(1, 1).End(xlDown)
        Set rw = Range(rw, rw.End(xlToRight))
        rw.Copy
        wsMaster.Cells(i, 1).PasteSpecial xlPasteValues
    End Select
Next ws
End Sub

Thanks Again!!!
0
 
LVL 81

Expert Comment

by:byundt
ID: 9781516
Mary,
Thanks for the grade!
Brad
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

927 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