combine code into one button

jnsigman

New Member
Joined
Apr 7, 2017
Messages
6
Is there a way to combine this code into one big button. I had one before that called each macro but it stopped working for some reason
Code:
Sub SavePDF()    Dim strFileName As String
    Dim UserName As String
UserName = VBA.Environ$("username")
strFileName = Range("A52")
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
            Filename:="C:\Users\" & Environ("Username") & "\Google Drive\Customer Bills\" & strFileName
           MyPrint
    End
End Sub
Sub SaveExcel()
    Dim strFileName As String
    Dim UserName As String
UserName = VBA.Environ$("username")
strFileName = Range("A52")
        ActiveWorkbook.saveas Filename:="C:\Users\" & Environ("Username") & "\Google Drive\Customer Bills\" & strFileName, FileFormat:= _
        xlOpenXMLWorkbookMacroEnabled
        SavePDF
    End
End Sub
Sub MyPrint()
 ActiveSheet.PrintOut
 End Sub

Here is the code that quit working
Code:
Sub AllMacros()SaveExcel
MyPrint
SavePDF


End Sub

I looking to create a separte email button as well as add it into the big code as well because some of my pdf i just print and some I email and print
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I have a typo in the code the saveexcel is on its own line in my code some reason when i typed the thread it did not end up there
 
Upvote 0
Code:
Option Explicit


Sub SavePDF()
Dim strFileName As String
Dim UserName As String
UserName = VBA.Environ$("username")


