PC Review


Reply
Thread Tools Rate Thread

Copy from one workbook to another

 
 
Karen53
Guest
Posts: n/a
 
      13th Mar 2008
Hi,

I have two workbooks in a folder on my desktop. I want to copy from one
workbook to the other workbook. It tells me it cannot locate the file. It
did run once but I have not been able to get it to run again. I have tried
the path various ways with no success. Have I missed something?

Here is my code:

Sub wkbookCreate()

Dim wbkCopyFrom As Workbook
Dim rngCopyFrom As Range
Dim rngCopyTo As Range
Dim wbkName As String

On Error Resume Next

Set wbkCopyFrom = Workbooks("test.xls")
If wbkCopyFrom Is Nothing Then
Set wbkCopyFrom = Workbooks.Open("test.xls")
On Error GoTo Done
If wbkCopyFrom Is Nothing Then
MsgBox "Cannot find originating file"
Else
Application.ScreenUpdating = False

ThisWorkbook.Sheets((Replace(Tablespg.Name, "'", "''"))). _
Unprotect
Password:=([MyPassword])

'Pool lists
'CAM
Set rngCopyFrom = wbkCopyFrom.Sheets((Replace(Tablespg.Name, _
"'",
"''"))).Range("J4:J21")
Set rngCopyTo = ThisWorkbook.Sheets((Replace(Tablespg.Name, _

"'", "''"))).Range("J4:J21")
rngCopyTo.Value = rngCopyFrom.Value

ThisWorkbook.Sheets((Replace(Tablespg.Name, "'", "''"))). _
Protect
Password:=([MyPassword])

ThisWorkbook.Sheets((Replace(GrossUpspg.Name, "'", "''"))). _
Unprotect
Password:=([MyPassword])
'Cam Pools list 2
Set rngCopyFrom = wbkCopyFrom.Sheets((Replace(GrossUppg.Name, _
"'",
"''"))).Range("B15:J16")
Set rngCopyTo = ThisWorkbook.Sheets((Replace(GrossUppg.Name, +
"'",
"''"))).Range("B15:J16")
rngCopyTo.Value = rngCopyFrom.Value

ThisWorkbook.Sheets((Replace(GrossUppg.Name, "'", "''"))). _
Protect
Password:=([MyPassword])
End If
End If
Done:
Application.ScreenUpdating = True
End Sub

--
Thanks for your help.
Karen53
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      13th Mar 2008
Maybe specifying the complete path to the file that should be opened would help:

Set wbkCopyFrom = Workbooks.Open("test.xls")

would look more like:

Set wbkCopyFrom _
= Workbooks.Open("C:\Documents and Settings\David Peterson\Desktop\test.xls")

for me.

If test.xls is already open, does your code work ok? If no, what line causes
the error?

Karen53 wrote:
>
> Hi,
>
> I have two workbooks in a folder on my desktop. I want to copy from one
> workbook to the other workbook. It tells me it cannot locate the file. It
> did run once but I have not been able to get it to run again. I have tried
> the path various ways with no success. Have I missed something?
>
> Here is my code:
>
> Sub wkbookCreate()
>
> Dim wbkCopyFrom As Workbook
> Dim rngCopyFrom As Range
> Dim rngCopyTo As Range
> Dim wbkName As String
>
> On Error Resume Next
>
> Set wbkCopyFrom = Workbooks("test.xls")
> If wbkCopyFrom Is Nothing Then
> Set wbkCopyFrom = Workbooks.Open("test.xls")
> On Error GoTo Done
> If wbkCopyFrom Is Nothing Then
> MsgBox "Cannot find originating file"
> Else
> Application.ScreenUpdating = False
>
> ThisWorkbook.Sheets((Replace(Tablespg.Name, "'", "''"))). _
> Unprotect
> Password:=([MyPassword])
>
> 'Pool lists
> 'CAM
> Set rngCopyFrom = wbkCopyFrom.Sheets((Replace(Tablespg.Name, _
> "'",
> "''"))).Range("J4:J21")
> Set rngCopyTo = ThisWorkbook.Sheets((Replace(Tablespg.Name, _
>
> "'", "''"))).Range("J4:J21")
> rngCopyTo.Value = rngCopyFrom.Value
>
> ThisWorkbook.Sheets((Replace(Tablespg.Name, "'", "''"))). _
> Protect
> Password:=([MyPassword])
>
> ThisWorkbook.Sheets((Replace(GrossUpspg.Name, "'", "''"))). _
> Unprotect
> Password:=([MyPassword])
> 'Cam Pools list 2
> Set rngCopyFrom = wbkCopyFrom.Sheets((Replace(GrossUppg.Name, _
> "'",
> "''"))).Range("B15:J16")
> Set rngCopyTo = ThisWorkbook.Sheets((Replace(GrossUppg.Name, +
> "'",
> "''"))).Range("B15:J16")
> rngCopyTo.Value = rngCopyFrom.Value
>
> ThisWorkbook.Sheets((Replace(GrossUppg.Name, "'", "''"))). _
> Protect
> Password:=([MyPassword])
> End If
> End If
> Done:
> Application.ScreenUpdating = True
> End Sub
>
> --
> Thanks for your help.
> Karen53


