View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Paul Paul is offline
external usenet poster
 
Posts: 21
Default Need a macro to copy a range in one workbook and paste into another workbook

Ken,
Thanks for your code.
It worked great.

One more thing if you wouldn't mind.

Once I have my column in place,
I need column B to run a vlookup. I can do the worksheet function no
problems
But I need it to run down the column as far as column A and no more
as I run a Countif on col B, and 0 will give me an incorrect result.
AS Column A can vary in length each report, i will either have to many
rows or not enough. Too many will come up with 0 or errors.

Any further assistance will be greatly appreciated, if you have the
time.

Cheers Paul


"Ken Oliver" wrote in message ...
Try:

Sub ITHDdocID()
Dim rng1 as Range
Dim rng2 as Range

rng1 = Workbooks("ITHD
Reports").Sheets("Sheet1").Range(Range("A6"),Range ("A6").End(xlDown))
rng2 = Workbooks("ITHD Monthly Calculator for reports
V5").Sheets("Sheet1").Range("B3").Resize(rng1.Rows .Count,1)
rng2.Formula=rng1.Formula

End Sub

You may need to substitute the appropriate sheet names for "Sheet1" above -
I can't tell what you've called them from your code fragment. Otherwise we
can use Copy & Paste, but it's less efficient and bulletproof:

Sub ITHDdocID()
Dim NumOfRows as Integer

Windows("ITHD Reports.xls").Activate
With Range(Range("A6"), Range("A6").End(xlDown))
NumOfRows=.Rows.Count
.Copy
End With
Windows("ITHD Monthly Calculator for reports V5.xls").Activate
ActiveSheet.Range("B3").Resize(NumOfRows,1).Paste

End Sub

We store the number of rows in the region under A6 in the variable
NumOfRows, then use it to measure out the range into which we're pasting.

- Ken

"Paul" wrote in message
om...
Hi all,
I'm experienced in excel, but not very good with Macros.
I am trying to write a macro to copy from 1 workbook into another.
Using Window 2K SP4, and MS Office 2k SR1
So far I have only this using the Macro Recorder:
Sub ITHDdocID()
'
' ITHDdocID Macro
' Macro recorded 25/06/2004 by husky101
'

'
Windows("ITHD Reports.xls").Activate
Range("A6").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("ITHD Monthly Calculator for reports V5.xls").Activate
ActiveSheet.Paste
Range("B3").Select
End Sub

When I run this macro I receive a 'Subscript out of range error', on
line 5 the destination wkbk.
I need to copy an unkown range down from ITHD!A6 from wkbk (ITHD
Reports.xls)
and paste into wkbk2 (ITHD Monthly Calculator for reports V5.xls) into
sheet Time!A3

Any assistance will be greatly appreciated.
Cheers Paul



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.708 / Virus Database: 464 - Release Date: 18/06/2004