moving between different workboo
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Moving between different workbooks

Moving between different workbooks

resolvedResolved · Low Priority · Version 2007

replyReply Thu 16 Apr 2009, 16:10Delegate Peter said...

Peter has attended:
Excel VBA Intro Intermediate course

Moving between different workbooks

I want to get data from a series of workbooks (which I don't want to name) and enter the data into my workbook. What code will I need.

For upcoming training course dates see: Pricing & availability

replyReply Fri 17 Apr 2009, 14:26 Edited on Fri 17 Apr 2009, 14:55Trainer Carlos said...

RE: Moving between different workbooks

Hi Peter

The code below is a basic routine that rotates through all the open workbooks and copies data from preset ranges on them to the target (Active Workbook).

Copy the code into a module in the target Workbook and then call it from a button on the workbook.

You will note I put in comments on the code explaining steps.

Sub FindWorkBooks()

Dim WBK As Workbook

Dim intColumn As Integer 'Hold the number of values in the range

Dim intRangeCount As Integer

intColumn = 2 'The data from the first book goes to column 2

For Each WBK In Application.Workbooks

If WBK.Name <> ActiveWorkbook.Name Then

If WBK.Name <> "PERSONAL.XLS" Then 'Ignores the PERSONAL Workbook

'Count the number of cells in the named range and used to set the end cell of target
intRangeCount = RowsInNamedRange(Workbooks(WBK.Name).Sheets("Total Sales").Range("Makes"))

'Sheets(2) refers to the 2nd sheet in the Active workbook. For accuracy replace with the Sheet's name.
Sheets(2).Range(Cells(10, intColumn), Cells(intRangeCount + 9, intColumn)).Value = Workbooks(WBK.Name).Sheets("Total Sales").Range("Makes").Value
'The above copies the data from the Total Sales sheet in each open workbook
'to a new column in the Active workbook

intColumn = intColumn + 1 'Moves the focus to the next empty column

End If

End If

Next WBK

End Sub

'-------------------------------------------

Function RowsInNamedRange(NamedRange As Range) As Long
'This function counts the number of values in the named range

RowsInNamedRange = NamedRange.Rows.Count

End Function


Hope this helps

Carlos

 

Please browse our web site to find out more about
ms excel courses and other Microsoft training courses.

Excel tip:

Large Icons on toolbar

You can make the buttons on your toolbars bigger by going to Tools / Customize / Options / select Large icons.

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard