Microsoft KB Archive/151320

= Microsoft Knowledge Base =

Putting Cell Contents from Closed Workbook to Active Workbook
Last reviewed: March 27, 1997

Article ID: Q151320

5.00 5.00c 7.00 | 5.00 5.00a

WINDOWS        | MACINTOSH kbprog

The information in this article applies to:


 * Microsoft Excel for Windows, versions 5.0, 5.0c
 * Microsoft Excel for Windows 95, version 7.0
 * Microsoft Excel for the Macintosh, versions 5.0, 5.0a

SUMMARY

With Visual Basic for Applications, the contents of a cell in a closed workbook cannot be directly put into a cell in an active workbook. However, you can do this indirectly by linking a cell in the active workbook to the cell in the closed workbook, and then copying that cell and pasting the value back, using the Paste Special command.

MORE INFORMATION
Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

The following line of code will put the contents of a cell in SOURCE.XLS into the active workbook only if SOURCE.XLS is open.

Sheets("sheet1").Range("a1") = Workbooks("source.xls").Sheets("sheet1").Range("a1") You can use the following macro to accomplish the same result with SOURCE.XLS not open.

Sub Macro1 Sheets("sheet1").Range("a1").Formula = "='C:\Excel\[source.xls]Sheet1'!$A$1"  ' Link a cell to source. Sheets("sheet1").Range("a1").Copy      ' Copy linked cell. ' Paste value back into cell. Sheets("sheet1").Range("a1").PasteSpecial Paste:=xlValues 'Cancel Copy mode and remove the moving border. Application.CutCopyMode = False End Sub