+ Reply to Thread
Results 1 to 3 of 3

Compile Error with Command Button

  1. #1
    Registered User
    Join Date
    06-26-2023
    Location
    London
    MS-Off Ver
    365
    Posts
    6

    Compile Error with Command Button

    I am having issues with adding a VBA code to a command button.
    I am attempting to create an excel training sign off that saves a worksheet as a PDF to a particular folder and also names it using information in certain cells.
    The code I am using is:

    Private Sub CommandButton1_Click()
    Sub SaveAsPDF()
    EmployeeID = Range("F39")
    EmployeeName = Range("F41")
    Path = "H:\LRT\LRT - Training Division Staff - Light Rail\LRT - Training Division - Systems RSF\Trk\Track Training\Sign Offs"
    fname = EmployeeID & "-" & EmployeeName
    MsgBox "Saved as PDF"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, ignoreprintareas:=False, Filename:=Path & fname

    End Sub

    It works when I run the code without the Private Sub , but as soon as I add it I get an error stating "Compile Error: Expected End Sub".
    I have attached a screenshot of the error message.
    I am one level up from being a novice and can usually work these things out, but I am stumped with this one.
    Attached Images Attached Images

  2. #2
    Forum Expert ByteMarks's Avatar
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,176

    Re: Compile Error with Command Button

    You can either delete the Sub line (highlighted)

    Please Login or Register  to view this content.
    Or call the sub from the button
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-26-2023
    Location
    London
    MS-Off Ver
    365
    Posts
    6

    Re: Compile Error with Command Button

    Thanks. That worked!! So simple but effective.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Button in a form keeps bringing up a Compile error
    By Kellise in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-07-2016, 05:42 PM
  2. [SOLVED] Creating a command button to compile specific data
    By evancruz in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-13-2015, 12:39 PM
  3. [SOLVED] Compile Error in Hidden Module and Compile Error: Can't find project or library
    By Taislin in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-10-2013, 07:03 PM
  4. [SOLVED] Compile Error: Method or data member not found when i click Save button...
    By aditbaco in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-15-2013, 07:11 AM
  5. [SOLVED] Option button returning compile error
    By ObiWanBaloney in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-05-2012, 05:10 PM
  6. First timer command button link - compile error expected end sub
    By MONSIRET in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-25-2012, 08:34 AM
  7. Compile Error on VBA Search Button
    By laffers in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-05-2011, 07:23 AM

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