+ Reply to Thread
Results 1 to 6 of 6

Saving excel macro based on commandbutton1 click (user form)

Hybrid View

  1. #1
    Registered User
    Join Date
    06-07-2013
    Location
    Vilnius, Lithuania
    MS-Off Ver
    Excel 2007
    Posts
    86

    Saving excel macro based on commandbutton1 click (user form)

    Dear all,

    I am trying to write macro which would start when open the workbook, then the user make necessary changes and macro will start again after close + save the workbook.

    Please find a dummy code below:

    Main:
    Private Sub Workbook_Open()
    
    UserForm1.Show
    
    End Sub
    Userform1:
    Private Sub CommandButton1_Click()
    Range("A1").Value = "You"
    End Sub
    
    Private Sub CommandButton2_Click()
    Range("A1").Value = "I"
    End Sub
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
         
    Dim ws As Worksheet
    Dim pwd As String
    
    pwd = "Hi" ' Put your password here
    For Each ws In Worksheets
        ws.Protect Password:=pwd
    Next ws
         
    End Sub
    I am struggling with the last part. I need to run different macro when exiting and saving the workbook, depending on Userform CommandButton choice at the begining.
    Is there a way to do it?
    Thanks a lot!

  2. #2
    Registered User
    Join Date
    06-07-2013
    Location
    Vilnius, Lithuania
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Saving excel macro based on commandbutton1 click (user form)

    Hi all!

    I though about the last part and came up with something like below:
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
     If UserForm1.ActiveControl.Name = "CommandButton1" Then
     MsgBox "1"
     Else
     MsgBox "2"
     End If
     End Sub
    How properly write this part in order to work? At the moment it does nothing... Any thoughts?
    Thanks!

  3. #3
    Registered User
    Join Date
    08-11-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    11

    Re: Saving excel macro based on commandbutton1 click (user form)

    It looks like you can just check the "A1" value, no? If so, you could just use:
    If ActiveWorkbook.Sheets("Sheet1").Range("A1").Value = "You" Then
    Application.Run "Macro1"
    Elseif ActiveWorkbook.Sheets("Sheet1").Range("A1").Value = "I" Then
    Application.Run "Macro2"
    End If

    Or if you prefer checking the ActiveControl property, just swap out the conditionals.
    Private Sub Workbook_BeforeClose(Cancel As Boolean) might be appropriate as well.

    Hope this helps!

  4. #4
    Registered User
    Join Date
    06-07-2013
    Location
    Vilnius, Lithuania
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Saving excel macro based on commandbutton1 click (user form)

    Hi Aule,

    Thanks for a reply! However, I can't check values because there will be no values. The one posted is just the "dummy" code. Basically, I need a macro to remember which button was clicked and then act accordingly depending on that when saving an excel workbook.
    Could you please clarify remove the conditions for me? If i will remove how to force macro to work according different button clicked?

    Thanks a lot!

  5. #5
    Registered User
    Join Date
    08-11-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    11

    Re: Saving excel macro based on commandbutton1 click (user form)

    Ah, I see. What I like to do is keep a very hidden sheet in the workbook to track these things, then you can put an indicator in that sheet without sacrificing user interface. It's a bit of a workaround, but it's what I do. Basically, add a new sheet, then run a quick sub for ActiveWorkbook.Sheets("NewSheetName").Visible = xlVeryHidden.
    Then as part of the callback for your button clicks, you can set the "A1" value in the hidden sheet to whatever indicator you want. After that, when you go to close the workbook, you can check those values:
    Sub CommandButton1_Click()
    ActiveWorkbook.Sheets("NewSheetName").Visible = xlVeryHidden"
    ActiveWorkbook.Sheets("NewSheetName").Range("A1").Value = "button 1"
    End Sub
    
    
    If ActiveWorkbook.Sheets("NewSheetName").Range("A1")Value = "button 1" Then
       'do some stuff
    ElseIf ActiveWorkbook.Sheets("NewSheetName").Range("A1")Value = "button 2" Then
       'do some different stuff
    End If
    That's how I'd go about it. That way, if you add other buttons and the user clicks them, it doesn't mess with anything.
    Hope this is satisfactory
    Last edited by Aule; 08-25-2015 at 06:10 PM.

  6. #6
    Registered User
    Join Date
    06-07-2013
    Location
    Vilnius, Lithuania
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Saving excel macro based on commandbutton1 click (user form)

    Thanks a lot! It did a trick. I didn't even know about very hidden

+ 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. macro to open userform1 turn optionbox1 to true and click commandbutton1
    By Jenkins27 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-15-2015, 09:20 PM
  2. [SOLVED] populate text box in user form with cell location based on click within worksheet
    By mcdermott2 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-19-2015, 09:15 PM
  3. [SOLVED] EXCEL USER FORM- data not saving to sheet 3
    By PANTECH in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-17-2013, 01:27 PM
  4. User form and list - click events
    By vn900 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2011, 12:02 AM
  5. Why is the scrollbar in a user form, by the first click on a button, scrolling?
    By Excel-Master in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-21-2010, 11:01 PM
  6. user form, saving values
    By ophelia in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-05-2006, 07:47 AM
  7. Trouble with saving user input in an Excel Form.
    By University of Maine student in forum Excel General
    Replies: 0
    Last Post: 03-14-2006, 03:35 PM

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