+ Reply to Thread
Results 1 to 8 of 8

general purpose command button

Hybrid View

  1. #1
    Registered User
    Join Date
    02-20-2014
    Location
    nebraska
    MS-Off Ver
    Excel 2003
    Posts
    6

    general purpose command button

    I have Created a Userform with two buttons and a couple of text boxes and I would like this form to act similar to a vbyesno msgbox. The problem I am having is that I can't figure out how to make these buttons function without having to put a bunch of if-then statements in a Commandbutton1_click sub. What I would like to do is write a macro in a Module and then call the userform with text and write an if-then statement with the code for the buttons. For Instance:

    Sub Filesaveas()
    '
    ' Save Invoice As PDF for Billing Records
    '
    
    ' Save File
        Dim IntialName As String
        Dim fileSaveName As Variant
        InitialName = Range("F7").Text & "_" & Range("K2") & "_" & Range("F6")
        fileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitialName, _
        FileFilter:="PDF (*.pdf), *pdf")
            If fileSaveName <> False Then
                UserForm1.Caption = "Save As"
                UserForm1.TextBox1.Caption = "Save As:"
                UserForm1.TextBox2.Caption = "'" & fileSaveName & "'"
                UserForm1.TextBox1.Font.Bold = True
                UserForm1.TextBox2.Font.Bold = False
                UserForm1.TextBox1.Font.Italic = False
                UserForm1.TextBox2.Font.Italic = True
                UserForm1.TextBox2.TextAlign = fmTextAlignCenter
                UserForm1.TextBox1.TextAlign = fmTextAlignLeft
                UserForm1.Show
                If 'CommandButton1_Click Here' Then
                   'Action'
                Else
                    If 'CommandButton2_Click Here' Then
                        'Action'
                    End If
                End If
            End If
    End Sub
    Last edited by Son_of_Thor; 03-04-2014 at 02:14 PM.

  2. #2
    Registered User
    Join Date
    02-20-2014
    Location
    nebraska
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: general purpose command button

    I appologize for the ugly code, it is my first time posting and when posted it removed all my tabs and spacing. Any advice for future posting is welcomed.

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: general purpose command button

    Hi, Son_of_Thor,

    have a look at the Forum Rules, especially Rule #3.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    02-20-2014
    Location
    nebraska
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: general purpose command button

    Thank you HaHoBe!

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: general purpose command button

    Hi, Son_of_Thor,

    I would put the code for the commandbuttons behind the UserForm, and I would handle the coding differently from what you want to do.

    Sub Filesaveas()
    '
    ' Save Invoice As PDF for Billing Records
    '
    
    ' Save File
        Dim IntialName As String
        Dim fileSaveName As Variant
        InitialName = Range("F7").Text & "_" & Range("K2") & "_" & Range("F6")
        fileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitialName, _
            FileFilter:="PDF (*.pdf), *pdf")
        If fileSaveName <> False Then
            With UserForm1
              .Caption = "Save As"
              With .TextBox1
                .Caption = "Save As:"
                .Font.Bold = True
                .Font.Italic = False
                .TextAlign = fmTextAlignLeft
              End With
              With .TextBox1
                .Caption = "'" & fileSaveName & "'"
                .Font.Bold = False
                .Font.Italic = True
                .TextAlign = fmTextAlignLeft
              End With
              .Show
            End With
        End If
    End Sub
    Ciao,
    Holger

  6. #6
    Registered User
    Join Date
    02-20-2014
    Location
    nebraska
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: general purpose command button

    I appreciate you cleaning up my coding, it looks much better. As for coding behind the Userform, I have thought of this and would do this but it doesn't seem to workout for me and the reason being because i am using the GetSaveAsFilename application property and therefore when I use the Save action it no longer pulls the filename from the save as dialog box nor does it use the initial name generated from the Sub Filesaveas if the user chooses not to change the filename. How can I get away from this problem? and here is the code I would like to perform with the CommandButton1_click action:

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fileSaveName, _
    Quality:=xlQualityStandard, includedocproperties:=True, IgnorePrintAreas:=False, _
    openafterpublish:=True

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: general purpose command button

    Hi, Son_of_Thor,

    you passed it to Textbox1 on the UserForm, why not take it from there?

    Ciao,
    Holger

  8. #8
    Registered User
    Join Date
    02-20-2014
    Location
    nebraska
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: general purpose command button

    Thanks HaHoBe, that will work. I knew I had to be missing something simple, I really appreciate you pointing this out.

+ 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. General ODBC error on SQL command
    By penfold1992 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-25-2013, 06:08 AM
  2. Create a command button with code with a command button
    By jakara in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-29-2013, 01:28 PM
  3. [SOLVED] vba code, command bar, command bar button, one button works but not two
    By amazingg64 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2012, 09:55 AM
  4. Replies: 1
    Last Post: 09-17-2007, 09:57 PM
  5. [SOLVED] command button:Can i use a command button to open diffrrent sheets
    By Hellboy in forum Excel General
    Replies: 0
    Last Post: 11-03-2005, 11:00 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