+ Reply to Thread
Results 1 to 9 of 9

A macro that copies a sheet to a new workbook pasted AS VALUES and AS FORMULA on another?

  1. #1
    Forum Contributor
    Join Date
    12-03-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    107

    A macro that copies a sheet to a new workbook pasted AS VALUES and AS FORMULA on another?

    I have to make this excel program and it's due in about 10 days and i'm very nervous as it's getting nearer and nearer, i've tried learning online but it's just too hard for me to squeeze in a short amount of time, so here is my question: (i'll use my case as an example so it'll be easy to understand)

    I have this QUOTATION ESTIMATE workbook that has a QUOTATION sheet and a PRICELIST sheet, the quotation sheet is where our engineers make estimates for our clients and this sheet has vlookup functions with table arrays on its neighbor PRICELIST sheet. When they're done making the quotation they'll export (copy) the QUOTATION sheet to another workbook in a folder let's say CUSTOMER_QUOTES and the data there will be AS VALUES so the customer wont be able to see the formulas (and other cells that have vlookups on the pricelist sheet wont produce an error if they're e-mailed to the customers). Aside from that, they'll also export a copy of the quotation sheet to a workbook in a folder let's say CUSTOMER_QUOTES_REFERENCE, this time, pasted as FORMULAS. The reason behind this is that if ever they need to revise the quotation, they can just put that reference file back to the original QUOTATION ESTIMATE workbook and all formulas will work again.

    So how do i write a macro for this? A sheet in one file that when i press export, it will make a copy of the sheet in a new workbook in a CUSTOMER_QUOTES folder (pasted as values) and another one copied in the CUSTOMER_QUOTES_REFERENCE (pasted as formula). I really need help please, thanks...
    Last edited by kjy1989; 01-25-2013 at 04:22 AM.

  2. #2
    Forum Contributor
    Join Date
    08-20-2012
    Location
    Walsall,England
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: A macro that copies a sheet to a new workbook pasted AS VALUES and AS FORMULA on anoth

    Hi,

    Give this a go, changing the two string strWorkbook to the relevant workbook paths/filenames:

    Please Login or Register  to view this content.
    Last edited by anteagles20; 01-25-2013 at 10:11 AM.

  3. #3
    Forum Contributor
    Join Date
    12-03-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    107

    Re: A macro that copies a sheet to a new workbook pasted AS VALUES and AS FORMULA on anoth

    Wow it looks more complicated as what i imagined it would be. Anyway i'll give it a go and post here asap, thanks for the help anteagles20!

  4. #4
    Forum Contributor
    Join Date
    12-03-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    107

    Re: A macro that copies a sheet to a new workbook pasted AS VALUES and AS FORMULA on anoth

    Hi Anteagles20, I just tried your code and it produces this error: Run-time error '1004': Excel cannot access 'CUSTOMER_QUOTES'. The document may be read-only or encrypted. And when i press debug the yellow highlight is on this line:

    Workbooks.Open (strWorkbook(intLoop))

    by the way this is the code i copied in the file:

    Public Sub copyingSub()
    Dim strWorkbook(1 To 2) As String
    Dim intLoop As Integer
    Dim wkbMyWorkbook As Workbook

    strWorkbook(1) = "C:\Users\Jason\Desktop\Excelforumhelp\CUSTOMER_QUOTES"
    strWorkbook(2) = "C:\Users\Jason\Desktop\Excelforumhelp\CUSTOMER_QUOTES_REFERENCE"


    For intLoop = 1 To 2

    Workbooks.Open (strWorkbook(intLoop))
    Set wkbMyWorkbook = ActiveWorkbook
    wkbMyWorkbook.Sheets.Add After:=Sheets(Sheets.Count)
    wkbMyWorkbook.Worksheets(Worksheets.Count).Name = "Quotations"

    ThisWorkbook.Worksheets("Quotations").Cells.Copy

    If intLoop = 1 Then
    wkbMyWorkbook.Worksheets("Quotations").Cells.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    ElseIf intLoop = 2 Then
    wkbMyWorkbook.Worksheets("Quotations").Cells.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    End If

    wkbMyWorkbook.Close True
    Set wkbMyWorkbook = Nothing

    Next intLoop

    End Sub

    I tried removing the password of the QUOTATION ESTIMATE file it didn't work, i also tried changing the target folder and it doesn't work. I don't know why excel can't access the targets i put.

    For better understanding, here is the QUOTATION ESTIMATE file i made: QUOTATION ESTIMATE.xlsm The code is in module5

    i apologize if it seems i'm "making you do my homework" but i am trying to understand every line of code you're writing so i could improve on my vba skills. Again, thank you for your help

  5. #5
    Forum Contributor
    Join Date
    08-20-2012
    Location
    Walsall,England
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: A macro that copies a sheet to a new workbook pasted AS VALUES and AS FORMULA on anoth

    Hmm, not sure. Try including the file extension as well e.g
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    08-20-2012
    Location
    Walsall,England
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: A macro that copies a sheet to a new workbook pasted AS VALUES and AS FORMULA on anoth

    Also edited original post with comments to explain process, hopefully useful if you are learning vba.

  7. #7
    Forum Contributor
    Join Date
    08-20-2012
    Location
    Walsall,England
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: A macro that copies a sheet to a new workbook pasted AS VALUES and AS FORMULA on anoth

    Just tested it out and you definitely need to include the file extension. Hopefully that is the issue.

  8. #8
    Forum Contributor
    Join Date
    08-20-2012
    Location
    Walsall,England
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: A macro that copies a sheet to a new workbook pasted AS VALUES and AS FORMULA on anoth

    Also should probably mention for future reference - when putting code in a post you need to use the code tags. This is the # button above where you type the post.

  9. #9
    Forum Contributor
    Join Date
    12-03-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    107

    Re: A macro that copies a sheet to a new workbook pasted AS VALUES and AS FORMULA on anoth

    Hi anteagles,

    It's still not working for me I'm a little confused. My intention is to put the copied sheet into a QUOTATION_ESTIMATE folder and a QUOTATION_ESTIMATE_REFERENCE folder. Did you think that the copied sheet will be copied and named as QUOTATION_ESTIMATE.xlsm and QUOTATION_ESTIMATE_REFERENCE.xlsm that's why you suggested the extension? This is the error it gives me when i put an xlsm extention:

    Run-time error '1004':

    'C:\Users\Kevin\Desktop\Excelforumhelp\Customer_Quotes.xlsm' could not be found. Check the spelling of the file name, and verify that the file location is correct. If you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.

    Anyway, i appreciated the comments on the codes you wrote, am learning a lot!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1