--

Dave Peterson
 
Reply With Quote
 
Karen53
Guest
Posts: n/a
 
      13th Mar 2008
Hi Dave,

It doesn't whether the file is open or not. I'll try it with the complete
path.

By the way, does all of the code for a worksheet calculate procedure have to
reside in the worksheet module or can it call out to a general module?
--
Thanks for your help.
Karen53


"Dave Peterson" wrote:

> Maybe specifying the complete path to the file that should be opened would help:
>
> Set wbkCopyFrom = Workbooks.Open("test.xls")
>
> would look more like:
>
> Set wbkCopyFrom _
> = Workbooks.Open("C:\Documents and Settings\David Peterson\Desktop\test.xls")
>
> for me.
>
> If test.xls is already open, does your code work ok? If no, what line causes
> the error?
>
> Karen53 wrote:
> >
> > Hi,
> >
> > I have two workbooks in a folder on my desktop. I want to copy from one
> > workbook to the other workbook. It tells me it cannot locate the file. It
> > did run once but I have not been able to get it to run again. I have tried
> > the path various ways with no success. Have I missed something?
> >
> > Here is my code:
> >
> > Sub wkbookCreate()
> >
> > Dim wbkCopyFrom As Workbook
> > Dim rngCopyFrom As Range
> > Dim rngCopyTo As Range
> > Dim wbkName As String
> >
> > On Error Resume Next
> >
> > Set wbkCopyFrom = Workbooks("test.xls")
> > If wbkCopyFrom Is Nothing Then
> > Set wbkCopyFrom = Workbooks.Open("test.xls")
> > On Error GoTo Done
> > If wbkCopyFrom Is Nothing Then
> > MsgBox "Cannot find originating file"
> > Else
> > Application.ScreenUpdating = False
> >
> > ThisWorkbook.Sheets((Replace(Tablespg.Name, "'", "''"))). _
> > Unprotect
> > Password:=([MyPassword])
> >
> > 'Pool lists
> > 'CAM
> > Set rngCopyFrom = wbkCopyFrom.Sheets((Replace(Tablespg.Name, _
> > "'",
> > "''"))).Range("J4:J21")
> > Set rngCopyTo = ThisWorkbook.Sheets((Replace(Tablespg.Name, _
> >
> > "'", "''"))).Range("J4:J21")
> > rngCopyTo.Value = rngCopyFrom.Value
> >
> > ThisWorkbook.Sheets((Replace(Tablespg.Name, "'", "''"))). _
> > Protect
> > Password:=([MyPassword])
> >
> > ThisWorkbook.Sheets((Replace(GrossUpspg.Name, "'", "''"))). _
> > Unprotect
> > Password:=([MyPassword])
> > 'Cam Pools list 2
> > Set rngCopyFrom = wbkCopyFrom.Sheets((Replace(GrossUppg.Name, _
> > "'",
> > "''"))).Range("B15:J16")
> > Set rngCopyTo = ThisWorkbook.Sheets((Replace(GrossUppg.Name, +
> > "'",
> > "''"))).Range("B15:J16")
> > rngCopyTo.Value = rngCopyFrom.Value
> >
> > ThisWorkbook.Sheets((Replace(GrossUppg.Name, "'", "''"))). _
> > Protect
> > Password:=([MyPassword])
> > End If
> > End If
> > Done:
> > Application.ScreenUpdating = True
> > End Sub
> >
> > --
> > Thanks for your help.
> > Karen53

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      13th Mar 2008
You can call a procedure in a different module in the worksheet_Calculate event.

