Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

moving between different workboo

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

Moving between different workbooks

ResolvedVersion 2007

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.

Edited on Fri 17 Apr 2009, 14:55

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

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Removing custom dictionary entries

If you add something to the custom dictionary in Excel you cannot remove it. The way to get around this is to go into word and remove it there.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 2.47 secs.