You have declared wb1 and wb2 as Workbook objects, but in the line of code
Set wb2 = "c:\library.xls"
you are attempting to put a string of text in the wb2 variable. (VBA doesn't
see this as a workbook name or file name -- it is simply a series of text
characters) and thus you are getting the error. Change that line to
Set wb2 = Workbooks("Library.xls")
This assumes that the file Library.xls is already open in Excel. If it is
not, use the following instead:
Set wb2 = Workbooks.Open("c:\library.xls")
In the first line of code, you do not (and indeed cannot) use the drive and
folder path, since Excel allows only one workbook having the same file name
to be open at the same time. For example, you cannot have both
C:\Test\Book1.xls and C:\Test2\Book1.xls open at the same time, since both
have the same file name (file name only, not drive or folder name). In
second example, you should provide the full file name, including the drive
and folder names.
If you don't know whether C:\Library.xls is open or not, you can use code
like
On Error Resume Next
Set wb2 = Workbooks("Library.xls")
If wb2 Is Nothing Then
' file was not open. open it now:
Set wb2 = Workbooks.Open("C:\Library.xls")
End If
This code attempts to access an open workbook named "Library.xls". If there
is no such workbook open, it opens it (assuming the file exists).
--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)