PC Review


Reply
Thread Tools Rate Thread

Copy from one open workbook to another?

 
 
Geoff
Guest
Posts: n/a
 
      22nd Nov 2009
I asked the following question a few days ago and was given a link to a Ron
Debruin page (with more code I didn't understand). What I would prefer is for
someone to please comment on why my code below doesn't work. I am trying to
learn Excel VBA and considering that the help that comes with the program is
only marginally better than useless, I was hoping for an explanation here.

Please...no more links to Ron Debruin. I'm sure that code is genius, but it
hardly helps if I don't understand it.

Here goes....

-------------------------------------------------------------------------
I have a workbook that gets a lot of use by others. Periodically, I
distribute revisions so I wrote some code to clear all my data (inventory
levels, etc.) but leave the base sheet data (product attributes, etc.), That
was relatively easy.

Now I want to be able to take a cleaned file and put my data back in which
is stumping me because I don't really know how to switch back and forth
between sheets.

One wrinkle is that I don't want to refer to the actual filenames in the
code since they do change.

So I tried this:

iBlank = 1
iData = 2

Windows(iData).Activate
Range("C2:C4").Copy
Windows(iBlank).Activate
Range("C2:C4").Select
ActiveSheet.Paste

I will always be running this from the empty datafile, so if I'm not
mistaken that will have Index = 1 and the one I am copying from will have
Index = 2 if I understand this correctly. There are many ranges that need to
be copied.

So it would seem to me that the above:
* activates the datafile
* copies the range in the datafile
* activates the blankfile
* pastes in the range in the blankfile

The data gets copied and I see the marching ants. But it does not get copied
to the blankfile. I think it just copies it right back to the datafile.

TIA...Geoff
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      22nd Nov 2009
First, I would never trust the windows collection at all -- or the workbooks
collection if I were using a counter.

I'd do something like:

Dim FromWks as worksheet
dim ToWks as worksheet
dim Resp as long

I'd use some other way to determine the workbook that gets the update

maybe even the activeworkbook???
set towks = activeworkbook.worksheets("SomeToNameHere")
or the active sheet

resp = msgbox(Prompt:="You're about to update the activesheet, right?", _
buttons:=vbokcancel)

if resp = vbcancel then
msgbox "try later after you activate the correct sheet"
exit sub
end if

set towks = activesheet

Then if the worksheet that contains the range to copy:
set fromwks = thisworkbook.worksheets("Somesheetnamehere")

or if I wanted to have the code open the workbook:
set fromwks = workbooks.open(filename:="C:\path\newworkbookname.xls") _
.worksheets("someothersheetnamehere")


Then do the copy|paste

fromwks.range("c2:c4").copy _
destination:=towks.range("c2") 'let excel determine the extent of the paste

=====
Depending on activating the window means that the correct worksheet is active.
And I wouldn't trust all those stars to align (for me, anyway!).



Geoff wrote:
>
> I asked the following question a few days ago and was given a link to a Ron
> Debruin page (with more code I didn't understand). What I would prefer is for
> someone to please comment on why my code below doesn't work. I am trying to
> learn Excel VBA and considering that the help that comes with the program is
> only marginally better than useless, I was hoping for an explanation here.
>
> Please...no more links to Ron Debruin. I'm sure that code is genius, but it
> hardly helps if I don't understand it.
>
> Here goes....
>
> -------------------------------------------------------------------------
> I have a workbook that gets a lot of use by others. Periodically, I
> distribute revisions so I wrote some code to clear all my data (inventory
> levels, etc.) but leave the base sheet data (product attributes, etc.), That
> was relatively easy.
>
> Now I want to be able to take a cleaned file and put my data back in which
> is stumping me because I don't really know how to switch back and forth
> between sheets.
>
> One wrinkle is that I don't want to refer to the actual filenames in the
> code since they do change.
>
> So I tried this:
>
> iBlank = 1
> iData = 2
>
> Windows(iData).Activate
> Range("C2:C4").Copy
> Windows(iBlank).Activate
> Range("C2:C4").Select
> ActiveSheet.Paste
>
> I will always be running this from the empty datafile, so if I'm not
> mistaken that will have Index = 1 and the one I am copying from will have
> Index = 2 if I understand this correctly. There are many ranges that need to
> be copied.
>
> So it would seem to me that the above:
> * activates the datafile
> * copies the range in the datafile
> * activates the blankfile
> * pastes in the range in the blankfile
>
> The data gets copied and I see the marching ants. But it does not get copied
> to the blankfile. I think it just copies it right back to the datafile.
>
> TIA...Geoff


--

Dave Peterson
 
Reply With Quote
 
CellShocked
Guest
Posts: n/a
 
      22nd Nov 2009
On Sun, 22 Nov 2009 09:47:02 -0800, Geoff <(E-Mail Removed)> wrote:

>I asked the following question a few days ago and was given a link to a Ron
>Debruin page (with more code I didn't understand). What I would prefer is for
>someone to please comment on why my code below doesn't work. I am trying to
>learn Excel VBA and considering that the help that comes with the program is
>only marginally better than useless, I was hoping for an explanation here.
>
>Please...no more links to Ron Debruin. I'm sure that code is genius, but it
>hardly helps if I don't understand it.
>
>Here goes....
>
>-------------------------------------------------------------------------
>I have a workbook that gets a lot of use by others. Periodically, I
>distribute revisions so I wrote some code to clear all my data (inventory
>levels, etc.) but leave the base sheet data (product attributes, etc.), That
>was relatively easy.
>
>Now I want to be able to take a cleaned file and put my data back in which
>is stumping me because I don't really know how to switch back and forth
>between sheets.
>
>One wrinkle is that I don't want to refer to the actual filenames in the
>code since they do change.
>
>So I tried this:
>
>iBlank = 1
>iData = 2
>
>Windows(iData).Activate
>Range("C2:C4").Copy
>Windows(iBlank).Activate
>Range("C2:C4").Select
>ActiveSheet.Paste
>
>I will always be running this from the empty datafile, so if I'm not
>mistaken that will have Index = 1 and the one I am copying from will have
>Index = 2 if I understand this correctly. There are many ranges that need to
>be copied.
>
>So it would seem to me that the above:
>* activates the datafile
>* copies the range in the datafile
>* activates the blankfile
>* pastes in the range in the blankfile
>
>The data gets copied and I see the marching ants. But it does not get copied
>to the blankfile. I think it just copies it right back to the datafile.
>
>TIA...Geoff



So, bring up the macro recorder, and carefully perform the same task
manually, then stop recording and examine the code.

OR

When you switch to the blank file, your cursor should be in A1, so you
do not need to do anything other than the paste operation (ooops).
Selecting those cells may be what causes the failure. Just select the
upper left corner cell in the paste destination location which matches
your target range.
 
Reply With Quote
 
CellShocked
Guest
Posts: n/a
 
      22nd Nov 2009
On Sun, 22 Nov 2009 12:24:05 -0600, Dave Peterson
<(E-Mail Removed)> wrote:

>destination:=towks.range("c2")




> 'let excel determine the extent of the paste



This is what he wanted to know about why his may not have worked.
 
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
Copy Range from Open Workbook to Active Workbook Forgone Microsoft Excel Programming 0 17th Jul 2009 01:46 AM
Copy data from another workbook to any open workbook Coder1215 Microsoft Excel Programming 3 2nd Jan 2009 01:11 PM
How to open a workbook as a copy? c mateland Microsoft Excel Programming 5 15th Mar 2007 08:00 PM
xp sp3 10.6501.6735 SaveCopyAs, Open copy, SaveAs copy sequence Closes first workbook 4usenet Microsoft Excel Crashes 2 8th Jan 2006 07:11 AM
copy row to another open workbook =?Utf-8?B?QmlsbA==?= Microsoft Excel Programming 1 15th Aug 2005 07:26 PM


Features
 

Advertising
 

Newsgroups
 


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