Results 1 to 6 of 6

Thread: Copy Excel sheet (values only) [Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Location
    Perth - Australia
    Posts
    105

    Resolved 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

  2. #2
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    Re: Copy Excel sheet (values only)

    maybe this link can help you

    http://vbforums.com/showthread.php?t=391154
    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

  3. #3
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     CloneWorkbook "C:\123.xls"
    5. End Sub
    6.  
    7. Private Sub CloneWorkbook (pWBPath As String)
    8. 'Consts so you don't need a reference to Excel library
    9. Const lcnstPasteValuesAndFormats    As Integer = 12
    10. Const lcnstNone                     As Long = -4142
    11. 'Declares
    12. Dim objExcel            As Object
    13. Dim objSourceWorkBook   As Object
    14. Dim objDestWorkBook     As Object
    15. Dim i                   As Integer
    16.  
    17.     Set objExcel = CreateObject("Excel.Application")
    18.     Set objSourceWorkBook = objExcel.Workbooks.Open(pWBPath)
    19.     Set objDestWorkBook = objExcel.Workbooks.Add
    20.    
    21.     Clipboard.Clear 'Clear Clipboard
    22.    
    23.     'If target has less sheets than source add sheets
    24.     Do While objSourceWorkBook.Sheets.Count < objDestWorkBook.Sheets.Count
    25.         objDestWorkBook.Sheets.Add
    26.     Loop
    27.    
    28.     For i = 1 To objSourceWorkBook.Sheets.Count 'Loop for each sheet
    29.         objSourceWorkBook.Sheets(i).Cells.Copy  'Copy source sheet
    30.         objDestWorkBook.Sheets(i).Select        'Select target sheet
    31.         'Paste all except formulas
    32.         objExcel.Selection.PasteSpecial lcnstPasteValuesAndFormats, lcnstNone, False, False
    33.         objDestWorkBook.Sheets(i).Range("A1").Select 'Leave just first cell selected
    34.     Next
    35.     objDestWorkBook.Sheets(1).Select ' Select first sheet
    36.    
    37.     Clipboard.Clear                 'Remove last sheet from clipboard
    38.     objExcel.DisplayAlerts = False  'Dont ask about saving
    39.     objSourceWorkBook.Close         'Close original Workbook
    40.     Set objSourceWorkBook = Nothing
    41.    
    42.     'Show Excel with the new created Workbook, you could save it and close it here also
    43.     objExcel.Visible = True
    44. End Sub
    Last edited by jcis; Mar 22nd, 2006 at 05:02 AM.

  4. #4
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    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"

  5. #5
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Location
    Perth - Australia
    Posts
    105

    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
  •  



Click Here to Expand Forum to Full Width