+ Reply to Thread
Results 1 to 7 of 7

Commandbutton to Workbook Open

  1. #1
    Richard
    Guest

    Commandbutton to Workbook Open

    How would you send code from the CommandButton2 located on a Userform to code
    located on Workbook open(). Example:
    If Commandbutton2 is "clicked" then GoTo Workbook Open() Inputagain:
    I've tried GoTo Inputagain from the CommandButton2 but that doesn't work,
    Variable not Defined! Any suggestions. Thanks in Advance

  2. #2
    Toppers
    Guest

    RE: Commandbutton to Workbook Open

    Richard,
    Can you not put your "Inputagain" code in a general module,
    which can be called from Workbook_Open and hence also from your CommandButton?

    HTH

    "Richard" wrote:

    > How would you send code from the CommandButton2 located on a Userform to code
    > located on Workbook open(). Example:
    > If Commandbutton2 is "clicked" then GoTo Workbook Open() Inputagain:
    > I've tried GoTo Inputagain from the CommandButton2 but that doesn't work,
    > Variable not Defined! Any suggestions. Thanks in Advance


  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,480

    This Again

    Did you just ask this exact question again,
    Copy the macro that is in the workbook open event and place it in your command button code, you may have to do some adjusting, but that's a given with xl.
    The workbook open event works when you open the workbook

  4. #4
    Richard
    Guest

    Re: Commandbutton to Workbook Open

    Yes I did ask the same question, but I thought I might have misworded it so I
    sent it again. Again I can't copy the code for the Workbook Open over to the
    CommandButton because I need the code in the workbook open. I need it to Exit
    the workbook Open code IF the commandbutton2 is selected because if all the
    code is ran and the user selects the commandButton2 with out any input then
    this code won't be ran untill next year at that date. Therefore not have any
    days added to there vacation. I can remove the CommandButton2 and not give
    the user that opition to "Remind me later" but I thought excel could do most
    anything. Thanks anyway for your help.

    "davesexcel" wrote:

    >
    > Did you just ask this exact question again,
    > Copy the macro that is in the workbook open event and place it in your
    > command button code, you may have to do some adjusting, but that's a
    > given with xl.
    > The workbook open event works when you open the workbook
    >
    >
    > --
    > davesexcel
    > ------------------------------------------------------------------------
    > davesexcel's Profile: http://www.excelforum.com/member.php...o&userid=31708
    > View this thread: http://www.excelforum.com/showthread...hreadid=516694
    >
    >


  5. #5
    GS
    Guest

    RE: Commandbutton to Workbook Open

    Hi Richard,

    You need to entirely restructure what you're doing for it to work how you
    want it to! If you're interested in how, read on:

    First:
    Move all the code to a standard module. Enter something like the following
    in that module:


    Option Explicit

    'Declare the following global variable so it can be accessed from anywhere
    in your code
    'The button on UserForm1 will set its value to TRUE if clicked
    Public bRemindMeLater As Boolean


    'Put your code in its own procedure, something like this:
    Sub CheckVacationDays()

    Dim L As Long, M As Long
    Dim Ans as Variant

    L = Month(Date) * 10000 + Year(Date)
    M = GetSetting("Demo", "Drafts", "Month", 0)

    If L = M Then Exit Sub

    If Month(Date) = 4 Then
    If Day(Date) >= 24 And Day(Date) < 27 Then
    bReminMeLater = False '**Optional: It's default value is FASLE, but
    this line ensures it!!
    UserForm1.Show
    End If

    'Here is the solution to your problem!
    'Read the value placed in the variable by the button on UserForm1
    If bRemindMeLater Then GoTo InputLater

    Ans = MsgBox("Do you qualify for Bank Day?", vbYesNo)
    If Ans = vbYes Then
    MsgBox ("1 Day has been added to your Bank Days")
    SaveSetting "Demo", "Drafts", "Month", L '**Notice no parenthesis
    used here!!
    Worksheets("Vacation").Range("C3").Value = 1
    Else
    Worksheets("Vacation").Range("C3").Value = 0 '**Do you really need
    this??
    End If

    InputLater:
    End If

    End Sub


    Second:
    Enter the following in ThisWorkbook: '**Replaces what you have now!!

    Option Explicit

    Private Sub Workbook_Open()
    CheckVacationDays
    End Sub


    Third:
    With UserForm1, implement some good practices for naming controls so they're
    easier to associate with.
    For example, you could rename CommandButton2 to: cmdRemindMeLater
    Enter the following in the code behind the userform:

    Private Sub cmdRemindMeLater_Click()
    bRemindMeLater = True '**Tell us the user clicked the button
    Unload Me '**If you want to dismiss the userform here!!
    End Sub


    I think this should work how you want.
    Good luck!
    GS

  6. #6
    Richard
    Guest

    RE: Commandbutton to Workbook Open

    Yes, that works, I just could not figure out the Globel thing! Thank you so
    much. I new there was a way!

    "GS" wrote:

    > Hi Richard,
    >
    > You need to entirely restructure what you're doing for it to work how you
    > want it to! If you're interested in how, read on:
    >
    > First:
    > Move all the code to a standard module. Enter something like the following
    > in that module:
    >
    >
    > Option Explicit
    >
    > 'Declare the following global variable so it can be accessed from anywhere
    > in your code
    > 'The button on UserForm1 will set its value to TRUE if clicked
    > Public bRemindMeLater As Boolean
    >
    >
    > 'Put your code in its own procedure, something like this:
    > Sub CheckVacationDays()
    >
    > Dim L As Long, M As Long
    > Dim Ans as Variant
    >
    > L = Month(Date) * 10000 + Year(Date)
    > M = GetSetting("Demo", "Drafts", "Month", 0)
    >
    > If L = M Then Exit Sub
    >
    > If Month(Date) = 4 Then
    > If Day(Date) >= 24 And Day(Date) < 27 Then
    > bReminMeLater = False '**Optional: It's default value is FASLE, but
    > this line ensures it!!
    > UserForm1.Show
    > End If
    >
    > 'Here is the solution to your problem!
    > 'Read the value placed in the variable by the button on UserForm1
    > If bRemindMeLater Then GoTo InputLater
    >
    > Ans = MsgBox("Do you qualify for Bank Day?", vbYesNo)
    > If Ans = vbYes Then
    > MsgBox ("1 Day has been added to your Bank Days")
    > SaveSetting "Demo", "Drafts", "Month", L '**Notice no parenthesis
    > used here!!
    > Worksheets("Vacation").Range("C3").Value = 1
    > Else
    > Worksheets("Vacation").Range("C3").Value = 0 '**Do you really need
    > this??
    > End If
    >
    > InputLater:
    > End If
    >
    > End Sub
    >
    >
    > Second:
    > Enter the following in ThisWorkbook: '**Replaces what you have now!!
    >
    > Option Explicit
    >
    > Private Sub Workbook_Open()
    > CheckVacationDays
    > End Sub
    >
    >
    > Third:
    > With UserForm1, implement some good practices for naming controls so they're
    > easier to associate with.
    > For example, you could rename CommandButton2 to: cmdRemindMeLater
    > Enter the following in the code behind the userform:
    >
    > Private Sub cmdRemindMeLater_Click()
    > bRemindMeLater = True '**Tell us the user clicked the button
    > Unload Me '**If you want to dismiss the userform here!!
    > End Sub
    >
    >
    > I think this should work how you want.
    > Good luck!
    > GS


  7. #7
    GS
    Guest

    RE: Commandbutton to Workbook Open

    I'm glad it works, and pleased to help!

    Regards,
    GS

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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