Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 7th, 2004, 05:46 PM
Registered User
 
Join Date: Jun 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Appending data from active wkb to inactive wkb

I'm receiving "Run time error '9'; Subscript out of range" when I try to execute a macro I'm working on. I'm trying to create code to append data from BookA SheetA into BookB SheetB when BookA is saved. The amount of data in BookA SheetA is variable (e.g., there could be 2 rows to copy or 200). Regardless of the number of rows in BookA SheetA, I always want the data (values only -- not formulas) to be added to the row following the end of data (first blank row) in BookB SheetB. I would like to leave BookB inactive during this process but haven't gotten that far yet. I've had OJT with macros as needed to automate things but am a beginner with VBA for Excel.

I recorded a macro as a starting point. Working from what the recorded macro generated, I started by leaving BookB active. However, when I run, I receive the following error when the code attempts to perform Windows(BookB).Activate after copying the second range from BookA to paste into BookB(there are two ranges of data I need to copy):
"Run time error '9'; Subscript out of range". When I manually activate BookB and step over this line of code, I can complete my macro successfully.

Here's the actual code, where 1126 is the last row of data in BookB and there are 5 rows to copy from BookA (it's hardcoded now while I try to figure this out):
    Workbooks.Open Filename:=BookB
    Selection.End(xlDown).Select
    Range("A1126:D1130").Select
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
    Windows(BookA).Activate
    ActiveWindow.SmallScroll ToRight:=1
    Range("G2:H6").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows(BookB).Activate
    Range("E1126:F1130").Select
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False

(I'm running Excel 2000 on a WinNT 4 OS. )

Any advice on what is incorrect is appreciated.
 
Old June 8th, 2004, 02:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this approach:

Sub CopyFromBookAToBookB()

'Run this code from a module in BookA

Windows("BookA.xls").Activate
    'Select the range to be copied...
    Range("A1:G3").Select
    Selection.Copy

    'Turn off screenupdating...
    Application.ScreenUpdating = False

        'Open BookB...
        Workbooks.Open Filename:="C:\BookB.xls"

        Windows("BookB.xls").Activate

        'Select last row with value in Column A and move one down
        'to get the inserting point
        Range("A65536").End(xlUp).Select
        ActiveCell.Offset(1, 0).Select

        'Paste values
        Selection.PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False

        'Return to BookA and select the second range to be copied...
        Windows("BookA.xls").Activate
        Range("A7:G9").Select
        Selection.Copy

        'Then back to BookB and repeat the paste procedure
        Windows("BookB.xls").Activate

        Range("A65536").End(xlUp).Select
        ActiveCell.Offset(1, 0).Select

        Selection.PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False

        Range("A1").Select

        'Save and close BookB
        ActiveWorkbook.Save
        ActiveWorkbook.Close

    'Turn on screenupdating
    Application.ScreenUpdating = True

  '..and activate BookA
 Windows("BookA.xls").Activate
    Range("A1").Select

End Sub

It a good idea to try to follow what's happening. Resize BookA and BookB so you can see both on the screen. Save and close BookB. Then, in VBEditor, instead of using Run(F5), use Step Into(F8) to run one line of code at a time. Shift back and forth (Alt/TAB) between the sheets to see what's happening. Just remember to disable the Application.ScreenUpdating lines before doing this. You get a pretty good idea of what's going on.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Inactive Workbook halem2 Beginning VB 6 0 June 8th, 2006 04:42 PM
appending Excel data to VB msflexgrid zach2004 VB How-To 0 July 26th, 2004 09:11 PM
How Long Before We're Considered Inactive? Ben Horne Forum and Wrox.com Feedback 8 April 29th, 2004 10:55 AM
ads active/inactive rclancy BOOK: ASP.NET Website Programming Problem-Design-Solution 2 March 19th, 2004 06:38 PM
Device Controls Inactive brthede VS.NET 2002/2003 0 July 26th, 2003 01:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.