|
-
Mar 22nd, 2006, 03:50 AM
#1
Thread Starter
Lively Member
Copy Excel sheet (values only) [Resolved]
Hi - I've read through just about every forum I can find and I hope someone can help me with a problem slightly different to anything I've read.
I want to write a vb6 app. to copy a master pricing Excel Workbook (Master.xls) over to a blank Excel workbook (Pricing.xls) with just the values from each sheet in Master.xls (Don't want clients to see formulaes used in Master pricing workbook).
Number of worksheets and rows of data for each worksheet in Master.xls is variable.
I need to use non version specific coding (eg. Dim xlMaster, xlPricing as Object) so I don't have problems on client end.
Would appreciate any help to point me in the right direction.
thanks.
Last edited by nightshift; Mar 22nd, 2006 at 07:42 AM.
Reason: Resolved
-
Mar 22nd, 2006, 04:08 AM
#2
Frenzied Member
Re: Copy Excel sheet (values only)
On error goto Trap
Trap:
in case of emergency, drop the case...
****************************************
If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option. if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar
-
Mar 22nd, 2006, 04:55 AM
#3
Re: Copy Excel sheet (values only)
Just made this, it copies all data in a workbook and add it to a new workbook pasting all except formulas, works without a reference to Excel and works fine in Excel XP, but I'm not sure about older versions, maybe someone who has an older version could test this code.
VB Code:
Option Explicit
Private Sub Command1_Click()
CloneWorkbook "C:\123.xls"
End Sub
Private Sub CloneWorkbook (pWBPath As String)
'Consts so you don't need a reference to Excel library
Const lcnstPasteValuesAndFormats As Integer = 12
Const lcnstNone As Long = -4142
'Declares
Dim objExcel As Object
Dim objSourceWorkBook As Object
Dim objDestWorkBook As Object
Dim i As Integer
Set objExcel = CreateObject("Excel.Application")
Set objSourceWorkBook = objExcel.Workbooks.Open(pWBPath)
Set objDestWorkBook = objExcel.Workbooks.Add
Clipboard.Clear 'Clear Clipboard
'If target has less sheets than source add sheets
Do While objSourceWorkBook.Sheets.Count < objDestWorkBook.Sheets.Count
objDestWorkBook.Sheets.Add
Loop
For i = 1 To objSourceWorkBook.Sheets.Count 'Loop for each sheet
objSourceWorkBook.Sheets(i).Cells.Copy 'Copy source sheet
objDestWorkBook.Sheets(i).Select 'Select target sheet
'Paste all except formulas
objExcel.Selection.PasteSpecial lcnstPasteValuesAndFormats, lcnstNone, False, False
objDestWorkBook.Sheets(i).Range("A1").Select 'Leave just first cell selected
Next
objDestWorkBook.Sheets(1).Select ' Select first sheet
Clipboard.Clear 'Remove last sheet from clipboard
objExcel.DisplayAlerts = False 'Dont ask about saving
objSourceWorkBook.Close 'Close original Workbook
Set objSourceWorkBook = Nothing
'Show Excel with the new created Workbook, you could save it and close it here also
objExcel.Visible = True
End Sub
Last edited by jcis; Mar 22nd, 2006 at 05:02 AM.
-
Mar 22nd, 2006, 06:52 AM
#4
Re: Copy Excel sheet (values only)
I tried it ASIS on Excel 97 and got an error at:
objExcel.Selection.PasteSpecial lcnstPasteValuesAndFormats, lcnstNone, False, False
The error was "PasteSpecial Method of Range Class Failed"
Run-Time error "1004"
-
Mar 22nd, 2006, 07:27 AM
#5
Frenzied Member
Re: Copy Excel sheet (values only)
It's more efficient to open the XL workbook/Sheet using the ADO provider. I can't find code examples at the minute, anyone?
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Mar 22nd, 2006, 07:43 AM
#6
Thread Starter
Lively Member
Re: Copy Excel sheet (values only) [Resolved]
Many thanks jcis - not sure about that other users error, but worked fine for me. Saving me going mad. Cheers
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|