How do I call up a line of code that references a cell/range in theactive workbook workbook where I

L

Lav

E.g. The macro is in workbook1.xls

I wish to copy cell B2

So Activeworkbook.Select

Dim Workbook1

Set Workbook1 = Workbooks.Add (or is it Open?)("C:Workbook1.xls")

Workbook1.Activate

Range("B2").Copy

?
 
R

royUK

You don't need to select/activate the workbooks. Is the code in th
activeworkbook & you want to copy B2 from workbook1.xls

Code
-------------------

Option Explici

Function IsOpen(wbName As String) As Boolea
Dim Wb As Workboo
On Error Resume Nex
Set Wb = Workbooks(wbName
If Err = 0 Then IsOpen = Tru
End Functio

Sub copyRange(
Dim sFilName As Strin
sFilName = "WorkBook1.xls

If Not IsOpen(sFilName) Then Workbooks.Open
("C:" & sFilName & ".xls"
Workbooks(sFilName).Sheet1.Cells(2, 2).Copy
ThisWorkbook.Sheet1.Cells(2, 2

End Su

-------------------

--
royU

Hope that helps, RoyUK
For tips & examples visit my 'web site
' (http://www.excel-it.com
 
C

Chip Pearson

The ActiveWorkbook object references the workbook that is presently
active in Excel. It may or may not be the workbook that contains the
currently executing code. The ThisWorkbook object references the
workbook that contains the executing code, regardless of what workbook
happens to be active in Excel.

Workbooks.Add (or is it Open?)("C:Workbook1.xls")

The Add method is used to create a new workbook. The Open method is
used to open an existing workbook.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top