?
Solved

Copy Data from one Excel Spreadsheet to another without being able to refresh using a VB Module

Posted on 2008-02-11
4
Medium Priority
?
1,049 Views
Last Modified: 2011-10-03
I have VB Code that copies data from one spreadsheet to another but I want to copy the data only not the query (  I want to create a new spreadsheet without the "Refresh" capability.

Does anyone know how to do this using VB Code.  I can manually do it, but would like to code it so it can be put in an automated process if possible
0
Comment
Question by:morinia
  • 2
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 1000 total points
ID: 20871048
Hi, try this macro.  Specify your second workbook to copy to, and change sheet names if required.

Sub CopyValuesOnly()
    Set wb1 = ActiveWorkbook
    strWorkbook2 = "C:\TEMP\Temp\Test script\Excel\Test2.xls"
    Set wb2 = Workbooks.Open(Filename:=strWorkbook2)
    Application.CutCopyMode = False
    wb1.Sheets("Sheet1").Cells.Copy
    wb2.Sheets("Sheet1").Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    wb2.Sheets("Sheet1").Range("A1").Select
    'wb1.Sheets("Sheet1").Range("A1").Select
    wb2.Save
    wb2.Close
    MsgBox "Data copied"
End Sub


Regards,

Rob.
0
 

Author Comment

by:morinia
ID: 20876160
Rob,

The solution worked perfectly.  Is there a way to execute this outside of Excel using a VB Script?
0
 

Author Comment

by:morinia
ID: 20877397
I would like to award the points
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20879778
Yes, you can use this outside Excel. Put this in a VBS file:

'====================
Set objExcel = CreateObject("Excel.Application")
Const xlPasteValues = -4163
Const xlNone = -4142
strWorkbook1 = "C:\TEMP\Temp\Test script\Excel\Test.xls"
strWorkbook2 = "C:\TEMP\Temp\Test script\Excel\Test2.xls"
Set wb1 = objExcel.Workbooks.Open(strWorkbook1, False, False)
Set wb2 = objExcel.Workbooks.Open(strWorkbook2, False, False)
objExcel.Visible = False
'objExcel.CutCopyMode = False
wb1.Sheets("Sheet1").Cells.Copy
wb2.Sheets("Sheet1").Range("A1").Select
objExcel.Selection.PasteSpecial xlPasteValues, xlNone, False, False
wb2.Sheets("Sheet1").Range("A1").Select
'wb1.Sheets("Sheet1").Range("A1").Select
wb2.Save
wb2.Close
wb1.Close
objExcel.Quit
Set objExcel = Nothing
MsgBox "Data copied"
'====================

Regards,

Rob.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a serious pitfall that can happen when deleting shapes using VBA.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question