简体   繁体   中英

Error when creating new workbook with vba, copying 2 of many sheets

The following code results in an .xls with the 2 worksheets I want. Only I don't end up with just values.. the formatting is in there (and I don't think it should be. paste. values. dammit.), and the .xls has a unbreakable link to the original document that doesn't need to be there (and I'm thinking shouldn't be there). All the cells only contain values and do not contain any of the equations contained in the source workbook.

When I open the newly created .xls I receive the message "the file format and extension of [ws name].xls don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. do you want to open it anyway?"

I loathe the lack of trust.. :)

What am I doing wrong?

Sub QUOTE_ITEM_OUTPUT()

    Dim ws As Worksheet

    Dim Filename As String
    Dim Filelocation As String
    Dim UserName As String
    Dim Password As String

        Filename = Worksheets("CALCULATION PAGE").Range("ITEMNUM").Value & "_" & Worksheets("CALCULATION PAGE").Range("PDFSAVEREV").Value & ".xls"
        Filelocation = "\\GSWGS\Apps\Global\FILES\Import\GWS-Upload-TST\"

    With Application
        .ScreenUpdating = False

         '       Copy specific sheets
        Sheets(Array("ITEM OUTPUT", "ROUTING")).Copy

         '       Paste sheets as values
        For Each ws In ActiveWorkbook.Worksheets
            ws.Cells.Copy
            ws.[A1].PasteSpecial Paste:=xlValues
            Application.CutCopyMode = False
            Cells(1, 1).Select
            ws.Activate
        Next ws
        Cells(1, 1).Select

        ActiveWorkbook.SaveCopyAs Filelocation & Filename
        ActiveWorkbook.Close SaveChanges:=False

        .ScreenUpdating = True
    End With
End Sub

Use another sub to 'cleanse' the sheet

Sub ApplyValuesTo(ByVal sh As Excel.Worksheet)

    For Each cell In sh.UsedRange.Cells
        With cell
            .Value = .Value
            '// This may take a while; the next line will allow you to manually 
            '// break if needed (e.g. If you have a lot of data in the sheet)
            DoEvents 
        End With
    Next

End Sub

You are saving as .xls file, but have not specified that file format in the saveAs method. That's why you are getting security warnings...

You need to specify that parameter in the SaveAs method.

ActiveWorkbook.SaveAs Filename:=Filelocation & Filename, FileFormat:=56

Here is a link to MSDN page for the various fileFormat parameters: https://msdn.microsoft.com/en-us/library/office/ff198017.aspx

EDIT - for second problem you face:

As for the fact that formats are being carried across, that is because you are not using the correct Enumeration value.

ws.[A1].PasteSpecial Paste:=xlValues

should be:

ws.[A1].PasteSpecial Paste:=xlPasteValues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM