Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 28th, 2008, 08:56 AM
Authorized User
 
Join Date: Mar 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Macro for copy pasting without opening the source

Hi All, I'm looking for a macro that can copy paste values from one file to another. Since I've multiple source files, I'm looking for a way to copy paste without opening all the files. Fyi...All the source files are saved in the same location as the output file. Kindly help me with the right command.Thanks!
 
Old March 28th, 2008, 10:53 AM
Authorized User
 
Join Date: Mar 2008
Posts: 35
Thanks: 0
Thanked 1 Time in 1 Post
Default

The general idea is that you have to be specific about which cell addresses you want to copy from the closed files.
The code to use is:
Code:
'for a single cell
Range("A1").Value = "='C:\MyFolder\[1.xls]Sheet1'!A15"
'for more than one cells, always copy on a row by row basis
Range("A2:B2").Value = "='C:\MyFolder\[1.xls]Sheet1'!A15:B15"
The 1.xls is your closed file, the Sheet1 is the name of the Sheet with the data and the C:\MyFolder is its path.
The documentation I have come across about this subject is extremely poor.
 
Old March 31st, 2008, 06:41 AM
Authorized User
 
Join Date: Mar 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, Amazing....Thanks a lot .That works great.One more thing I'm curious about...This seems to copy the cells as links rather than values. Is there a way I can paste these as values rather than link? Many Thanks again!
 
Old March 31st, 2008, 09:34 AM
Authorized User
 
Join Date: Mar 2008
Posts: 35
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by Umasriram2
 Is there a way I can paste these as values rather than links?

Yes there is.
Use this before copying values
Code:
ThisWorkbook.SaveLinkValues = False
and after copying is done you can reinstate the SaveLinkValues to True (if, for whatever reason, you might need to do so).



 
Old April 2nd, 2008, 09:14 AM
Authorized User
 
Join Date: Mar 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks...It helped. But I see the blank rows showing 0's when pasted as values. How do I tackle this...Thanks

 
Old April 2nd, 2008, 10:29 AM
Authorized User
 
Join Date: Mar 2008
Posts: 35
Thanks: 0
Thanked 1 Time in 1 Post
Default

Apologies... I should've mentioned the zeros right from the start.
To avoid them you'll have to do two things.
1. Copy cells one cell at a time.
2. Add the following snippet to the Worksheet_Change event.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ErrorHandler
If Application.EnableEvents = True Then
   Application.EnableEvents = False
   If Target.Value = 0 Then
      Target.Value = ""
   End If
End If
ErrorHandler:
Application.EnableEvents = True
End Sub
The above event code will not work for ranges larger than one cell. Target is always one cell.
P.S. Real zeros being copied over will also be turned to blanks. No way to fix that.
 
Old April 9th, 2008, 02:27 AM
Authorized User
 
Join Date: Mar 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Got it..THANK YOU!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy visible cells in a macro?Runtime 438 error? nbaj2k Excel VBA 2 April 7th, 2010 11:13 AM
on opening workbook, how to disable macro ? jjk2 Beginning VB 6 1 January 7th, 2008 05:05 AM
Copy source-element to output tree der_bAUer XSLT 2 June 9th, 2006 08:17 AM
Copy and Pasting jilly Beginning VB 6 1 March 8th, 2006 09:46 AM
Opening VB.NET 2005 WROX Source code rezien1 Pro Visual Basic 2005 0 December 29th, 2005 04:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.