access visual basic - excel vba
Microsoft Office Training verified by visa - mastercard securecode about microsoft training company london ukadd this page to your favourites/bookmarksAdd to favourites
view a printable version of this pagePrintable version
email this page to somebodyEmail this page
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft VBA Training and help » access visual basic - Excel VBA

access visual basic - Excel VBA

The UK's most regular instructor-led training courses.
Training information: access visual basic · Excel london

resolvedResolved · Low Priority · Version Standard

Excel VBA

Anne has attended:
Excel Introduction course
Excel Intermediate course
Excel Advanced course
Excel VBA course
Excel Advanced course

by - delegate Anne [6 posts] (2007 Sep 28 Fri, 16:24) replyReply

Need to consolidate worksheets from a workbook onto a consolidated workbook.

Thanks.

Anne

RE: Excel VBA

by - trainer Roy [23 posts] (2007 Oct 1 Mon, 11:49) Edited on 2007 Oct 1 Mon, 11:58 replyReply

Anne,
Try the code below.
Note that I've assumed that the Consolidated workbook is empty of data when the import is done. If you want to handle the case where the import is done incrementally, then you'll need to make some modifications regarding worksheet names, and the row number on the Consolidated worksheet.

Files attached for download.

regards
/Roy MacLean
--------------------------------------------------------------
Option Explicit

Sub ImportData()
'Import data from multiple workbooks called "Centre n" for n = 1, 2, ...
'Each source workbook has a single worksheet called "Marks",
'which is copied to this workbook.

On Error GoTo handler

Dim source_wbname As String 'Centre n workbook
Dim destination_wbname As String 'This workbook
Dim wscount As Integer 'number of worksheets in this workbook
Dim i As Integer

destination_wbname = ActiveWorkbook.Name
wscount = Worksheets.Count

i = 1
Do While True 'Do 'forever'; Exit loop when error occurs

source_wbname = "Centre " & i & ".xls"
Workbooks.Open (source_wbname) 'Becomes active

ActiveWorkbook.Worksheets("Marks").Copy _
after:=Workbooks(destination_wbname).Worksheets(wscount)

Workbooks(source_wbname).Close 'This workbook becomes active

ActiveWorkbook.Worksheets("Marks").Name = "Centre " & i

wscount = wscount + 1
i = i + 1
Loop

Exit Sub

handler:
Dim errnum As Integer 'Error number - identifies the type of error

errnum = Err.Number
If errnum = 1004 Then ''No Object' error
MsgBox "Import Completed"
Else 'Any other error
'Output the associated error message
MsgBox "Error: " & errnum & vbCrLf & Error(Err.Number)
End If

End Sub

Sub Consolidate()
'For each "Centre n" worksheet, copy the Totals range to the "Consolidated" worksheet,
'putting the Centre name in column A.

Dim wscount As Integer 'Number of worksheets in this workbook
Dim i As Integer
Dim wsname As String 'Name of the current "Centre n" worksheet

wscount = Worksheets.Count - 2 'minus 2 for the sheets "Front" and "Consolidated"

For i = 1 To wscount
wsname = "Centre " & i

Worksheets("Consolidated").Range("A" & i).Value = wsname

Worksheets("Consolidated").Range("B" & i & ":C" & i).Value = _
Worksheets(wsname).Range("Total").Value
Next


End Sub

Attached files...

Centre 1.xls
Centre 2.xls
Consolidated_initial.xls


Related articles

· Company Roles in Visual Basic Training
· What Is Microsoft Excel VBA and How Can It Help You Work Smarter?

Vba tip:

Empty The Clipboard with CutCopyMode

After each Copy/Paste operation in VBA, you should use the following line of code to empty the clipboard. This ensures that the computer memory doesn't overload:

ActiveSheet.Paste
Application.CutCopyMode = False

View all VBA hints and tips

Institute of IT Training - Accredited Training Provider Microsoft Certified Partner
microsoft office
Microsoft Office Specialist Authorised Testing Centre (MOS and MCAS)

Prodigy Platinum Learning Partner

Institute of IT Training - Accredited Training Provider Association of Computer Trainers Valid HTML 4.01 Transitional
Valid CSS Markup

secure online payments - visa - mastercard

Mini sitemap. These are the main areas of our web site. Full sitemap.

Training by application Main information pages See also

Access courses
DreamWeaver courses
Excel courses
MS Project courses
Outlook courses
PowerPoint courses
VBA courses
Word courses
(more...)

Public scheduled courses
On-site training
Closed company courses

Microsoft Office training
Pricing and availability
Training schedule
Training venues

Access training
Dreamweaver training
Excel training
MS Project training
PowerPoint training

London Computer Training
Computer Training London

Microsoft Access training
Microsoft Excel training
Microsoft Project training
Microsoft Outlook training
Microsoft Powerpoint training
Microsoft Word training

Time Management Course London

Interested in Access training? Please see the following pages:
microsoft access courses · microsoft training access course
microsoft+access+training · access courses in london

Training Information
AddThis Social Bookmark Button What's this?
Add to Del.icio.us Add to Facebook Add to Digg Add to Reddit Add to Google Add to Yahoo Add to Diigo Add to Mr. Wong Add to Linkarena Add to Power Oldie Add to Folkd Add to Jumptags Add to Upchuckr Add to Simpy Add to StumbleUpon Add to Slashdot Add to Netscape Add to Furl Add to Spurl Add to Blinklist Add to Blogmarks Add to Technorati Add to Newsvine Add to Blinkbits Add to Ma.Gnolia Add to Smarking Add to Netvouz