Copying from Multiple Workbooks into One :: MrExcel Message Board


 FAQFAQ
   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   FavoritesFavorites   StatisticsStatistics 
 RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Online StoreOnline Store

MrExcel Message Board Forum Index -> Excel Questions

Copying from Multiple Workbooks into One
Post new topic   Reply to topic
Last Thread | Next Thread  >   Printable version
  Author    Thread

AllShookDown
Board Regular


Joined: 07 May 2002
Posts: 48
Location: Wisconsin
Flag: Usa

Status: Offline

 Reply with quote  

Copying from Multiple Workbooks into One

I'm in over my head again and could use some help.

I have multiple versions of a "Region" workbook. Each version will be copied to a different server. Each version will be copying data from a range in one or more "State" workbooks to be stacked in a range in the "Region" workbook.

Here's the code I've written. As soon as I step into it I get "Compile Error. Type Mismatch." and it highlights StateBook in "Workbooks.Open StateBook". From the help file it seems that dimming this as a variant is causing the problem but I don't know how I should do it instead. Can someone tell me how to fix this?

code:

Sub CopyPremiumData()

RegionBook = ActiveWorkbook.Name
Dim StateBook(3) As Variant

StateBook(0) = "Test1 Model.xls"
StateBook(1) = "Test2 Model.xls"
StateBook(2) = "Test3 Model.xls"

Dim x As Integer
   
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    RegionBook.Activate
    ChDir ThisWorkbook.Path
   
    Sheets("PremData").Visible = True
    Application.Goto Reference:="PremiumDelete"
    Selection.ClearContents
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For x = 1 To 3

    RegionBook.Activate
    Application.Goto Reference:="PremInStart"
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
   
On Error GoTo ErrorCheck
    Workbooks.Open StateBook ''''''This is where the type mismatch error is
On Error GoTo 0

    StateBook.Activate
    Calculate
   
    Sheets("BPMData").Visible = True
    Application.Goto Reference:="DataOut"
    Selection.Copy
    RegionBook.Activate
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False

    StateBook.Activate
    ActiveWorkbook.Close

Next x

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    RegionBook.Activate
    Sheets("PremData").Visible = False

Exit Sub

ErrorCheck:
MsgBox "All workbooks must be in the same directory."

Exit Sub
    RegionBook.Activate
    Sheets("PremData").Visible = False
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub


Post Fri Sep 19, 2003 12:17 pm 
 View user's profile Send private message

Andrew Poulsom
MrExcel MVP


Joined: 22 Jul 2002
Posts: 8266

Flag: Uk

Status: Offline

 Reply with quote  

Re: Copying from Multiple Workbooks into One

I think it should be:

Workbooks.Open StateBook(x)

and your assignment statements should be:

Dim StateBook(1 To 3) As Variant

StateBook(1) = "Test1 Model.xls"
StateBook(2) = "Test2 Model.xls"
StateBook(3) = "Test3 Model.xls"

Post Fri Sep 19, 2003 12:28 pm 
 View user's profile Send private message

AllShookDown
Board Regular


Joined: 07 May 2002
Posts: 48
Location: Wisconsin
Flag: Usa

Status: Offline

 Reply with quote  

Re: Copying from Multiple Workbooks into One

Thanks, that helps. I can step through it now. But I've got another error I don't know what to do with. I'm getting "Runtime error 424, Object Required" when it hits the first "RegionBook.Activate". The help file on that one is totally confusing to me.

Post Fri Sep 19, 2003 12:38 pm 
 View user's profile Send private message

Andrew Poulsom
MrExcel MVP


Joined: 22 Jul 2002
Posts: 8266

Flag: Uk

Status: Offline

 Reply with quote  

Re: Copying from Multiple Workbooks into One

Instead of:

RegionBook = ActiveWorkbook.Name ' a string variable

use:

Set RegionBook = ActiveWorkbook ' an object variable

Or instead of:

RegionBook.Activate

use

Workbooks(RegionBook).Activate

Currently you trying to us the Activate method with a string, but an object is required. I agree the Help isn't helpful. Trust me, you'll be able to make sense of it eventually.

Post Fri Sep 19, 2003 12:51 pm 
 View user's profile Send private message

AllShookDown
Board Regular


Joined: 07 May 2002
Posts: 48
Location: Wisconsin
Flag: Usa

Status: Offline

 Reply with quote  

Re: Copying from Multiple Workbooks into One

That did it. Thank you sooo much, Andrew!

Post Fri Sep 19, 2003 1:27 pm 
 View user's profile Send private message
  Display posts from previous:      

MrExcel Message Board Forum Index -> Excel Questions


Forum Jump:
Jump to:  

Post new topic   Reply to topic
Page 1 of 1



Add To Favorites

 


Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB: 2.0.4 © 2001 phpBB Group

Need help posting your first question? Read how to post

Need extra help ? Couldn't get the answer you needed ? Get a free quote from our Consulting Team

Download Colo's HTML Maker utility for displaying your Excel Worksheet on the board.

Download VB HTML Maker to post your code on the board


Check out our new index to 485 Excel Articles.


Return to MrExcel Consulting

All contents Copyright 1998-2004 by MrExcel.com
If you believe information posted here is from your copyrighted source, notify us per the Terms of Use
Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.