VBA Excel copy sheet as value and save to directory

martinus1988

New Member
Joined
Aug 13, 2015
Messages
15
Hi,

I have a sheet that saves to a specified directory. The problem is that it saves with all the formulas and always lookes data up in the main workbook. That can give errors so i need to save the sheet as value.
[Excel 2013]

HTML:
Sub Save_To_Excel()
Sheets("Offerte").Copy
With ActiveSheet.UsedRange
    .Value = .Value

    Dim wb As Workbook
    Set wb = Workbooks.Add
    ThisWorkbook.Sheets("Offerte").Copy Before:=wb.Sheets(1)
    wb.SaveAs ThisWorkbook.Sheets("Medewerkers").Range("I11").Value & ".xlsx"
    
    End With
End Sub

I added this;
Sheets("Offerte").Copy
With ActiveSheet.UsedRange
.Value = .Value


But this isnt oke.
Iam also looking for a way that the sheet will not open automatically anymore. (somthing like OpenAfterSave=false)
Please help.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Does this work?
Code:
Sub Save_To_Excel()
Dim wb As Workbook

    Sheets("Offerte").Copy

    With ActiveSheet.UsedRange
        .Value = .Value
    End With
    
    Set wb = ActiveWorkbook
    
    wb.SaveAs ThisWorkbook.Sheets("Medewerkers").Range("I11").Value & ".xlsx"
    
    wb.Close SaveChanges:=False    

End Sub
 
Upvote 0
This is working, BUT it gives conflicts with the other macro's (save as pdf and automail...)

Please can the code be any different?

thank you very much.
 
Upvote 0
What conflicts?

It conflict with the following:

When i made a quotation i have 2 buttons: Send and reset.
When i press send the sheet ("Offerte") (means quotation) will normally save as xlsx and PDF. Then microsoft outlooks starts with the quotation in attachment so i only need to press send. The annoying part was that the xlsx opens after pressing send and that the xlsx werent values.

When i now press send, it wouldnt save as pdf anymore (sometimes it works when running directly from vba but then it saves another sheet named: Medewerkers).

Check code of funtioning module:
HTML:
Option Explicit

'The code below are used by the macros in the other two modules
'Do not change the code in the functions in this module

