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.
|