Karen53 wrote:
>
> Hi Dave,
>
> It doesn't whether the file is open or not. I'll try it with the complete
> path.
>
> By the way, does all of the code for a worksheet calculate procedure have to
> reside in the worksheet module or can it call out to a general module?
> --
> Thanks for your help.
> Karen53
>
> "Dave Peterson" wrote:
>
> > Maybe specifying the complete path to the file that should be opened would help:
> >
> > Set wbkCopyFrom = Workbooks.Open("test.xls")
> >
> > would look more like:
> >
> > Set wbkCopyFrom _
> > = Workbooks.Open("C:\Documents and Settings\David Peterson\Desktop\test.xls")
> >
> > for me.
> >
> > If test.xls is already open, does your code work ok? If no, what line causes
> > the error?
> >
> > Karen53 wrote:
> > >
> > > Hi,
> > >
> > > I have two workbooks in a folder on my desktop. I want to copy from one
> > > workbook to the other workbook. It tells me it cannot locate the file. It
> > > did run once but I have not been able to get it to run again. I have tried
> > > the path various ways with no success. Have I missed something?
> > >
> > > Here is my code:
> > >
> > > Sub wkbookCreate()
> > >
> > > Dim wbkCopyFrom As Workbook
> > > Dim rngCopyFrom As Range
> > > Dim rngCopyTo As Range
> > > Dim wbkName As String
> > >
> > > On Error Resume Next
> > >
> > > Set wbkCopyFrom = Workbooks("test.xls")
> > > If wbkCopyFrom Is Nothing Then
> > > Set wbkCopyFrom = Workbooks.Open("test.xls")
> > > On Error GoTo Done
> > > If wbkCopyFrom Is Nothing Then
> > > MsgBox "Cannot find originating file"
> > > Else
> > > Application.ScreenUpdating = False
> > >
> > > ThisWorkbook.Sheets((Replace(Tablespg.Name, "'", "''"))). _
> > > Unprotect
> > > Password:=([MyPassword])
> > >
> > > 'Pool lists
> > > 'CAM
> > > Set rngCopyFrom = wbkCopyFrom.Sheets((Replace(Tablespg.Name, _
> > > "'",
> > > "''"))).Range("J4:J21")
> > > Set rngCopyTo = ThisWorkbook.Sheets((Replace(Tablespg.Name, _
> > >
> > > "'", "''"))).Range("J4:J21")
> > > rngCopyTo.Value = rngCopyFrom.Value
> > >
> > > ThisWorkbook.Sheets((Replace(Tablespg.Name, "'", "''"))). _
> > > Protect
> > > Password:=([MyPassword])
> > >
> > > ThisWorkbook.Sheets((Replace(GrossUpspg.Name, "'", "''"))). _
> > > Unprotect
> > > Password:=([MyPassword])
> > > 'Cam Pools list 2
> > > Set rngCopyFrom = wbkCopyFrom.Sheets((Replace(GrossUppg.Name, _
> > > "'",
> > > "''"))).Range("B15:J16")
> > > Set rngCopyTo = ThisWorkbook.Sheets((Replace(GrossUppg.Name, +
> > > "'",
> > > "''"))).Range("B15:J16")
> > > rngCopyTo.Value = rngCopyFrom.Value
> > >
> > > ThisWorkbook.Sheets((Replace(GrossUppg.Name, "'", "''"))). _
> > > Protect
> > > Password:=([MyPassword])
> > > End If
> > > End If
> > > Done:
> > > Application.ScreenUpdating = True
> > > End Sub
> > >
> > > --
> > > Thanks for your help.
> > > Karen53

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      13th Mar 2008
If the file is open, then what line causes that error message?