Function RDB_Create_PDF(Source As Object, FixedFilePathName As String, _
                        OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
    Dim FileFormatstr As String
    Dim Fname As Variant

    'Test If the Microsoft Add-in is installed
    If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
         & Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then

        If FixedFilePathName = "" Then
            'Open the GetSaveAsFilename dialog to enter a file name for the pdf
            FileFormatstr = "PDF Files (*.pdf), *.pdf"
            Fname = Application.GetSaveAsFilename("", FileFilter:=FileFormatstr, _
                                                  Title:="Create PDF")

            'If you cancel this dialog Exit the function
            If Fname = False Then Exit Function
        Else
            Fname = FixedFilePathName
        End If

        'If OverwriteIfFileExist = False we test if the PDF
        'already exist in the folder and Exit the function if that is True
        If OverwriteIfFileExist = False Then
            If Dir(Fname) <> "" Then Exit Function
        End If

        'Now the file name is correct we Publish to PDF
        On Error Resume Next
        Source.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                FileName:=Fname, _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=OpenPDFAfterPublish
        On Error GoTo 0

        'If Publish is Ok the function will return the file name
        If Dir(Fname) <> "" Then RDB_Create_PDF = Fname
    End If
End Function



Function RDB_Mail_PDF_Outlook(FileNamePDF As String, StrTo As String, _
                              StrCC As String, StrBCC As String, StrSubject As String, _
                              Signature As Boolean, Send As Boolean, StrBody As String)
    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        If Signature = True Then .Display
        .To = StrTo
        .CC = StrCC
        .BCC = StrBCC
        .Subject = StrSubject
        .HTMLBody = StrBody & "<br>" & .HTMLBody
        .Attachments.Add FileNamePDF
        .Attachments.Add = ThisWorkbook.Sheets("Medewerkers").Range("S2").Value
        .Attachments.Add = ThisWorkbook.Sheets("Medewerkers").Range("S3").Value
        If Send = True Then
            .Send
        Else
            .Display
        End If
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Function



Function Create_PDF_Sheet_Level_Names(NamedRange As String, FixedFilePathName As String, _
                                      OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
'This function will create a PDF with every sheet with
'a sheet level name variable <NamedRange> in it
    Dim FileFormatstr As String
    Dim Fname As Variant
    Dim Ash As Worksheet
    Dim SH As Worksheet
    Dim ShArr() As String
    Dim s As Long
    Dim SheetLevelName As Name

    'Test If the Microsoft Add-in is installed
    If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
         & Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then

        'We fill the Array with sheets with the sheet level name variable
        For Each SH In ActiveWorkbook.Worksheets
            If SH.Visible = -1 Then
                Set SheetLevelName = Nothing
                On Error Resume Next
                Set SheetLevelName = SH.Names(NamedRange)
                On Error GoTo 0
                If Not SheetLevelName Is Nothing Then
                    s = s + 1
                    ReDim Preserve ShArr(1 To s)
                    ShArr(s) = SH.Name
                End If
            End If
        Next SH

        'We exit the function If there are no sheets with
        'a sheet level name variable named <NamedRange>
        If s = 0 Then Exit Function

        If FixedFilePathName = "" Then

            'Open the GetSaveAsFilename dialog to enter a file name for the pdf
            FileFormatstr = "PDF Files (*.pdf), *.pdf"
            Fname = Application.GetSaveAsFilename("", FileFilter:=FileFormatstr, _
                                                  Title:="Create PDF")

            'If you cancel this dialog Exit the function
            If Fname = False Then Exit Function
        Else
            Fname = FixedFilePathName
        End If


        'If OverwriteIfFileExist = False we test if the PDF
        'already exist in the folder and Exit the function if that is True
        If OverwriteIfFileExist = False Then
            If Dir(Fname) <> "" Then Exit Function
        End If

        Application.ScreenUpdating = False
        Application.EnableEvents = False

        'Remember the ActiveSheet
        Set Ash = ActiveSheet

        'Select the sheets with the sheet level name in it
        Sheets(ShArr).Select

        'Now the file name is correct we Publish to PDF
        On Error Resume Next
        ActiveSheet.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                FileName:=Fname, _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=OpenPDFAfterPublish
        On Error GoTo 0

        'If Publish is Ok the function will return the file name
        If Dir(Fname) <> "" Then
            Create_PDF_Sheet_Level_Names = Fname
        End If

        Ash.Select

        Application.ScreenUpdating = True
        Application.EnableEvents = True
    End If
End Function

Check code of saving pdf/ create mail

HTML:
Option Explicit

Sub RDB_Worksheet_Or_Worksheets_To_PDF_And_Create_Mail2()
    Dim FileName As String

    If ActiveWindow.SelectedSheets.Count > 1 Then
        MsgBox "There is more then one sheet selected," & vbNewLine & _
               "be aware that every selected sheet will be published"
    End If
    'Call the function with the correct arguments
    'Tip: You can also use Sheets("YourSheetName") instead of ActiveSheet in the code(sheet not have to be active then)
    FileName = RDB_Create_PDF(Source:=ActiveSheet, _
                              FixedFilePathName:=ThisWorkbook.Sheets("Medewerkers").Range("I11").Value & ".pdf", _
                              OverwriteIfFileExist:=True, _
                              OpenPDFAfterPublish:=False)
    'Zet de OpenPDFAfterPublish:=True) aan om PDF te openen voor verzending.
    If FileName <> "" Then
        RDB_Mail_PDF_Outlook FileNamePDF:=FileName, _
                             StrTo:=ThisWorkbook.Sheets("Offerte").Range("D11").Value, _
                             StrCC:="", _
                             StrBCC:="sales@hollandaviation.com", _
                             StrSubject:="Requested quotation " & Sheets("Offerte").Range("I9").Value, _
                             Signature:=True, _
                             Send:=False, _
StrBody:="<font face=""century gothic"" color=""#17365D""><h3><B>Dear " & Sheets("Offerte").Range("D9").Value & "</B></h3><br>" & _
          "<body>Thank you for your interest in Holland Aviation." & _
          "<br><br>" & "We have made the quotation you asked for. please check attachment." & _
          "<br><br>" & "If you have further question, then please let us know.</body></font>"
    'Zet de Send:=True, _) aan om mnail gelijk te verzenden!
    Else
        MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
               "Microsoft Add-in is not installed" & vbNewLine & _
               "You Canceled the GetSaveAsFilename dialog" & vbNewLine & _
               "The path to Save the file in arg 2 is not correct" & vbNewLine & _
               "You didn't want to overwrite the existing PDF if it exist"
    End If
    'http://www.mrexcel.com/forum/excel-questions/875351-macro-send-multiple-pdf-per-mail.html#post4244684
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top