strFileName = Range("A52")


        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:="C:\Users\" & Environ("Username") & "\Google Drive\Customer Bills\" & strFileName
        
        MyPrint [B][COLOR=#ff0000] '<----- See notes below re: Macros[/COLOR][/B]
        
        End      [B][COLOR=#ff0000] '<-----I don't see why this command is here. Shouldn't need it.[/COLOR][/B]
    
End Sub


Sub SaveExcel()
Dim strFileName As String
Dim UserName As String
UserName = VBA.Environ$("username")


strFileName = Range("A52")
        ActiveWorkbook.SaveAs Filename:="C:\Users\" & Environ("Username") & "\Google Drive\Customer Bills\" & strFileName, FileFormat:= _
        xlOpenXMLWorkbookMacroEnabled
        
        SavePDF  [B][COLOR=#ff0000] '<----- See notes below re: Macros[/COLOR][/B]
        
    'End  [B][COLOR=#ff0000]'<-----I don't see why this command is here. Shouldn't need it.[/COLOR][/B]
    
End Sub


Sub MyPrint()
    ActiveSheet.PrintOut
End Sub


Sub AllMacros()
    SaveExcel
    MyPrint
    SavePDF
End Sub



I am presuming your project first calls the ALLMACROS macro ? Which then calls, in this order,
SAVEEXCEL macro, then the SAVEPDF macro ? If this is true, please explain why the SAVEPDF macro
at the top ends by calling the MYPRINT macro ? Also, why the SAVELEXCEL macro calls the SAVEPDF macro
at the end ? Is it necessary to run all macros twice ? If not, you can comment out those macro
calls with a hyphen in front of the call or just delete it altogther.

Then the ALLMACROS macro will call each macro in order as required, and only progress to the next
macro when the previous one has finished its job.

Does that make sense ?
 
Upvote 0
You Make Absolute sense there but I have to give a better picture to why it was written like that. each one of those used to be a separate button that would work independently or I could click one button that called all the macros. It worked up until I changed it to be used across multiple computers with google drive. It used to be it referenced the default documents location and then the person I wrote this for wanted to work from multiple locations so we first had to change the save location. Then he wanted it to automatically generate the file name which it does, but when made those changes it broke the button that did everything and not the individual buttons. So to get around that I made a temporary fix of making it to were at the end of each macro it called the next since my button did not work. I will upload a copy of the template so you can see it better and a picture of what I am talking about.



Code:
Option Explicit


Sub SavePDF()
Dim strFileName As String
Dim UserName As String
UserName = VBA.Environ$("username")


strFileName = Range("A52")


        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:="C:\Users\" & Environ("Username") & "\Google Drive\Customer Bills\" & strFileName
        
        MyPrint [B][COLOR=#ff0000] '<----- See notes below re: Macros[/COLOR][/B]
        
        End      [B][COLOR=#ff0000] '<-----I don't see why this command is here. Shouldn't need it.[/COLOR][/B]
    
End Sub


Sub SaveExcel()
Dim strFileName As String
Dim UserName As String
UserName = VBA.Environ$("username")


strFileName = Range("A52")
        ActiveWorkbook.SaveAs Filename:="C:\Users\" & Environ("Username") & "\Google Drive\Customer Bills\" & strFileName, FileFormat:= _
        xlOpenXMLWorkbookMacroEnabled
        
        SavePDF  [B][COLOR=#ff0000] '<----- See notes below re: Macros[/COLOR][/B]
        
    'End  [B][COLOR=#ff0000]'<-----I don't see why this command is here. Shouldn't need it.[/COLOR][/B]
    
End Sub


Sub MyPrint()
    ActiveSheet.PrintOut
End Sub


Sub AllMacros()
    SaveExcel
    MyPrint
    SavePDF
End Sub



I am presuming your project first calls the ALLMACROS macro ? Which then calls, in this order,
SAVEEXCEL macro, then the SAVEPDF macro ? If this is true, please explain why the SAVEPDF macro
at the top ends by calling the MYPRINT macro ? Also, why the SAVELEXCEL macro calls the SAVEPDF macro
at the end ? Is it necessary to run all macros twice ? If not, you can comment out those macro
calls with a hyphen in front of the call or just delete it altogther.

Then the ALLMACROS macro will call each macro in order as required, and only progress to the next
macro when the previous one has finished its job.

Does that make sense ?
https://drive.google.com/file/d/0B8AP8WNn2gVsekM0NmZNYUdwTms/view?usp=sharing <Link to Sheet>
https://drive.google.com/file/d/0B8AP8WNn2gVsYkFHR3JFOXhBV0U/view?usp=sharing <link to Picture>
 
Upvote 0
.
.
Code:
Sub SavePDF()
Dim strFileName As String
Dim UserName As String

UserName = VBA.Environ$("username")
strFileName = [B][COLOR=#ff0000]Range("A52").Value[/COLOR][/B]

        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
[B][COLOR=#ff0000]        Filename:="C:\Users\" & Environ("Username") & "\Google Drive\Customer Bills\" & strFileName & ".xlsm"[/COLOR][/B]
        MyPrint
    End
End Sub


Sub SaveExcel()
Dim strFileName As String
Dim UserName As String
    
UserName = VBA.Environ$("username")
[B][COLOR=#ff0000]strFileName = Range("A52").Value[/COLOR][/B]

[B][COLOR=#ff0000]        ActiveWorkbook.SaveCopyAs "C:\Users\" & Environ("Username") & "\Google Drive\Customer Bills\" & strFileName & ".xlsm"[/COLOR][/B]
        SavePDF
    End
End Sub

Sub MyPrint()
 ActiveSheet.PrintOut
End Sub
 
Sub NextInvoice()
    Range("A46").Value = Range("A46").Value + 1
End Sub

Tested your workbook here and it functions as intended with the following changes to the macros :

The changes I made are highlighted in RED COLOR.

When referring to the content in a cell, adding .Value at the end tells Excel to refer to the data in the cell and not just the cell itself.

When saving the file, strFileName is the file name but does not include the file extension at the end. Adding & ".xlsm" tells Excel to
include the extension also. This extension (.xlsm) refers to a workbook with macros "m".

In the SaveExcel macro, I changed the method of saving from " xlOpenXMLWorkbookMacroEnabled " to: ActiveWorkbook.SaveCopyAs "C:\Users" & Environ("Username") & "\Google Drive\Customer Bills" & strFileName & ".xlsm" because the previous method was closing the activeworkbook and leaving the backup copy open. That seemed
confusing to me. I believe the preferred method (as with this new code) is to simply COPY the existing workbook and leave the original open to continue working. Maybe it's just
a personal preference for me ..... unless you meant to close the activeworkbook and then do work in the new backup version ? In that case, don't change this one line of code.

The only thing that might still be of issue is the directory path where the workbook is being backed up and / or where the PDF file is saved. I would verify those paths with the user
to insure it is accurate.

Hope this helps.
 
Upvote 0
This makes total sense to me now the only other thing that comes to mind that I been trying to accomplish and but have not been able to get to work at least with my writing code writing is on enthe PDF is saved in need to be able to either print it or email it is there a way to have it print one copy automatically once it saves and ask if I going to email it as PDF then bring up outlook in display mode or print it if I say no I not emailing it. I read several forums and tried several codes mixed and match and it seems to fail do you have a sorta straight forward solution that work with my code.

Thanks again for the help I done a lot of basic macro writing but this has been a bit more to me to play with.
 
Upvote 0
.
.
See how this looks now :

Code:
Option Explicit


Sub All()
    SaveAndPrintPDF
    CreatBackUp
End Sub

Sub SaveAndPrintPDF()
Dim strFileName As String
Dim UserName As String

UserName = VBA.Environ$("username")
strFileName = Range("A52").Value

        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:="C:\Users\" & Environ("Username") & "\Google Drive\Customer Bills\" & strFileName & ".xlsm"
        MyPrint
    End
End Sub

Sub SavePDF()
Dim strFileName As String
Dim UserName As String

UserName = VBA.Environ$("username")
strFileName = Range("A52").Value

        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:="C:\Users\" & Environ("Username") & "\Google Drive\Customer Bills\" & strFileName & ".xlsm"
      
    End
End Sub

Sub CreatBackUp()
Dim strFileName As String
Dim UserName As String
    
UserName = VBA.Environ$("username")
strFileName = Range("A52").Value

        ActiveWorkbook.SaveCopyAs "C:\Users\" & Environ("Username") & "\Google Drive\Customer Bills\" & strFileName & ".xlsm"
        SavePDF
    End
End Sub

Sub MyPrint()
 ActiveSheet.PrintOut
End Sub
 
Sub NextInvoice()
    Dim num As Integer
    Range("B47").Select
    num = Range("B47").Value
    num = num + 1
    Range("B47").Value = num
End Sub

Here's a link to download the updated file so you don't have to reptype or copy/paste everything : https://www.amazon.com/clouddrive/s...KsxWgx8BUGvUu6eS89?ref_=cd_ph_share_link_copy
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,543
Members
449,316
Latest member
sravya

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