access visual basic - excel vba
Microsoft Office TrainingThe UK's Number 1 for Microsoft Office Training Sitemap add this page to your favourites/bookmarksBookmark page
 
view a printable version of this pagePrintable version
Plus One Google
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

resolvedResolved · Low Priority · Version Standard

replyReplyFri 28 Sep 2007, 16:24Delegate Anne said...

Excel VBA

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

Thanks.

Anne

For upcoming training course dates see: Pricing & availability

replyReplyMon 1 Oct 2007, 11:49 Edited on Mon 1 Oct 2007, 11:58Trainer Roy said...

RE: Excel VBA

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

 

 

Vba tip:

Use GoTo to Select A Cell With VBA

To select a certain cell on a Worksheet you can use:

Application.Goto Reference:=Range("V300")

or more simply

Range("V300").Select

If, on the other hand, you want the selected cell to be the top/left cell on the screen you use:

Application.Goto Reference:=Range("V300"), Scroll=True

View all VBA hints and tips

article

Tips On Project Managing Workplace Problems

Even natural born problem solvers might need assistance when they come across issues for the first time. Take a look below to see how a standardized approach could benefit you and your colleagues.

» Article: Problem solving training

forum postHow to get to 'start to start' linking?

» Forum post: Linking

hint tipIf you go into print preview and click on the page you will zoom into the text. If you need to make changes and don't want to come out of print preview, then deselect the magnifer (sencond button in -next to the print button)and click on the document. Y...

» Tip: Editing in print preview


Rate this page:
2.2/5 (213 votes cast)
Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses
Microsoft Certified Partner
Security Seal verified by visa, mastercard securecard

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

Management training

Professional Skills courses
Project Management Course London
Project Management Courses London
Project Management Training London
Project Management Training
Project Seminar
Project Seminars
Time Management Course London
Time Management London
Time Management Courses London
Time Management Training London
Introduction to Finance course
Assertiveness Skills course
Effective Communications Skills training
Presentation Skills London

Training Formats

Public scheduled courses
On-site training
Closed company courses

Consultancy
Application Development

Blogs

Excel Training
MS Project Training
Microsoft Training Blog

Version differences

Office 2010 vs 2007
MS Project version differences

Training Information

London Computer Training
Computer Training London
Docklands Training Courses
Docklands Training London

Training venues London
Client list
FAQ
Pricing and availability
Course details / Syllabus

Training Articles
Training Information

Microsoft training

Microsoft Office training
& IT Applications

Microsoft Project training
Microsoft Outlook training
Microsoft Powerpoint training
Microsoft Word training
MS Project courses
MS Project training
Outlook courses
PowerPoint courses
PowerPoint training
VBA courses
Word courses
Microsoft.training
(more...)

Excel Training

Excel courses
Excel Training Courses Medway
Autonumber in Excel
Microsoft Excel training
Basic Excel Courses
Basic Excel Course
Basic Excel Training

Interested in MS Access training?

Access courses
Microsoft Access training
Microsoft access courses
Microsoft training access course
Microsoft+access+training
Access courses in london

Training provider

Training providers
IT training companies
IT training providers
Management Training providers
Management Training provider

Event history, feedback results
Events in 2012 · 2011 · 2010 · More

See also

Crystal Reports training