Karen53 wrote:
>
> Hi Dave,
>
> It doesn't whether the file is open or not. I'll try it with the complete
> path.
>
> By the way, does all of the code for a worksheet calculate procedure have to
> reside in the worksheet module or can it call out to a general module?
> --
> Thanks for your help.
> Karen53
>
> "Dave Peterson" wrote:
>
> > Maybe specifying the complete path to the file that should be opened would help:
> >
> > Set wbkCopyFrom = Workbooks.Open("test.xls")
> >
> > would look more like:
> >
> > Set wbkCopyFrom _
> > = Workbooks.Open("C:\Documents and Settings\David Peterson\Desktop\test.xls")
> >
> > for me.
> >
> > If test.xls is already open, does your code work ok? If no, what line causes
> > the error?
> >
> > Karen53 wrote:
> > >
> > > Hi,
> > >
> > > I have two workbooks in a folder on my desktop. I want to copy from one
> > > workbook to the other workbook. It tells me it cannot locate the file. It
> > > did run once but I have not been able to get it to run again. I have tried
> > > the path various ways with no success. Have I missed something?
> > >
> > > Here is my code:
> > >
> > > Sub wkbookCreate()
> > >
> > > Dim wbkCopyFrom As Workbook
> > > Dim rngCopyFrom As Range
> > > Dim rngCopyTo As Range
> > > Dim wbkName As String
> > >
> > > On Error Resume Next
> > >
> > > Set wbkCopyFrom = Workbooks("test.xls")
> > > If wbkCopyFrom Is Nothing Then
> > > Set wbkCopyFrom = Workbooks.Open("test.xls")
> > > On Error GoTo Done
> > > If wbkCopyFrom Is Nothing Then
> > > MsgBox "Cannot find originating file"
> > > Else
> > > Application.ScreenUpdating = False
> > >
> > > ThisWorkbook.Sheets((Replace(Tablespg.Name, "'", "''"))). _
> > > Unprotect
> > > Password:=([MyPassword])
> > >
> > > 'Pool lists
> > > 'CAM
> > > Set rngCopyFrom = wbkCopyFrom.Sheets((Replace(Tablespg.Name, _
> > > "'",
> > > "''"))).Range("J4:J21")
> > > Set rngCopyTo = ThisWorkbook.Sheets((Replace(Tablespg.Name, _
> > >
> > > "'", "''"))).Range("J4:J21")
> > > rngCopyTo.Value = rngCopyFrom.Value
> > >
> > > ThisWorkbook.Sheets((Replace(Tablespg.Name, "'", "''"))). _
> > > Protect
> > > Password:=([MyPassword])
> > >
> > > ThisWorkbook.Sheets((Replace(GrossUpspg.Name, "'", "''"))). _
> > > Unprotect
> > > Password:=([MyPassword])
> > > 'Cam Pools list 2
> > > Set rngCopyFrom = wbkCopyFrom.Sheets((Replace(GrossUppg.Name, _
> > > "'",
> > > "''"))).Range("B15:J16")
> > > Set rngCopyTo = ThisWorkbook.Sheets((Replace(GrossUppg.Name, +
> > > "'",
> > > "''"))).Range("B15:J16")
> > > rngCopyTo.Value = rngCopyFrom.Value
> > >
> > > ThisWorkbook.Sheets((Replace(GrossUppg.Name, "'", "''"))). _
> > > Protect
> > > Password:=([MyPassword])
> > > End If
> > > End If
> > > Done:
> > > Application.ScreenUpdating = True
> > > End Sub
> > >
> > > --
> > > Thanks for your help.
> > > Karen53

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Copy all worksheets to another workbook, excl. duplicate sheets already in other workbook Chip Pearson Microsoft Excel Programming 0 26th May 2009 04:27 PM
Copy Autofilter Source Workbook A result in Destination Workbook BSheet1 u473 Microsoft Excel Programming 1 9th Sep 2008 05:14 PM
Copy data from Workbook Alpha & reorganize it in Workbook Bravo u473 Microsoft Excel Programming 1 31st Oct 2007 02:37 AM
loop through a column on a workbook copying data on each row to another workbook, then copy data back to the original workbook burl_rfc Microsoft Excel Programming 1 1st Apr 2006 08:48 PM
Copy a range of cells in an unopened workbook and paste it to the current workbook topstar Microsoft Excel Programming 3 24th Jun 2004 12:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:30 AM.